【发布时间】: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