TExcelTAD
Home › Mobile Build Pipelines: Gradle/AGP, Xcode, SwiftPM & Fastlane › Dependencies & Unit Tests per Variant

Dependencies & Unit Tests per Variant

Every variant has its own classpaths, so dependencies and tests can differ per flavor and build type. This lesson shows how AGP names those configurations, what it adds on its own, and how local unit tests run on CI without a device.

Dependency configurations multiply too

Besides the plain Gradle configurations (implementation, api, compileOnly, runtimeOnly, testImplementation), AGP creates one per flavor, build type and variant:

Configuration Applies to
implementation every variant
freeImplementation variants of the free flavor
debugImplementation debug variants (e.g. a debug-only inspector library)
freeDebugImplementation one variant (needs explicit declaration in older AGP; rarely needed)
testImplementation local unit tests (run on the JVM)
androidTestImplementation instrumented tests (run on a device or emulator)

The resolved result lives in <variant>RuntimeClasspath and <variant>CompileClasspath, which is what you inspect:

bash · try it yourself
mkdir -p hello/app/src/main/java/com/example/hello && cd hello
cat > settings.gradle.kts <<'KTS'
pluginManagement { repositories { google(); mavenCentral(); gradlePluginPortal() } }
dependencyResolutionManagement { repositories { google(); mavenCentral() } }
rootProject.name = "hello"
include(":app")
KTS
echo 'plugins { id("com.android.application") version "9.4.1" apply false }' > build.gradle.kts
echo '<manifest xmlns:android="http://schemas.android.com/apk/res/android"><application android:label="Hello" /></manifest>' > app/src/main/AndroidManifest.xml
cat > app/build.gradle.kts <<'KTS'
plugins { id("com.android.application") }
android {
    namespace = "com.example.hello"
    compileSdk = 36
    defaultConfig { applicationId = "com.example.hello"; minSdk = 24; targetSdk = 36; versionCode = 1; versionName = "1.0" }
    flavorDimensions += "tier"
    productFlavors { create("free") { dimension = "tier" }; create("paid") { dimension = "tier" } }
}
dependencies {
    testImplementation("junit:junit:4.13.2")
}
KTS
gradle wrapper > /dev/null
./gradlew -q :app:dependencies --configuration freeReleaseRuntimeClasspath | grep -E '^[+\\]|^ |No dependencies'
echo "---"
./gradlew -q :app:dependencies --configuration freeDebugUnitTestRuntimeClasspath | grep -E '^[+\\]|^ '
\--- org.jetbrains.kotlin:kotlin-stdlib:2.2.10
     \--- org.jetbrains:annotations:13.0
---
+--- project ':app' (*)
+--- junit:junit:4.13.2
\--- org.jetbrains.kotlin:kotlin-stdlib:2.2.10
     \--- org.jetbrains:annotations:13.0

A surprise: we declared no runtime dependency, yet kotlin-stdlib is on the release classpath. AGP 9 has Kotlin support built in and adds the standard library to every app module, Java-only or not. R8 removes the unused classes in release builds, but you'll still see some leftovers in Inspecting APKs. Older projects instead applied org.jetbrains.kotlin.android themselves; with AGP 9 that plugin is no longer needed, and the AGP 9 migration guide tells you to remove it.

Local unit tests

Tests in src/test/java run on your machine's JVM against a stubbed android.jar (every Android method throws "not mocked"), so they're fast and need no device. Keep business logic in plain Java/Kotlin classes and they're easy to test this way.

bash · try it yourself
mkdir -p hello/app/src/main/java/com/example/hello hello/app/src/test/java/com/example/hello && cd hello
cat > settings.gradle.kts <<'KTS'
pluginManagement { repositories { google(); mavenCentral(); gradlePluginPortal() } }
dependencyResolutionManagement { repositories { google(); mavenCentral() } }
rootProject.name = "hello"
include(":app")
KTS
echo 'plugins { id("com.android.application") version "9.4.1" apply false }' > build.gradle.kts
echo '<manifest xmlns:android="http://schemas.android.com/apk/res/android"><application android:label="Hello" /></manifest>' > app/src/main/AndroidManifest.xml
cat > app/src/main/java/com/example/hello/Price.java <<'JAVA'
package com.example.hello;

public final class Price {
    /** Price in cents after a percentage discount, rounded down. */
    public static int discounted(int cents, int percent) {
        if (percent < 0 || percent > 100) throw new IllegalArgumentException("percent " + percent);
        return cents * (100 - percent) / 100;
    }
}
JAVA
cat > app/src/test/java/com/example/hello/PriceTest.java <<'JAVA'
package com.example.hello;

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class PriceTest {
    @Test public void tenPercentOff() { assertEquals(900, Price.discounted(1000, 10)); }
    @Test public void roundsDown() { assertEquals(669, Price.discounted(999, 33)); }
    @Test(expected = IllegalArgumentException.class) public void rejectsBadPercent() { Price.discounted(100, 120); }
}
JAVA
cat > app/build.gradle.kts <<'KTS'
plugins { id("com.android.application") }
android {
    namespace = "com.example.hello"
    compileSdk = 36
    defaultConfig { applicationId = "com.example.hello"; minSdk = 24; targetSdk = 36; versionCode = 1; versionName = "1.0" }
    flavorDimensions += "tier"
    productFlavors { create("free") { dimension = "tier" }; create("paid") { dimension = "tier" } }
}
dependencies { testImplementation("junit:junit:4.13.2") }
KTS
gradle wrapper > /dev/null
./gradlew tasks --all | grep -oE '^app:test[A-Za-z]*UnitTest'
./gradlew test | grep -E 'UnitTest|^BUILD'
for f in app/build/test-results/*/TEST-*.xml; do
  echo "$f: $(grep -o 'tests="[0-9]*" skipped="[0-9]*" failures="[0-9]*"' $f)"
done
app:testFreeDebugUnitTest
app:testPaidDebugUnitTest
> Task :app:preFreeDebugUnitTestBuild UP-TO-DATE
> Task :app:compileFreeDebugUnitTestKotlin NO-SOURCE
> Task :app:javaPreCompileFreeDebugUnitTest
> Task :app:compileFreeDebugUnitTestJavaWithJavac
> Task :app:processFreeDebugUnitTestJavaRes NO-SOURCE
> Task :app:testFreeDebugUnitTest
> Task :app:prePaidDebugUnitTestBuild UP-TO-DATE
> Task :app:compilePaidDebugUnitTestKotlin NO-SOURCE
> Task :app:javaPreCompilePaidDebugUnitTest
> Task :app:compilePaidDebugUnitTestJavaWithJavac
> Task :app:processPaidDebugUnitTestJavaRes NO-SOURCE
> Task :app:testPaidDebugUnitTest
BUILD SUCCESSFUL in 7s
app/build/test-results/testFreeDebugUnitTest/TEST-com.example.hello.PriceTest.xml: tests="3" skipped="0" failures="0"
app/build/test-results/testPaidDebugUnitTest/TEST-com.example.hello.PriceTest.xml: tests="3" skipped="0" failures="0"

Notice what's not there: there is no testFreeReleaseUnitTest. With AGP 9, local unit tests are created only for the tested build type (debug unless you change testBuildType), which halves test time in projects that used to run every test twice. With flavors, though, test still runs once per flavor. On CI, run the one variant that matters for the job, e.g. ./gradlew testFreeDebugUnitTest, and let a nightly job run the full matrix.

The XML files in build/test-results/ are JUnit reports that every CI system understands (GitHub Actions test reporters, GitLab's junit artifacts, Jenkins); the HTML report is in build/reports/tests/.

When a test fails

bash · try it yourself
mkdir -p hello/app/src/main/java/com/example/hello hello/app/src/test/java/com/example/hello && cd hello
cat > settings.gradle.kts <<'KTS'
pluginManagement { repositories { google(); mavenCentral(); gradlePluginPortal() } }
dependencyResolutionManagement { repositories { google(); mavenCentral() } }
rootProject.name = "hello"
include(":app")
KTS
echo 'plugins { id("com.android.application") version "9.4.1" apply false }' > build.gradle.kts
echo '<manifest xmlns:android="http://schemas.android.com/apk/res/android"><application android:label="Hello" /></manifest>' > app/src/main/AndroidManifest.xml
cat > app/src/main/java/com/example/hello/Price.java <<'JAVA'
package com.example.hello;

public final class Price {
    public static int discounted(int cents, int percent) { return cents * (100 - percent) / 100; }
}
JAVA
cat > app/src/test/java/com/example/hello/PriceTest.java <<'JAVA'
package com.example.hello;

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class PriceTest {
    @Test public void roundsHalfUp() { assertEquals(500, Price.discounted(999, 50)); }
}
JAVA
cat > app/build.gradle.kts <<'KTS'
plugins { id("com.android.application") }
android {
    namespace = "com.example.hello"
    compileSdk = 36
    defaultConfig { applicationId = "com.example.hello"; minSdk = 24; targetSdk = 36; versionCode = 1; versionName = "1.0" }
}
dependencies { testImplementation("junit:junit:4.13.2") }
KTS
gradle wrapper > /dev/null
./gradlew testDebugUnitTest 2>&1 | grep -E 'PriceTest|AssertionError|tests completed|What went wrong|^BUILD' 
echo "exit code: ${PIPESTATUS[0]}"
PriceTest > roundsHalfUp FAILED
    java.lang.AssertionError at PriceTest.java:7
* What went wrong:
BUILD FAILED in 6s
exit code: 1

Gradle exits with a non-zero code, which is all a CI job needs to go red. --continue makes Gradle run the other variants' tests too before failing, so one run reports every broken variant.

Instrumented tests (non-run)

Tests in src/androidTest run on a device or emulator with ./gradlew connectedFreeDebugAndroidTest. On CI they need an emulator (GitHub's ubuntu-latest supports hardware acceleration for the Android emulator via KVM), Gradle Managed Devices (testOptions.managedDevices, which download a system image and run headless), or a device farm such as Firebase Test Lab. They're slower and flakier than unit tests, so most teams run them on merge or nightly rather than on every push.

Test yourself

Why does kotlin-stdlib appear on the runtime classpath of a Java-only app with AGP 9?

  1. junit depends on it
  2. AGP 9's built-in Kotlin support adds it to app modules
  3. Gradle needs it at runtime on the device
  4. It's a bug in the session

An app has flavors free and paid. Which unit-test tasks does AGP 9 create by default?

  1. testDebugUnitTest and testReleaseUnitTest
  2. testFreeDebugUnitTest and testPaidDebugUnitTest
  3. One task per variant, four in total
  4. Only test

Where are the JUnit XML reports that a CI test reporter reads?

  1. app/build/outputs/apk
  2. app/build/test-results/<task>/
  3. app/build/intermediates/merged_manifests
  4. ~/.gradle/caches