[Solved] Unable to load class AndroidComponentsExtension after upgrading the Android Gradle Plugin 7.1

Android Habibur Rhoman Joy

I tried to update Android Studio Bumblebee and getting a build error - 


Unable to load class 'com.android.build.api.extension.AndroidComponentsExtension'.
This is an unexpected error. Please file a bug containing the idea.log file.


Also on the idea.log file getting this error -


A problem occurred evaluating project ':main'.
    at org.gradle.initialization.exception.DefaultExceptionAnalyser.transform(DefaultExceptionAnalyser.java:103)
    ...
Caused by: org.gradle.api.GradleScriptException: A problem occurred evaluating project ':main'.
    at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:93)
    ...
Caused by: java.lang.NoClassDefFoundError: com/android/build/api/extension/AndroidComponentsExtension
    at androidx.navigation.safeargs.gradle.SafeArgsPlugin.apply(SafeArgsPlugin.kt:73)
    at androidx.navigation.safeargs.gradle.SafeArgsPlugin.apply(SafeArgsPlugin.kt:42)
    ...
Caused by: java.lang.ClassNotFoundException: com.android.build.api.extension.AndroidComponentsExtension


Solution:

Updating Navigation Safe Args

These lines are the important ones to look at:

Caused by: java.lang.NoClassDefFoundError: com/android/build/api/extension/AndroidComponentsExtension
    at androidx.navigation.safeargs.gradle.SafeArgsPlugin.apply(SafeArgsPlugin.kt:73)
    at androidx.navigation.safeargs.gradle.SafeArgsPlugin.apply(SafeArgsPlugin.kt:42)

This indicates that the error is coming from the Navigation Safe Args plugin.

As per the Android Gradle Plugin 7.1.0 release notes:

AGP APIs that the Navigation Safe Args Gradle plugin depend on have been removed. AGP 7.1 does not work with Navigation Safe Args versions 2.4.0-rc1 or 2.4.0, but will work with versions 2.5.0-alpha01 and 2.4.1. In the meantime, as a workaround, you can use AGP 7.1 with a snapshot build of Navigation Safe Args, Navigation 2.5.0-SNAPSHOT. To use the snapshot build, follow the snapshot instructions with build id #8054565.

While Navigation 2.4.1 is not out yet, Navigation 2.5.0-alpha01 is available, which allows you to use Safe Args 2.5.0-alpha01 to fix this incompatibility:

dependencies {
    classpath 'com.android.tools.build:gradle:7.1.0'

    // Update this line to use 2.5.0-alpha01
    classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.5.0-alpha01"
}

While technically you should always use the same version of the Navigation library as the Safe Args plugin (i.e., your app should also use Navigation 2.5.0-alpha01, in this case, using a Navigation 2.4.0 stable and updating only the Safe Args Plugin to use 2.5.0-alpha01 is enough to fix the issue and let you continue to use AGP 7.1.0.

Note on Firebase Perf Plugin

Note that you might see this same error when you are using:

classpath "com.google.firebase:perf-plugin:1.4.0"

With an idea.log of that states:

Caused by: java.lang.NoClassDefFoundError: com/android/build/api/extension/AndroidComponentsExtension
    at com.google.firebase.perf.plugin.FirebasePerfClassVisitorFactory.registerForProject(FirebasePerfClassVisitorFactory.java:54)
    at com.google.firebase.perf.plugin.FirebasePerfPlugin.perform(FirebasePerfPlugin.java:145)
    at com.google.firebase.perf.plugin.FirebasePerfPlugin.lambda$apply$0(FirebasePerfPlugin.java:107)

As per the Firebase Perf Plugin 1.4.1 Release Notes:

Migrated away from the deprecated Android Gradle plugin APIs.

So you should upgrade to 1.4.1:

classpath "com.google.firebase:perf-plugin:1.4.1"

Thank you for reading the article. If you face any problem, please comment below.