Android R8 即时崩溃
Posted
技术标签:
【中文标题】Android R8 即时崩溃【英文标题】:Android R8 Instant Crash 【发布时间】:2021-08-21 13:50:44 【问题描述】:我正在尝试使用 R8 完整模式 构建我的应用程序的发布版本,但它会导致崩溃。更具体地说,当通过 android Studio 安装时,构建将完全正常运行,或者它会立即崩溃并添加下面的堆栈跟踪。
有时它会在配置更改后立即发生,例如 debuggable false
到 debuggable true
,有时是在连续构建相同的 gradle 配置之后,这很奇怪,我无法确定它的行为。
release
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
minifyEnabled true
debuggable false
shrinkResources true
Proguard 文件:
-keepclassmembers class fqcn.of.javascript.interface.for.webview
public *;
## kotlin
-dontwarn org.jetbrains.annotations.**
-keep class kotlin.Metadata *;
-dontwarn kotlin.reflect.jvm.internal.**
-keep class kotlin.reflect.jvm.internal.* *;
-keepclassmembers class kotlin.Metadata
public <methods>;
-keepnames @dagger.hilt.android.lifecycle.HiltViewModel class * extends androidx.lifecycle.ViewModel
应用程序是用
构建的 Android Studio 4.2.1 AGP 版本 4.2.1 Gradle 版本 gradle-6.7.1-all由于包含 Jetpack Compose,我们还添加了:
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all
kotlinOptions
jvmTarget = "1.8"
freeCompilerArgs += [
"-Xallow-jvm-ir-dependencies",
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true"
]
compileOptions
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
此外,还包括multiDexEnabled true
和implementation "org.jetbrains.kotlin:kotlin-reflect:1.5.10"
,这将是一个可能的解决方案。
崩溃 1:
E/zd3: SafeAtomicHelper is broken!
java.lang.ClassCastException
at java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl.<init>(AtomicReferenceFieldUpdater.java:352)
at java.util.concurrent.atomic.AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdater.java:109)
at zd3.<clinit>(Unknown Source:96)
at be3.t(Unknown Source:46)
at d93.<init>(Unknown Source:41)
at ad3.<init>(Unknown Source:77)
at i93.a(Unknown Source:186)
at r93.c(Unknown Source:410)
at qg8.<init>(Unknown Source:247)
at og8.b(Unknown Source:108)
at MyAppApplication.c(Unknown Source:74)
at MyAppApplication.onCreate(Unknown Source:44)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1190)
at android.app.ActivityThread.handleMakeApplication(ActivityThread.java:7184)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7134)
at android.app.ActivityThread.access$1500(ActivityThread.java:274)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2102)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8167)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
崩溃 2:
Fatal Exception: java.lang.ExceptionInInitializerError
at kotlin.LazyKt__LazyJVMKt.lazy(LazyKt__LazyJVMKt.java:34)
at kotlin.reflect.jvm.internal.KProperty1Impl.<init>(KProperty1Impl.java:41)
at kotlin.reflect.jvm.internal.ReflectionFactoryImpl.property1(ReflectionFactoryImpl.java:18)
at kotlin.jvm.internal.Reflection.property1(Reflection.java:2)
at kotlin.reflect.jvm.internal.KDeclarationContainerImpl$Data.<clinit>(KDeclarationContainerImpl.java:18)
at kotlin.reflect.jvm.internal.KClassImpl$data$1.invoke(KClassImpl.java)
at kotlin.reflect.jvm.internal.KClassImpl$data$1.invoke(KClassImpl.java)
at kotlin.reflect.jvm.internal.ReflectProperties$LazyVal.invoke(ReflectProperties.java:11)
at kotlin.reflect.jvm.internal.KClassImpl.getSimpleName(KClassImpl.java:2)
at com.android.tools.r8.GeneratedOutlineSupport.outline55(GeneratedOutlineSupport.java:4)
at MyAppViewModel.subscribe(MyAppViewModel.java:2)
at MyAppActivity.onResume(MyAppActivity.java:26)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1454)
at android.app.Activity.performResume(Activity.java:8111)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4647)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4690)
at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8167)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
Caused by java.lang.ClassCastException
at java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl.<init>(AtomicReferenceFieldUpdater.java:352)
at java.util.concurrent.atomic.AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdater.java:109)
at kotlin.SafePublicationLazyImpl.<clinit>(SafePublicationLazyImpl.java:6)
at kotlin.LazyKt__LazyJVMKt.lazy(LazyKt__LazyJVMKt.java:34)
at kotlin.reflect.jvm.internal.KProperty1Impl.<init>(KProperty1Impl.java:41)
at kotlin.reflect.jvm.internal.ReflectionFactoryImpl.property1(ReflectionFactoryImpl.java:18)
at kotlin.jvm.internal.Reflection.property1(Reflection.java:2)
at kotlin.reflect.jvm.internal.KDeclarationContainerImpl$Data.<clinit>(KDeclarationContainerImpl.java:18)
at kotlin.reflect.jvm.internal.KClassImpl$data$1.invoke(KClassImpl.java)
at kotlin.reflect.jvm.internal.KClassImpl$data$1.invoke(KClassImpl.java)
at kotlin.reflect.jvm.internal.ReflectProperties$LazyVal.invoke(ReflectProperties.java:11)
at kotlin.reflect.jvm.internal.KClassImpl.getSimpleName(KClassImpl.java:2)
at com.android.tools.r8.GeneratedOutlineSupport.outline55(GeneratedOutlineSupport.java:4)
at MyAppViewModel.subscribe(MyAppViewModel.java:2)
at MyAppActivity.onResume(MyAppActivity.java:26)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1454)
at android.app.Activity.performResume(Activity.java:8111)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4647)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4690)
at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8167)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
【问题讨论】:
我已经把它变成了一个 R8 问题,issuetracker.google.com/190382641。请自行抄送您的后续行动。 【参考方案1】:回答我自己的问题。一种解决方法是在您的 proguard-rules.pro 中添加以下规则:
-keepclassmembers class kotlin.SafePublicationLazyImpl
java.lang.Object _value;
问题在Google's issuetracker进行跟踪
【讨论】:
以上是关于Android R8 即时崩溃的主要内容,如果未能解决你的问题,请参考以下文章
在 Android 中使用 R8 和 Proguard 时如何保护数据模型类免受逆向工程的影响?
Android/Java 混淆:R8 与(ProGuard 或 DexGuard)?