PC パソコン

Android Studio: Fix “task ‘testReleaseUnitTest’ not found in project ‘:app’” in AGP 9

After upgrading to Android Gradle Plugin (AGP) 9, a release unit-test command may fail because Gradle can no longer find testReleaseUnitTest. This usually is not a test-discovery problem or a corrupted Gradle cache. It is a consequence of AGP 9.0 changing which unit-test components are created by default.

Representative error

FAILURE: Build failed with an exception.

* What went wrong:
Cannot locate tasks that match ':app:testReleaseUnitTest'
as task 'testReleaseUnitTest' not found in project ':app'.

The module path may differ from :app:. Projects with product flavors can encounter the same failure with a task such as testPaidReleaseUnitTest.

Cause

AGP 9.0 changed the default value of android.onlyEnableUnitTestForTheTestedBuildType from false to true. According to the official release notes, AGP now creates unit-test components only for the tested build type. In a default project, that normally means one debug unit-test component. A CI command inherited from AGP 8.x can therefore request testReleaseUnitTest even though AGP 9 never created that task.

Important: This is intended AGP 9 behavior. The test count has not fallen to zero; the requested task is absent from the Gradle model. Running clean, deleting caches, or renaming test classes will not create it.

Affected configurations

  • The project has been upgraded to Android Gradle Plugin 9.0 or later.
  • The application module defines multiple build types, such as debug and release.
  • CI, a shell script, or an Android Studio run configuration directly invokes a unit-test task for a non-tested build type.
  • The workflow assumes the AGP 8.x behavior in which unit-test components were created for more than one build type.

Recommended fix

1. Inspect the tasks AGP actually created

./gradlew :app:tasks --all

Look for an existing task such as testDebugUnitTest. On macOS or Linux, you can narrow the output with:

./gradlew :app:tasks --all | grep UnitTest

On Windows, use findstr UnitTest instead of grep.

2. If debug coverage is sufficient, update CI to use the existing task

./gradlew :app:testDebugUnitTest

This keeps the AGP 9 default and avoids creating components that the project does not need.

3. If both debug and release unit tests are required, restore the previous behavior explicitly

Add the following line to the project’s gradle.properties file:

android.onlyEnableUnitTestForTheTestedBuildType=false

Sync Gradle, verify that the release task is present, and then run:

./gradlew :app:testReleaseUnitTest

Alternatives

  • Run unit tests only for the build type that is meaningful to the project and pin CI to that existing task.
  • For multi-variant pipelines, check the task model during AGP migrations instead of assuming every historical task still exists.
  • If the goal is to validate release-only shrinking, signing, or packaged behavior, complement local unit tests with an appropriate integration or device test.

How to verify the fix

  1. Confirm that the desired task appears in ./gradlew :app:tasks --all.
  2. Run ./gradlew :app:testReleaseUnitTest --info.
  3. Verify a zero Gradle exit code and updated XML or HTML test reports.
  4. Repeat the run in CI with the same Gradle Wrapper and AGP version.

Cautions

  • Do not create an empty Gradle task merely to satisfy the old task name. That can make CI pass without running any tests.
  • Setting android.onlyEnableUnitTestForTheTestedBuildType=false creates additional unit-test components and may increase configuration time and memory use.
  • This is an AGP 9 behavior change, not a version-specific defect with a documented fixed release. Choose the required test scope and update either CI or the Gradle property.

Summary

task 'testReleaseUnitTest' not found appears because AGP 9 no longer creates unit-test components for every build type by default. Use testDebugUnitTest when debug testing is sufficient. If the pipeline genuinely requires both debug and release unit tests, set android.onlyEnableUnitTestForTheTestedBuildType=false. Inspecting the task list first avoids wasting time on test discovery and cache troubleshooting.

Official references