因此,我通过在gradle.properties中添加android.enableR8=true
在项目中启用了R8。
这是我的gradle文件:
buildTypes {
release {
useProguard false
minifyEnabled true
debuggable false
multiDexEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
...
但是,当我运行构建时,在应用启动时会发生以下崩溃:java.lang.RuntimeException: Unable to instantiate application com.example.application.MyApplication: java.lang.NullPointerException: throw with null exception
at android.app.LoadedApk.makeApplication(LoadedApk.java:1069)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5842)
at android.app.ActivityThread.access$1100(ActivityThread.java:199)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: throw with null exception
at com.example.application.MyApplication.<init>(MyApplication.kt:18)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateApplication(AppComponentFactory.java:50)
at androidx.core.app.CoreComponentFactory.instantiateApplication(CoreComponentFactory.java:52)
at android.app.Instrumentation.newApplication(Instrumentation.java:1120)
at android.app.LoadedApk.makeApplication(LoadedApk.java:1061)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5842)
at android.app.ActivityThread.access$1100(ActivityThread.java:199)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
我在Proguard文件中添加了这些内容,因此R8不会删除或混淆任何内容-dontobfuscate
-dontshrink
-dontoptimize
并且该应用仍然崩溃。我尝试通过在Gradle文件中将
android.enableR8=false
设置为gradle.properties和useProguard true
来迁移到Proguard,该应用程序正常运行而不会崩溃。有人遇到过同样的问题吗?
顺便说一句,我的项目在 Kotlin 。
最佳答案
我建议您查看R8的输出,以查看是否有任何警告可能将您指示正确的方向。如果您能够始终如一地重现该问题,则可以向R8提交一个问题:https://issuetracker.google.com/issues/new?component=326788&template=1025938。
关于android - 启用R8导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63741654/