PC パソコン

How to Fix “ApplicationExtensionImpl Cannot Be Cast to BaseExtension” in Android Studio

After upgrading to Android Gradle Plugin (AGP) 9.0 or later, Gradle Sync may fail with a ClassCastException stating that ApplicationExtensionImpl cannot be cast to BaseExtension.

AGP 9 switched the Android DSL to new public interfaces. The failure occurs when custom build logic or a third-party Gradle plugin still casts the Android extension to the deprecated com.android.build.gradle.BaseExtension.

Typical error

java.lang.ClassCastException:
class com.android.build.gradle.internal.dsl.ApplicationExtensionImpl
cannot be cast to class com.android.build.gradle.BaseExtension

The exception usually happens during Gradle’s configuration phase, before application sources are compiled.

Why it appears in AGP 9

In AGP 7.x and 8.x, Android DSL implementations supported both the legacy BaseExtension type and the newer public interfaces. Older plugins could therefore use code like this:

val android = extensions.getByType(
    com.android.build.gradle.BaseExtension::class.java
)

AGP 9 uses only the new public DSL interfaces and hides the concrete implementation types. Casting ApplicationExtensionImpl to the legacy BaseExtension therefore fails.

This is an API compatibility problem, not normally a corrupted cache. “Invalidate Caches” and gradlew clean alone cannot correct the incompatible cast.

Fix 1: Update the responsible third-party plugin

If the stack trace names a third-party plugin, upgrade it to a release that explicitly supports AGP 9. Plugins for static analysis, code generation, Flutter or React Native integrations, Firebase tooling, or output customization may access the Android DSL.

./gradlew :app:tasks --stacktrace
./gradlew :app:assembleDebug --stacktrace

Look for the first package in the stack trace that belongs to your build logic or an external plugin. The caller immediately before AGP internal classes is usually the most useful clue.

Fix 2: Migrate custom Gradle plugins to the public DSL

If a convention plugin or buildSrc code references BaseExtension, replace it with the public interface for the module type.

Application module

import com.android.build.api.dsl.ApplicationExtension

val android = extensions.getByType(
    ApplicationExtension::class.java
)

android.compileSdk = 37

Library module

import com.android.build.api.dsl.LibraryExtension

val android = extensions.getByType(
    LibraryExtension::class.java
)

android.compileSdk = 37

Do not cast to an internal implementation class. Use ApplicationExtension, LibraryExtension, or another public AGP interface appropriate for the applied Android plugin.

Fix 3: Replace applicationVariants with androidComponents

Build logic using applicationVariants, libraryVariants, testVariants, or related legacy APIs must move to the new Variant API.

androidComponents {
    onVariants(selector().all()) { variant ->
        println("Variant: " + variant.name)
    }
}

Replace the old variantFilter pattern with beforeVariants.

androidComponents {
    beforeVariants(
        selector().withBuildType("release")
    ) { variantBuilder ->
        variantBuilder.enable = false
    }
}

The new API has different configuration timing, so review the official AGP recipes instead of treating migration as a simple type rename.

Temporary workaround: android.newDsl=false

If a required plugin has not yet migrated, AGP 9 can temporarily restore the legacy DSL implementation.

# gradle.properties
android.newDsl=false

This is a migration bridge. Google states that the opt-out is removed in AGP 10.0. Update the plugin or migrate custom build logic, then remove the flag.

android.builtInKotlin=false is a separate compatibility setting. Do not disable both features unless the plugin actually requires both legacy behaviors.

If the source of the cast is unclear

  1. Search the entire project for BaseExtension.
  2. Inspect buildSrc, build-logic, and convention plugins.
  3. Search for applicationVariants and libraryVariants.
  4. Use --stacktrace to identify external plugin code.
  5. Temporarily remove plugins one at a time to isolate the caller.
./gradlew help --stacktrace
./gradlew :app:assembleDebug -Pandroid.debug.obsoleteApi=true

How to verify the fix

  1. Confirm that no custom code references BaseExtension.
  2. Confirm that third-party plugins support AGP 9.
  3. Run Gradle Sync in Android Studio.
  4. Run ./gradlew clean :app:assembleDebug.
  5. Build Release, tests, and product flavors as well as Debug.
  6. Verify that the project builds without android.newDsl=false.

Important notes

Do not reference ApplicationExtensionImpl directly. It is an internal implementation, not a stable public API, and can change between AGP releases.

Rolling back to AGP 8 can unblock a build temporarily, but it does not migrate the incompatible plugin. Plan the update using the compatibility requirements for AGP, Gradle, and JDK.

Summary

“ApplicationExtensionImpl cannot be cast to BaseExtension” means AGP 9’s new DSL object is being cast to a removed legacy type. Update the third-party plugin or migrate custom logic to ApplicationExtension, LibraryExtension, and androidComponents. Use android.newDsl=false only as a temporary bridge.

References: Android Gradle Plugin 9.0 release notes / Write Gradle plugins / Android Gradle Plugin Recipes