Kerim Bora
Kerim Bora

Reputation: 123

Fatal Exception When Enabling R8 in Android App with Jetpack Compose and Coroutines - ClassCastException Issue

I'm currently developing an AndroidTV application using Jetpack Compose, Retrofit, and Coroutines. To test the performance issues in my project, I need to enable R8 mode. However, when I do so, I encounter the following error:

Fatal Exception: java.lang.ClassCastException:
   at com.xx.xx.domain.home.usecase.XxxXUseCase$invoke$2.invokeSuspend(XxxXUseCase.kt:24)
   at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
   at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
   at android.os.Handler.handleCallback(Handler.java:942)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loopOnce(Looper.java:201)
   at android.os.Looper.loop(Looper.java:288)
   at android.app.ActivityThread.main(ActivityThread.java:7898)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

or (with obfuscation):

Fatal Exception: java.lang.ClassCastException:
   at a.d.s(:5)
   at u6.a.h(SourceFile:81)
   at ic.a.t(SourceFile:9)
   at yc.e0.run(SourceFile:116)
   at android.os.Handler.handleCallback(Handler.java:942)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loopOnce(Looper.java:201)
   at android.os.Looper.loop(Looper.java:288)
   at android.app.ActivityThread.main(ActivityThread.java:7898)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

Here's my configuration:


My "build.gradle" file

release {
 minifyEnabled true

 firebaseCrashlytics {
  mappingFileUploadEnabled true
 }

 proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 
  'proguard-rules.pro'

 signingConfig signingConfigs.debug
}

My "proguard-rules.pro" file:

-keepattributes Signature


# Retrofit
-keep,allowobfuscation,allowshrinking interface retrofit2.Call
-keep,allowobfuscation,allowshrinking class retrofit2.Response
-keep,allowobfuscation,allowshrinking class kotlin.coroutines.Continuation

# Kotlin Coroutines
-dontwarn kotlin.**
-dontwarn kotlinx.atomicfu.** 
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}
-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}
-keepnames class kotlin.ResultKt {}
-keepclassmembernames class kotlinx.** {
    volatile <fields>;
}

I've tried to look into the documentation and search online for this specific error but couldn't find any relevant solutions. After some research, I also added the following code, but it didn't work:

-keepnames class kotlin.ResultKt {}

Has anyone faced this issue before, and how can I resolve it?

Additionally, it's worth mentioning that the application does not crash in debug mode or when "minifyEnabled" is set to false.

Any help would be appreciated. Thank you!

Upvotes: 2

Views: 2285

Answers (1)

Kerim Bora
Kerim Bora

Reputation: 123

this solved it:

in gradle.properties:

android.enableR8.fullMode=false

Upvotes: 1

Related Questions