【问题标题】:Enabling R8 crashes application启用 R8 会导致应用程序崩溃
【发布时间】:2020-09-04 12:56:09
【问题描述】:

所以我通过在 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 无法静态确定的使用。您遇到的显式 throw null 可能是由于使用了 R8 确定从未实例化的类。在这种情况下,R8 会将对该类实例成员的任何访问重写为throw null

标签: android kotlin proguard obfuscation android-r8


【解决方案1】:

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

【讨论】:

  • 感谢您的建议@Matt。已经打印了用法和种子(-printusage ../usage.txt 和 -printseeds ../seeds.txt)但遗憾的是找不到问题。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 2016-06-04
    相关资源
    最近更新 更多