PC パソコン

How to Fix “IllegalStateException: targetCompatibility is not yet finalized” in Android Studio

An Android Studio build that accesses Android’s bootClasspath from a custom Gradle plugin or buildSrc can fail during configuration with “IllegalStateException: targetCompatibility is not yet finalized.” Google tracked this as Issue #461382865 and lists it as fixed in Android Gradle Plugin (AGP) 9.2.0.

Representative error log

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':app'.
> java.lang.IllegalStateException:
  targetCompatibility is not yet finalized

The stack trace may point to a custom plugin, buildSrc, a convention plugin, or code that reads bootClasspath. Exact class names and callers vary by project.

Cause

AGP finalizes configuration in stages: DSL parsing, finalizeDsl(), variant creation, and task creation. Before the fix, merely accessing bootClasspath through a particular path could require targetCompatibility before that value had been finalized. Google’s release note describes the defect as an error when “accessing (but not evaluating) bootClasspath.”

Version note: AGP 9.2.0 is the officially confirmed fixed version. The public source does not identify the first affected AGP release, so it would be inaccurate to state that every earlier version is affected.

When it occurs

  • The build uses an AGP release that does not contain the fix.
  • A custom plugin, buildSrc, or convention plugin accesses Android’s bootClasspath during configuration.
  • Even code intended only to retain a provider enters an AGP path that reads or finalizes the value too early.

The caller can also be a third-party plugin, especially in multi-module projects with shared build logic.

Recommended fix

  1. Identify the caller from a full stack trace.
    ./gradlew assembleDebug --stacktrace

    Determine whether the access originates in buildSrc, an internal plugin, or an external plugin.

  2. Upgrade to AGP 9.2.0 or later. For AGP 9.2, follow the official compatibility table: use Gradle 9.4.1 or newer and run Gradle on JDK 17.
  3. Make custom build logic lazy. Do not call get() or convert the value to an eager file collection during configuration. Wire providers to task inputs through Gradle’s Provider API and AGP’s public Variant API.
  4. Stop old daemons and rebuild.
    ./gradlew --stop
    ./gradlew clean
    ./gradlew assembleDebug --stacktrace

Example of build logic to review

Avoid eagerly materializing the classpath during configuration:

// Avoid: resolves the value during configuration
val files = android.bootClasspath.files
tasks.register("inspectBootClasspath") {
    doLast { println(files) }
}

When the AGP API in use exposes a Provider, retain it and connect it directly to a task input so resolution occurs later. The precise types differ between AGP releases, so implement this against the public Variant API reference for your selected version.

Alternatives when you cannot upgrade

If an AGP upgrade is temporarily blocked, update the plugin that performs the access or defer its bootClasspath read until task execution. If a third-party feature is the caller, disabling that feature temporarily may unblock the build. These are workarounds; the officially confirmed product fix is AGP 9.2.0.

A blanket afterEvaluate workaround can harm configuration-cache compatibility and may break again with a later AGP release. Google recommends public Variant API extension points instead of direct dependencies on AGP’s internal tasks.

How to verify the fix

  • ./gradlew assembleDebug --stacktrace completes without the exception.
  • The Release build and the affected custom task also complete.
  • ./gradlew --version shows a Gradle/JDK combination compatible with AGP 9.2.
  • If configuration cache is enabled, two consecutive builds complete without the failure returning.

Cautions

Adding matching sourceCompatibility and targetCompatibility values is not a reliable fix when the actual cause is premature API access. Use the stack trace to find the caller, upgrade AGP, and make the build logic lazy.

Summary

“targetCompatibility is not yet finalized” is a known AGP failure associated with accessing bootClasspath before configuration has been finalized. Upgrade to AGP 9.2.0 or later, and use providers plus the public Variant API in custom build logic to delay value resolution.

Official references