升级 Android Gradle 插件 7.1 后无法加载 class AndroidComponentsExtension

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

我最近下载了 Android Studio Bumblebee,它询问我是否要升级到 Android Gradle 插件 7.1.0,这是 Android Studio Bumblebee 附带的版本.

升级后出现构建错误:

Unable to load class 'com.android.build.api.extension.AndroidComponentsExtension'.

This is an unexpected error. Please file a bug containing the idea.log file.

查看 idea.log 文件,我看到:

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

这意味着我无法 运行 我的应用程序,我需要降级 AGP 7.0 才能再次运行。我该如何解决这个问题并升级到 Android Gradle 插件 7.1.0?

正在更新导航安全参数

这些行是要看的重要行:

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)

这表明错误来自 Navigation Safe Args plugin

根据 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.

由于 Navigation 2.4.1 现已可用,您可以升级到该版本的导航以获得修复:

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"
}

请注意,您应始终使用与 Safe Args 插件相同版本的 Navigation 库(即,您的应用也应使用 Navigation 2.4.1):您不应尝试使用 Navigation 2.4.1+ Safe具有较早版本导航(例如 2.3.5)的 Args 插件。

关于 Firebase Perf 插件的说明

请注意,您在使用时可能会看到同样的错误:

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

有一个 idea.log 状态:

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[=13=](FirebasePerfPlugin.java:107)

根据 Firebase Perf Plugin 1.4.1 Release Notes:

Migrated away from the deprecated Android Gradle plugin APIs.

所以你应该升级到 1.4.1:

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

你可以做到这一点。非常适合我

// 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
}
I fixed the issue by downgrading the version of navigation to 2.3.5

    def nav_version = "2.3.5"

经过测试,使用导航2.4.0和AGP 7.0.4工作正常

就我而言,问题出在 hilt 版本上。我已经更新到最后一个 2.40.5 并且有效。

如果您的项目中没有 hilt 依赖项,找到在 gradle 同步期间抛出异常的库的最佳方法是检查 idea.log 文件.

找到类似的东西。

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

好好编码:)

更新: 该问题现已在 AGP 7.1.2.

中修复
  • 如果您的项目不依赖任何使用 multi-release JAR 的库,即 MRJARs.
  • ,Firebase 性能插件 1.4.1 将仅适用于 AGP 7.1
  • 使用 MRJAR 的流行依赖项是 moshi
  • 问题的根本原因在于Android Gradle Plugin
  • Firebase Perf 团队做出了以下 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.
  • 堆栈跟踪:
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[=10=](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[=10=](FirebasePerfPlugin.java:107)
    at org.gradle.api.internal.plugins.DefaultPluginManager.execute(DefaultPluginManager.java:258)
    at ...

将 Hilt 更新为“2.40.5”并将 firebase:perf-plugin 更新为“1.4.1”对我有用。

就我而言,我必须更新地图插件..

就我而言,我在 secrets-gradle-plugin 上遇到了问题。最初我使用版本 2.0.1(最新)。在我降级到 1.3.0 之后。它对我有用。

在项目的根 build.gradle 文件中:

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

在app-levelbuild.gradle文件中:

plugins {
    id 'com.android.application'
       id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
}