I’m setting up my project, including both desktop ...
# compose-desktop
l
I’m setting up my project, including both desktop and android, to run in multiple environments, but the error below occurred. How can I run my project in different environments?
Copy code
org.gradle.api.InvalidUserCodeException: Only one of the JVM targets can be configured to work with Java. The target 'common' is already set up to work with Java; cannot setup another target 'dev'
👋 1
d
You should create different modules instead of a single module
common
your common module for interfaces
dev
mock
sit
uat
prod
All of these should be depending on
common
and implement the interfaces in their own way
l
Or if there is some dependency, enable mpp hierarchy(
kotlin.mpp.enableGranularSourceSetsMetadata​=​true
) and insert
dependsOn
.
b
Or just try to not use withJava()
l
@Big Chungus without withJava() only work with the desktop only project
b
Really? Why? Can't you use android() and jvm() targets to separate the two ends?
l
I have a commonUI module that has jvm() and android() targets, and i implement the commonUI module in my desktop app. If i remove
withJava()
from my desktop app, it will leave the message
Copy code
Unable to find Gradle tasks to build: [:desktopApp:devMain]. 
			Build mode: COMPILE_JAVA. 
			Tests: All.
and nothing happen, but
withJava()
made it run
b
Ok, makes sense. Thanks for clarifying.
l
@Dragos Rachieru your idea sounds great, but I don’t fully understand it yet. Do you have any sample code or any document related to it? It would be great if you can share them with me
d
common/src/commonMain/ExampleRepository.kt
Copy code
interface ExampleRepository.kt {
  fun getExamples(): List<String>
}
dev/src/commonMain/ExampleRepositoryImpl.kt
Copy code
class ExampleRepositoryImpl(
  private val devNetworkSource: NetworkSource
) : ExampleRepository {

  override fun getExamples(): List<String> = devNetworkSource.fetch()

}
mock/src/commonMain/ExampleRepositoryImpl.kt
Copy code
object ExampleRepositoryMock : ExampleRepository {

  override fun getExamples(): List<String> = List(10) { "Mock $it" }

}
and in your app module you add your dependencies and decide which implementation you want to use, I don't know how to explain this further, you should learn more about dependency injection and type inference
dev/build.gradle.kts
Copy code
implementation(":common")
mock/build.gradle.kts
Copy code
implementation(":common")
app/build.gradle.kts
Copy code
implementation(":common")
implementation(":dev")
implementation(":mock")
app/src/main/kotlin/ExampleRepositoryProvider.kt
Copy code
...
fun provideExampleRepository(appType: AppType): ExampleRepository {
  return when(appType) {
    AppType.Mock -> ExampleRepositoryMock
    AppType.Dev -> ExampleRepositoryImpl(createDevNetworkSource())
  }
}
...
l
Thanks for sharing, your comments are very detailed and helpful. 👍
👍 1
👍🏻 1