Android APK 在启动时崩溃并出现“非法类访问”错误

Coo*_*pps 9 android gradle kotlin

所以我最近更新到 Android Studio 4.1 并将我的项目更新到 Android 10。从那时起,调试应用程序构建工作正常,但每当我在 Android 设备上启动构建的 APK 时,我都会收到以下错误:

FATAL EXCEPTION: main
Process: com.cookiegames.smartcookie, PID: 12800
java.lang.IllegalAccessError: Illegal class access: 'androidx.appcompat.widget.ContentFrameLayout' attempting to access 'androidx.appcompat.app.w' (declaration of 'androidx.appcompat.widget.ContentFrameLayout' appears in base.apk)
    at androidx.appcompat.widget.ContentFrameLayout.onAttachedToWindow(Unknown Source:7)
    at android.view.View.dispatchAttachedToWindow(View.java:18347)
... and so on
Run Code Online (Sandbox Code Playgroud)

我已经回滚到 Android Studio 4.0 并将我的项目移回 SDK 29,但错误仍然存​​在。我不知道接下来要尝试什么。

Hir*_*nam 8

更新:

@headsvk 答案现在是正确的。Android studio 没有自动提示我更新 gradle,所以我必须手动完成。您必须使用 gradle 6.5.1(在gradle-wrapper.properties文件中更改它),然后将com.android.tools.build:gradle版本更改为4.1.1build.gradle在项目根目录中的文件中)。清理并重建项目,它应该被修复。

旧答案:

我找到了一种临时方法来为自己解决这个问题。我意识到禁用 proguard 可以解决问题。所以我只是添加了一条规则来告诉 proguard 继续androidx.appcompat上课。

为此,只需将此行代码添加到proguard-rules.pro文件中:

-keep class androidx.appcompat.** { *; }
Run Code Online (Sandbox Code Playgroud)

  • 哇??随着每次增量的增加,AS 更新变得越来越糟糕。谢谢大佬的解答 (3认同)