I've this error when i try to launch my tests in K...
# multiplatform
m
I've this error when i try to launch my tests in KMM : KotlinSourceSet with name 'androidUnitTest' not found. Someone has it too?
j
Do you have
kotlin.mpp.androidSourceSetLayoutVersion=2
in your gradle.properties?
m
@Jeff Lockhart yes
j
Can you share your build.gradle.kts files?
m
Copy code
import io.gitlab.arturbosch.detekt.Detekt
import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFramework

repositories {
    mavenCentral()
    google()
    gradlePluginPortal()
}
plugins {
    // For the Github action & iOS, add : version("1.8.10")

//    id("org.jetbrains.kotlin.multiplatform") version("1.8.10")
//    id("com.android.library") version("8.0.0")
//    id("org.jetbrains.kotlin.plugin.serialization") version("1.8.10")


    // For Android => no version

    id("org.jetbrains.kotlin.multiplatform")
    id("com.android.library")
    id("org.jetbrains.kotlin.plugin.serialization")

    id("com.chromaticnoise.multiplatform-swiftpackage") version ("2.0.3")
    id("com.google.devtools.ksp") version ("1.8.10-1.0.9")
    id("org.jetbrains.dokka") version ("1.7.20")
    id("com.rickclephas.kmp.nativecoroutines") version ("0.13.3")
    id("io.gitlab.arturbosch.detekt") version ("1.22.0")
}

detekt {
    buildUponDefaultConfig = false
    allRules = false
    autoCorrect = true
    config = files("$projectDir/detektConfig/detekt.yml")
    source = files("src/commonMain/kotlin")
}

tasks.apply {
    withType<Detekt>().configureEach {
        config.setFrom(files(rootProject.file("detektConfig/detekt.yml")))
        jvmTarget = "1.8"
    }

    withType<DokkaTask>().configureEach {
        outputDirectory.set(projectDir.resolve("doc"))
        offlineMode.set(true)
        dokkaSourceSets {
            registering {
                jdkVersion.set(8)
                noJdkLink.set(true)
                noAndroidSdkLink.set(true)
                noStdlibLink.set(true)
                sourceRoots.from(file("src/commonMain/kotlin"))
            }
        }
    }
}

kotlin {
    android {
        publishAllLibraryVariants()
    }

    multiplatformSwiftPackage {
        packageName("MyCardKmm")
        swiftToolsVersion("5.6")
        targetPlatforms {
            iOS { v("14") }
        }
        outputDirectory(File(projectDir, "/"))
        zipFileName("MyCardKmm_iOS")
    }

    val xcf = XCFramework()
    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach {
        it.binaries.framework {
            baseName = "MyCardKmm"
            xcf.add(this)
        }
    }

    sourceSets {
        val commonMain by getting {
            dependencies {
                // ADDED
                // NB : must *not* update ktor before updating kotlinx-coroutines
                implementation(libs.bundles.ktor)
                implementation(libs.koinCore)
                implementation(libs.kotlin.coroutines)
                implementation(libs.napier)
                implementation(libs.datetime)
                implementation(libs.viewmodel)
                implementation(libs.settings)
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4")
            }
        }
        val androidMain by getting {
            dependencies {
                // ADDED
                implementation(libs.ktor.android)
                implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1")
            }
        }
        val androidUnitTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
                implementation("junit:junit:4.13.2")
                implementation(libs.ktor.mock)
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4")
            }
        }
        val iosX64Main by getting
        val iosArm64Main by getting
        val iosSimulatorArm64Main by getting
        val iosMain by creating {
            dependsOn(commonMain)
            iosX64Main.dependsOn(this)
            iosArm64Main.dependsOn(this)
            iosSimulatorArm64Main.dependsOn(this)
            dependencies {
                implementation(libs.ktor.ios)
            }
        }
        val iosX64Test by getting
        val iosArm64Test by getting
        val iosSimulatorArm64Test by getting
        val iosTest by creating {
            dependsOn(commonTest)
            iosX64Test.dependsOn(this)
            iosArm64Test.dependsOn(this)
            iosSimulatorArm64Test.dependsOn(this)
        }
    }
}

dependencies {
    implementation("androidx.preference:preference-ktx:1.2.0")
    configurations
        .filter { it.name.startsWith("ksp") && it.name.contains("Test") }
        .forEach {
            add(it.name, "io.mockative:mockative-processor:1.2.3")
        }
}

android {
    compileSdk = 33
    namespace = "...."
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdk = 23
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }

    buildTypes {
        create("dev") {}
        create("recette") {}
        create("pprod") {}
    }
}
j
Are you using Kotlin 1.8.10? I'm assuming that's defined in your top-level build.gradle.kts.
m
yes 1.8.10
m
is the
kotlin.mpp.androidSourceSetLayoutVersion=2
declaration in your root project
gradle.properties
file?
m
yes
m
shit's wild, I'm not seeing any issues sad panda
m
If this can help, i have put my kmm in an Android project as a module maybe can hep
j
Are you running the tests with
./gradlew :shared-module-name:androidUnitTest
?
m
Cannot locate tasks that match 'corekmm:androidUnitTest' as task 'androidUnitTest' not found in project 'corekmm'. I have this error with your command @Jeff Lockhart
j
I don't see anything obvious myself. I do notice you're using Java 17 in your Android config, and JDK 8 elsewhere.
Copy code
jvmTarget = "1.8"
...
jdkVersion.set(8)
...
compileOptions {
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
}
There are some newer versions of some of your plugin dependencies and could use Kotlin 1.8.21 as well. Not sure any of that would be the cause though.
I'm also not sure what the behavior is having dependencies defined in the top-level
dependencies
block on a KMP project:
Copy code
dependencies {
    implementation("androidx.preference:preference-ktx:1.2.0")
    configurations
        .filter { it.name.startsWith("ksp") && it.name.contains("Test") }
        .forEach {
            add(it.name, "io.mockative:mockative-processor:1.2.3")
        }
}
Seems that should be moved to
Copy code
kotlin {
    sourceSets {
        val androidMain by getting {
            dependencies {
                implementation("androidx.preference:preference-ktx:1.2.0")
                configurations
                    .filter { it.name.startsWith("ksp") && it.name.contains("Test") }
                    .forEach {
                        add(it.name, "io.mockative:mockative-processor:1.2.3")
                    }
                }
            }
        }
    }
}
v
I think you need > 1.8.20
115 Views