PC パソコン

Android Studio: Fix “Class … requires its nest host … to be on program or class path.” in D8

An Android build that targets Java 11 can fail in D8 with Class ... requires its nest host ... to be on program or class path. This is not always an ordinary missing-dependency problem. In Google Issue Tracker issue #490426424, Google attributes the failure to annotation extraction performed by the Android Gradle Plugin (AGP): the extraction step can separate classes that belong to the same Java 11 nest, leaving D8 unable to see a complete nest. The official fix is included in AGP 9.2.0.

The error

Execution failed for task ':app:mergeExtDexDebug'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.DexMergingTaskDelegate
  > There was a failure while executing work items
    > A failure occurred while executing com.android.build.gradle.internal.dexing.DexWorkAction
      > Failed to process: .../classes.jar

Class com.example.Outer$Nested requires its nest host
com.example.Outer to be on program or class path.

The class names, Gradle task, and input JAR vary by project. Another form is Compilation of classes ... requires its nest mates ... to be on program or class path. The useful search phrases are requires its nest host and requires its nest mates.

Why it happens

Java 11 class files can describe an outer class and its nested classes as a nest host and nest members. When D8 desugars that bytecode, it needs all classes in the nest to be available on the program path or class path.

For issue #490426424, Google states that AGP’s annotation extraction causes the problem. If that processing filters classes, D8 may receive a nest member without its host, or a host without every required member. D8 then rejects the incomplete nest. This is why blindly adding another dependency for the class named in the message may not solve this particular regression.

Affected and fixed versions: The failure can occur with pre-fix AGP versions when Java 11 class files are processed. Google lists issue #490426424 as fixed in AGP 9.2.0-alpha05, shipped with Android Studio Panda 4 Canary 1, and also lists it in the stable AGP 9.2.0 fixed-issues section. Treat AGP 9.2.0 or newer as the officially fixed line.

Recommended fix

  1. Check the active AGP version. Inspect the root build.gradle(.kts) or version catalog for the com.android.application and com.android.library plugin versions.
  2. Upgrade to AGP 9.2.0 or newer. Check the official release notes for the matching Gradle and JDK compatibility requirements before changing the wrapper or build JDK.
  3. Sync and rebuild from clean inputs. Stop the Gradle daemon and rerun the failing variant so stale transformed artifacts do not hide the result.

Example using the plugins DSL

plugins {
    id("com.android.application") version "9.2.0" apply false
    id("com.android.library") version "9.2.0" apply false
}

Verification build

./gradlew --stop
./gradlew clean
./gradlew :app:assembleDebug --stacktrace

Temporary alternatives when you cannot upgrade

  • Temporarily target Java 8 bytecode: If the project and all dependencies permit it, setting sourceCompatibility and targetCompatibility to JavaVersion.VERSION_1_8 may avoid Java 11 nestmate attributes. This changes available language features, APIs, and library compatibility, so it is not a preferred permanent fix.
  • Inspect custom bytecode processing: Make sure custom Gradle tasks, JAR filters, or instrumentation do not remove only an inner class or only its outer class. Keep the complete nest on the same program input or class path.
  • Use the last known working AGP temporarily: A rollback may unblock a build, but the official fix is in 9.2.0, so plan to upgrade rather than freeze the toolchain indefinitely.

Do not silence the diagnostic or add an empty placeholder for the reported nest host. D8 needs the complete nest to transform nest-based private access correctly. Forcing an incomplete input through the build can produce incorrect runtime behavior.

How to verify the fix

  1. Run ./gradlew :app:assembleDebug --stacktrace and confirm that neither requires its nest host nor requires its nest mates appears.
  2. Use ./gradlew buildEnvironment or Android Studio’s Build Analyzer to confirm that the build actually loaded AGP 9.2.0 or newer.
  3. Build the release and test variants that process the same Java 11 library, not only the debug variant.
  4. If you temporarily moved to Java 8, test every feature and dependency that previously required Java 11.

Cautions

  • The Java version in compileOptions, the JDK that runs Gradle, and a dependency’s class-file version are separate. Record exactly which one you changed.
  • The same D8 diagnostic can be caused by custom JAR processing or a genuinely missing dependency. If it remains on AGP 9.2.0 or newer, inspect whether both the reported host and members are present in the build input.
  • Deleting caches alone does not repair the cause. Clean only after upgrading AGP or restoring the missing nest classes, to eliminate stale transformed outputs.

Summary

Class ... requires its nest host ... to be on program or class path. means D8 cannot see the complete Java 11 nest that it must desugar. In issue #490426424, the cause was AGP annotation extraction, and Google fixed it in AGP 9.2.0. Upgrade first, then run a clean build. Java 8 bytecode or preserving the entire nest can serve as temporary workarounds only after their compatibility impact has been tested.

Official references