I'm very new to Android - I've added android targe...
# testballoon
r
I'm very new to Android - I've added android target to my KMP library and tried to configure tests. It seems my android host tests don't execute my commonTests. I tried following the the testballon example but I'm not sure what am I missing.
What is important in this build file if I don't want to use robolectric?
I somehow fixed it 😉
It seems adding `testBalloon-integration-robolectric`dependency fixes my problem but I'm not sure why
Without this dependency in my
named("androidHostTest") {}
block running
allTest
gives me this error:
Copy code
Execution failed for task ':modules:kilua-rpc-core:testAndroidHostTest'.
> There are test sources present and no filters are applied, but the test task did not discover any tests to execute. This is likely due to a misconfiguration. Please check your test configuration. If this is not a misconfiguration, this error can be disabled by setting the 'failOnNoDiscoveredTests' property to false.
but running just
./gradlew :modules:kilua-rpc-core:testAndroidHostTest
works fine (!)
After adding the dependency running both
allTest
and single
testAndroidHostTest
works ok.
It's a bit strange for me.
Or not ... the error is back.
adding junit4 instead seems to help
e
o
To configure Android KMP testing using the
com.android.kotlin.multiplatform.library
plugin without Robolectric I'd use something like this (untested):
Copy code
plugins {
    id("org.jetbrains.kotlin.multiplatform")
    id("com.android.kotlin.multiplatform.library")
    id("de.infix.testBalloon")
}

kotlin {
    jvm()

    android {
        namespace = "org.example.android.multiplatform.library"
        compileSdk = TODO()
        minSdk = TODO()

        withHostTestBuilder {}.configure {
            // isIncludeAndroidResources = true
        }
        withDeviceTestBuilder {
            sourceSetTreeName = "test"
        }.configure {
            managedDevices {
                localDevices {
                    @Suppress("UnstableApiUsage")
                    create("pixel2api30") {
                        device = "Pixel 2"
                        apiLevel = 30
                        systemImageSource = "aosp"
                    }
                }
            }
        }
    }

    sourceSets {
        commonTest {
            dependencies {
                implementation("de.infix.testBalloon:testBalloon-framework-core:${testBalloonVersion}")
            }
        }

        named("androidHostTest") {
            dependencies {
                implementation("de.infix.testBalloon:testBalloon-framework-core:${testBalloonVersion}")
                implementation(libs.junit.junit4)
                implementation(libs.androidx.test.core)
            }
        }

        named("androidDeviceTest") {
            dependencies {
                implementation("de.infix.testBalloon:testBalloon-framework-core:${testBalloonVersion}")
                implementation(libs.androidx.test.runner)
            }
        }
    }
}
The Android-related docs are here: developer.android.com/kotlin/multiplatform/plugin I agree that it's somewhat involved, but AGP does not appear stable enough to offer the kind of auto-configuration TestBalloon does for other targets. If that doesn't work, please point me to a branch in your repo where I could try to set it up.
e
Not sure why android test core is needed. I will check what is inside.
o
r
Is adding
testBalloon-framework-core
to all source sets intentional? Shouldn't
commonTest
be enough?
o
Yes, for recent versions of the
com.android.kotlin.multiplatform.library
plugin, that's correct. At some point in the evolution from AGP 8 to AGP 9, not all source sets were connected to the
common
parents. This should be the required minimum:
Copy code
sourceSets {
    commonTest {
        dependencies {
            implementation("de.infix.testBalloon:testBalloon-framework-core:${testBalloonVersion}")
        }
    }

    named("androidHostTest") {
        dependencies {
            implementation(libs.junit.junit4)
        }
    }

    named("androidDeviceTest") {
        dependencies {
            implementation(libs.androidx.test.runner)
        }
    }
}
👍 1