启用R8导致应用程序崩溃

问题描述

因此,我通过在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,并且该应用程序运行正常而不会崩溃。

有人遇到过同样的问题吗?

顺便说一句,我的项目在科特林。

解决方法

我建议您查看R8的输出,以查看是否有任何警告可能会指示您正确的方向。如果您能够始终如一地重现该问题,则可以向R8提出问题:https://issuetracker.google.com/issues/new?component=326788&template=1025938

,

问题是我们在某些模块中捆绑了android.jar,但它只能放在bootclasspath上,就像Android团队在我在IssueTracker上创建的issue中所说的那样。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...