ChatGPT解决这个技术问题 Extra ChatGPT

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

我最近下载了 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 Plugin 7.1.0?


i
ianhanniballake

更新导航安全参数

这些行是要查看的重要行:

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

Navigation Safe Args Gradle 插件所依赖的 AGP API 已被删除。 AGP 7.1 不适用于 Navigation Safe Args 版本 2.4.0-rc1 或 2.4.0,但适用于版本 2.5.0-alpha01 和 2.4.1。同时,作为一种解决方法,您可以将 AGP 7.1 与 Navigation Safe Args、Navigation 2.5.0-SNAPSHOT 的快照构建一起使用。要使用快照构建,请按照构建 ID #8054565 的快照说明进行操作。

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

从 Navigation 2.5.0-alpha01 向后移植:Safe Args 现在依赖于 Android Gradle 插件版本 7.0.4。这意味着 Navigation Safe Args 将不再与 Android Studio 7.0 之前的版本兼容,但现在与 Android Gradle Plugin 7.1.0 及更高版本兼容。

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 Args 插件与早期版本一起使用Navigation 的版本(例如 2.3.5)。

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$0(FirebasePerfPlugin.java:107)

根据 Firebase Perf Plugin 1.4.1 Release Notes

从已弃用的 Android Gradle 插件 API 中迁移出来。

所以你应该升级到 1.4.1:

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

是的,但是 firebase perf 插件 1.4.* 与最新的 moshi 不兼容。该错误是已知的,修复将在 Chipmunk 中进行。在 Android Gradle Plugin 7.1.0 之前,1.3.5 运行良好。所以,现在,在最新的“稳定”AS 中使用带有 firebase perf 插件的 moshi 是不可能的。有那么厉害吗? github.com/square/moshi/issues/1463
@Jakoss - FWIW,this comment on the Firebase issue 似乎暗示 Firebase Perf 1.3.2 确实可与 Moshi 和 AGP 7.1 一起使用。可能值得一试。
是的,它确实可以工作,这有点让人松了一口气,但是我们必须等待花栗鼠与最新的 firebase 性能插件一起工作仍然很奇怪。对于维护更大应用程序的团队来说,使用不稳定的通道不是一种选择
用 2.5.0-alpha01 替换 2.4.0 对我有用。谢谢
对于刀柄问题,应用 hilt_version = '2.40.5'
D
David Miguel

更新:该问题现已在 AGP 7.1.2 中得到解决。

Firebase perf 插件 1.4.1 仅适用于 AGP 7.1,前提是您的项目不依赖于任何使用多版本 JAR(即 MRJAR)的库。

使用 MRJAR 的流行依赖项是 moshi。

问题的根本原因在于 Android Gradle 插件。

Firebase Perf 团队发表了以下声明:

以下是一些更新: AGP v7.2.0-beta01 于上周发布,将与最新的 perf 插件 v1.4.1 一起使用。 AGP v7.2 RC1 计划于 3 月 1 日发布,AGP v7.2 最终版计划于 3 月 9 日发布。同时,这里有一些值得尝试的解决方法:使用 perf plugin v1.3.5 和 AGP v6.8.3。 Perf 插件 v1.3.5 及以下不兼容 AGP v7.0.0+;为了使 perf 插件与 AGP v7.0.0+ 兼容,我们必须使用新的转换 API,它引入了构建失败,即使 perf 插件从检测中过滤掉 META-INF/ 目录中的类,AGP仍然尝试访问班级以收集有关它的信息。但是,AGP 正在为此进行更永久的修复,我们将可以选择完全过滤掉失败的类。等到 3 月 9 日集成最新的 perf 插件版本和 AGP。试用带有最新 perf 插件版本的 AGP 测试版,并在 3 月 9 日之后转移到生产版本。

堆栈跟踪:

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

将 Firebase 性能插件更新到 1.4.1 对我有用。
只有当您的项目不依赖于任何使用 MRJAR 的库时,它才会起作用。我已经用更多细节更新了答案。
Y
Yuri Misyac

就我而言,问题出在 hilt 版本上。我已更新到最后一个 2.40.5 并且可以正常工作。

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

https://i.stack.imgur.com/weyPX.png

并找到类似的东西。

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

有一个好的编码:)


在我的情况下,我必须同时升级刀柄和导航组件版本
N
Nafis Kabbo

你可以做这件事。完美地为我工作

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

您必须使用之前在 app 文件夹的 build.gradle 中使用的插件 ID(不是项目 build.gradle 之一)
你从哪里得到这些版本号?
@BitwiseDEVS 版本号与项目的 build.gradle 依赖块的先前版本相同
M
Mohammed Al-Hafidh

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


M
Magesh Pandian

就我而言,我在 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'
}

在应用级 build.gradle 文件中:

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

A
Alonso
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 工作正常


虽然这解决了同步问题,但由于不推荐使用 API 导致其他警告 - 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. - 我特别喜欢该消息的“查看待定”部分,表明这可能是危险区域,也可能在任何时候中断。
M
Muhaiminur Rahman

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

https://i.stack.imgur.com/XbNrk.png