Skip to content

Usage doc for AGP 9 and the new Android Kotlin Multiplatform plugin #404

@skolson

Description

@skolson

I have a Kotlin Multiplatform library that uses JNI for the android target. AGP9 and the new Android KMP plugin com.android.kotlin.multiplatform.library forced some changes since com.android.library plugin is now deprecated for this. I then changed this library's android unit tests to JUnit 6.0.2 and discovered the doc doesn't seem to exist for how to do this yet. Also the amount of stuff I had to change to get the androidDeviceTest dependencies to run tests was surprising - it seems the de.mannodermaus.android-junit5 plugin doesn't help in this setup, even though it could. So thought I'd post this issue to see what people think. Given JUnit 6.0.2 and mannodermaus 2.0.0, the following changes to build.gradle.kts and the Gradle version catalog stuff makes androidDeviceTest unit tests work. Although I've only tested this in Android Studio Panda Canary 4.

in the androidLibrary block required by the new plugin to enable instrumented tests:

        withHostTest {}
        withDeviceTest {
            instrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
            instrumentationRunnerArguments += mapOf(
                "runnerBuilder" to "de.mannodermaus.junit5.AndroidJUnit5Builder"
            )
            execution = "HOST"
        }

        packaging {
            resources.excludes.add("META-INF/LICENSE.md")
            resources.excludes.add("META-INF/LICENSE-notice.md")
        }

The packaging stuff was forced by JUnit 6.0.2.

Changing the block above alone leads to various NoClassdefFound exceptions when trying to run tests, so the androidDeviceTest source set now needs this:

        getByName("androidDeviceTest") {
            dependencies {
                implementation(libs.bundles.androidx.test)
                // whatever other libs required
            }
        }

And the bundle stuff from the Gradle catalog looks like this:

[libraries]

androidx-test-runner = { module = "androidx.test:runner", version.ref = "androidxTestRunner" }
junit5-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "junit5" }
junit5-params = { module = "org.junit.jupiter:junit-jupiter-params", version.ref = "junit5" }
junit5-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit5" }
android-junit5-test-runner = { module = "de.mannodermaus.junit5:android-test-runner", version.ref = "testMannodermausPlugin" }
android-junit5-launcher = { module = "org.junit.platform:junit-platform-launcher", version.ref = "junit5" }
android-junit5-suite-api = { module = "org.junit.platform:junit-platform-suite-api", version.ref = "junit5" }

[bundles]
androidx-test = [ "kotlinx-coroutines-test", "kotlinx-coroutines-core", "junit5-api", "junit5-params", "junit5-engine", "androidx-test-runner", "android-junit5-test-runner", "android-junit5-launcher", "android-junit5-suite-api" ]

My first thought after multiple trial and error attempts to get tests working is that the mannodermaus plugin could help with all these dependencies as they are all non-negotiable as far as I can tell :-). Anyway with all the above, JUnit 6.0.2 instrumented unit tests work just fine. So is this just doc? Or could the plugin be improved to handle this setup?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions