Unable to load class AndroidComponentsExtension after upgrading the Android Gradle Plugin 7.1
L

12

150

I recently downloaded Android Studio Bumblebee and it helpfully asked whether I wanted to upgrade to Android Gradle Plugin 7.1.0, the version that shipped alongside Android Studio Bumblebee.

After upgrading, I get 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.

And looking at the idea.log file, I see:

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

Which means I can't run my app and I need to downgrade the AGP 7.0 to get things working again. How do I fix this and upgrade to Android Gradle Plugin 7.1.0?

Libreville answered 26/1, 2022 at 0:45 Comment(0)
L
185

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.

As Navigation 2.4.1 is now available, you can upgrade to that version of Navigation to gain the fix:

Backported from Navigation 2.5.0-alpha01: Safe Args now depends on Android Gradle Plugin version 7.0.4. This means that Navigation Safe Args will no longer be compatible with Android Studio versions prior to 7.0, but is now compatible with Android Gradle Plugin 7.1.0 and higher.

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

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

Note that 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.4.1): you should not try to use the Navigation 2.4.1+ Safe Args plugin with an earlier version of Navigation (such as 2.3.5).

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"
Libreville answered 26/1, 2022 at 0:45 Comment(6)
yea, but the firebase perf plugin 1.4.* is not compatible with newest moshi. The bug is known, fix will be in Chipmunk. 1.3.5 worked fine until the Android Gradle Plugin 7.1.0. So, right now, it's impossible to use moshi with firebase perf plugin in newest "stable" AS. It's that great? github.com/square/moshi/issues/1463Mucker
@Mucker - FWIW, this comment on the Firebase issue seems to imply that Firebase Perf 1.3.2 does work with both Moshi and AGP 7.1. Might be worth trying out.Libreville
Yes, it does work which is a little bit of a relief, but it's still very weird that we have to wait for chipmunk to work with newest firebase performance plugin. Using unstable channel is not an option for teams maintaining bigger appsMucker
replacing 2.4.0 with 2.5.0-alpha01 worked for me. Thank youFreckly
For hilt issue, apply hilt_version = '2.40.5'Arlenarlena
in my case bumping up hilt and firebase perf plugins version fixed the problemHuskey
O
17

In my case, the problem was with hilt version. I've updated to the last 2.40.5 and it's work.

If you don't have hilt dependency in your project the best way to find a library that throws the exception during gradle sync it's to check idea.log file.

android studio hint

And find something like that.

Caused by: java.lang.NoClassDefFoundError: com/android/build/api/extension/AndroidComponentsExtension
    at dagger.hilt.android.plugin.HiltGradlePlugin.configureBytecodeTransformASM(HiltGradlePlugin.kt:257)

Have a good coding :)

Oria answered 29/1, 2022 at 7:50 Comment(1)
In my case when, I had to upgrade both of hilt and navigation componnet versionSatiety
S
13

Update: the issue is now fixed in AGP 7.1.2.


  • Firebase perf plugin 1.4.1 will only work with AGP 7.1 if your project doesn't depend on any library that uses multi-release JARs, aka MRJARs.
  • A popular dependency that uses MRJARs is moshi.
  • The root cause of the issue lies in the Android Gradle Plugin.
  • The Firebase Perf team made the following statement:

Here are some updates:

  1. AGP v7.2.0-beta01 was made available last week and will work with the latest perf plugin v1.4.1.
  2. AGP v7.2 RC1 is scheduled to release on the 1st of March and AGP v7.2 final is scheduled on the 9th of March.

In the meantime, here are some workarounds worth trying out:

  1. Use perf plugin v1.3.5 with AGP v6.8.3. Perf plugin v1.3.5 and below is not compatible with AGP v7.0.0+; in order for the perf plugin to be compatible with AGP v7.0.0+, we have to use the new transform API, which introduces the build failure where even if the perf plugin filters out classes in the META-INF/ directory from instrumentation, AGP still tries to visit the class to collect information about it. However, AGP is working on a more permanent fix for this where we will have an option to filter out failed classes completely.
  2. Wait until March 9 to integrate the latest perf plugin version and AGP.
  3. Try a beta version of AGP with the latest perf plugin version and move to the production version after March 9th.
  • Stack trace:
org.gradle.api.GradleScriptException: A problem occurred evaluating project ':app'.
    at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:93)
    at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl.lambda$apply$0(DefaultScriptPluginFactory.java:133)
    at ...
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)
    at org.gradle.api.internal.plugins.DefaultPluginManager$2.execute(DefaultPluginManager.java:258)
    at ...
Scyros answered 1/2, 2022 at 6:52 Comment(2)
updating Firebase Performance plugin to 1.4.1 worked for me.Bhagavadgita
It will work only if your project doesn't depend on any library that uses MRJARs. I've updated the answer with more details about it.Scyros
C
6

You can do this thing. Perfectly working for me

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.1.0' apply false
    id 'com.android.library' version '7.1.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
    id "com.google.dagger.hilt.android" version '2.41' apply false
    id 'androidx.navigation.safeargs.kotlin' version '2.4.0' apply false // use this one
//    classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.4.0" (will not work now)

}

task clean(type: Delete) {
    delete rootProject.buildDir
}
Chemo answered 27/1, 2022 at 2:14 Comment(3)
You have to use the Plugin Id that you were previously using in the build.gradle of app folder (not the project build.gradle one)Chemo
Where did you get these version number?Linskey
@BitwiseDEVS The version numbers are same like previous versions of project's build.gradle dependency blockChemo
A
3

Upgrade your gradle version, it will fix the issue

    classpath 'com.android.tools.build:gradle:7.4.2'

Gradle wrapper properties version

   distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
Agrestic answered 3/1 at 16:21 Comment(0)
A
2

Updating Hilt to '2.40.5' and firebase:perf-plugin to '1.4.1' worked for me.

Arguable answered 4/2, 2022 at 2:4 Comment(0)
W
2

In my case I faced issue on secrets-gradle-plugin. Initially I used version 2.0.1(latest). After I downgrade into 1.3.0. It worked for me.

In project's root build.gradle file:

dependencies {
        classpath 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:1.3.0'
}

In app-level build.gradle file:

plugins {
    id 'com.android.application'
       id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
}
Weakkneed answered 4/5, 2022 at 6:38 Comment(0)
B
0
I fixed the issue by downgrading the version of navigation to 2.3.5

    def nav_version = "2.3.5"

after testing, use navigation 2.4.0 and AGP 7.0.4 work fine

Briolette answered 27/1, 2022 at 3:4 Comment(1)
While this fixed the sync issue, this leads to other warnings due to deprecated API usages - API 'BaseVariant.getApplicationIdTextResource' is obsolete and has been replaced with 'VariantProperties.applicationId'. It will be removed in version 7.0 of the Android Gradle plugin. For more information, see TBD. - I particularly like the 'see TBD' part of that message, indicating that this is likely dangerous territory that could also break at any point.Libreville
L
0

In my case i had to update Maps plugin..

Updated Maps Plugin

Lyndseylyndsie answered 12/4, 2022 at 5:50 Comment(0)
P
0

Upgrade your navigation safeargs version to latest and everything will be fine. It worked for me. Here is [the link ] (https://developer.android.com/jetpack/androidx/releases/navigation "Navigation safeargs google documentation")! to get latest safeargs version.

Puppetry answered 21/9, 2022 at 21:51 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewInna
C
0

I had this exact same problem implementing Hilt in my project. I found out it was a plugin compatibility issue, just updating to the latest version fixed the issue.

Strangely, the IDE didn't signal that there was a newer version available, so I had to search manually.

in the project's build.gradle:

I changed this line:

classpath "com.google.dagger:hilt-android-gradle-plugin:2.35"

for this one:

classpath "com.google.dagger:hilt-android-gradle-plugin:2.45"

problem solved.

full file:

buildscript {
repositories {
    google()
    mavenCentral()
}
dependencies {
    classpath "androidx.navigation:navigation-safe-args-gradleplugin:2.5.3"
    classpath "com.google.dagger:hilt-android-gradle-plugin:2.45"
   }
}

plugins {
id 'com.android.application' version '7.4.1' apply false
id 'com.android.library' version '7.4.1' apply false
id 'org.jetbrains.kotlin.android' version '1.7.21' apply false

}
Creeps answered 26/2, 2023 at 12:22 Comment(0)
S
0

Go to build.gradle(project) and add the following dependency:

classpath 'com.android.tools.build:gradle:7.1.3'
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.7.6"

Now try again. Done

Stephens answered 2/1 at 18:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.