I am trying to build a release version of my application with R8 full mode but it results to crashes. More specifically, when installing through Android Studio either the build will be fully functional or it will crash at once with the stacktraces added below.
Sometimes it happens immediately after configuration changes, e.g debuggable false
to debuggable true
and sometimes after consecutive builds of the same gradle configuration which is very weird and I can not determine its behavior.
release {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
minifyEnabled true
debuggable false
shrinkResources true
}
Proguard file:
-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
The application is built with
Due to Jetpack Compose inclusion we have also added:
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'
Moreover multiDexEnabled true
and implementation "org.jetbrains.kotlin:kotlin-reflect:1.5.10"
which would be a possible solution is included.
Crash 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)
Crash 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)
Answering my own question. A workaround is to add the following rule in your proguard-rules.pro:
-keepclassmembers class kotlin.SafePublicationLazyImpl {
java.lang.Object _value;
}
Issue is tracked at Google's issuetracker