s

    Sourabh Rawat

    8 months ago
    Hi, getting following error when trying to share a jvm target in desktop and android targets
    Could not determine the dependencies of task ':desktop:run'.
    > Could not resolve all task dependencies for configuration ':desktop:jvmRuntimeClasspath'.
       > Could not resolve project :common.
         Required by:
             project :desktop
          > The consumer was configured to find a runtime of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm'. However we cannot choose between the following variants of project :common:
              - commonJvmDefault
              - commonJvmRuntimeElements
              - desktopDefault
              - desktopRuntimeElements
    gradle files posted in thread.
    desktop:build.gradle.kts
    import org.jetbrains.compose.compose
    import org.jetbrains.compose.desktop.application.dsl.TargetFormat
    
    plugins {
        kotlin("multiplatform")
        id("org.jetbrains.compose") version "1.0.0"
    }
    
    group = "com.github.dragneelfps"
    version = "1.0"
    
    kotlin {
        jvm {
            compilations.all {
                kotlinOptions.jvmTarget = "11"
            }
            withJava()
            testRuns["test"].executionTask.configure {
                useJUnitPlatform()
            }
        }
        sourceSets {
            val jvmMain by getting {
                dependencies {
                    implementation(project(":common"))
                    implementation(compose.desktop.currentOs)
                }
            }
            val jvmTest by getting {
                dependencies {
                    implementation(kotlin("test"))
                }
            }
        }
    }
    
    compose.desktop {
        application {
            mainClass = "MainKt"
            nativeDistributions {
                targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
                packageName = "jvm"
                packageVersion = "1.0.0"
            }
        }
    }
    commong:build.gradle.kts
    import org.jetbrains.compose.compose
    
    plugins {
        kotlin("multiplatform")
        id("org.jetbrains.compose") version "1.0.0"
        id("com.android.library")
    }
    
    group = "com.github.dragneelfps"
    version = "1.0"
    
    kotlin {
        android()
        jvm("commonJvm")
        jvm("desktop") {
            compilations.all {
                kotlinOptions.jvmTarget = "11"
            }
            testRuns["test"].executionTask.configure {
                useJUnitPlatform()
            }
        }
        sourceSets {
            val commonMain by getting {
                dependencies {
                    api(compose.runtime)
                    api(compose.foundation)
                    api(compose.material)
                }
            }
            val commonTest by getting {
                dependencies {
                    implementation(kotlin("test"))
                }
            }
            val commonJvmMain by getting {}
            val androidMain by getting {
                dependsOn(commonJvmMain)
                dependencies {
                    api("androidx.appcompat:appcompat:1.2.0")
                    api("androidx.core:core-ktx:1.3.1")
                }
            }
            val androidTest by getting {
                dependencies {
                    implementation("junit:junit:4.13")
                }
            }
            val desktopMain by getting {
                dependsOn(commonJvmMain)
                dependencies {
                    api(compose.preview)
                }
            }
            val desktopTest by getting
        }
    }
    
    android {
        compileSdkVersion(31)
        sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
        defaultConfig {
            minSdkVersion(24)
            targetSdkVersion(31)
        }
        compileOptions {
            sourceCompatibility = JavaVersion.VERSION_1_8
            targetCompatibility = JavaVersion.VERSION_1_8
        }
    }
    For making it work, I have currently updated common:build.gradle.kts with
    val commonJvmMain by creating {}
    val androidMain by getting {
        dependsOn(commonJvmMain)
        dependencies {
            api("androidx.appcompat:appcompat:1.2.0")
            api("androidx.core:core-ktx:1.3.1")
        }
    }
    val desktopMain by getting {
        dependsOn(commonJvmMain)
        dependencies {
            api(compose.preview)
        }
    }