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
- Why it appears in AGP 9
- Fix 1: Update the responsible third-party plugin
- Fix 2: Migrate custom Gradle plugins to the public DSL
- Fix 3: Replace applicationVariants with androidComponents
- Temporary workaround: android.newDsl=false
- If the source of the cast is unclear
- How to verify the fix
- Important notes
- Summary
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
- Search the entire project for
BaseExtension. - Inspect
buildSrc,build-logic, and convention plugins. - Search for
applicationVariantsandlibraryVariants. - Use
--stacktraceto identify external plugin code. - 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
- Confirm that no custom code references
BaseExtension. - Confirm that third-party plugins support AGP 9.
- Run Gradle Sync in Android Studio.
- Run
./gradlew clean :app:assembleDebug. - Build Release, tests, and product flavors as well as Debug.
- 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
