What does this mean and how can I avoid it?
Can you add more details, please? Stacktrace? How and where it appears? Steps to reproduce?
I think it's some sort of proguard issue, but I'm not sure which class is being broken. Happens 100% of the time AFAIK. It's fails here which makes no sense because that just references a method which returns the value or null (doesn't throw any exceptions).
Non-fatal Exception: java.lang.ClassCastException: kotlin.Result$Failure cannot be cast to com.google.firebase.auth.d
at com.supercilex.robotscouter.shared.client.AuthKt.onSignedIn(SourceFile:58)
at com.supercilex.robotscouter.shared.client.AuthKt$onSignedIn$1.invokeSuspend(SourceFile)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(SourceFile:32)
at kotlinx.coroutines.DispatchedTask.run(SourceFile:285)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(SourceFile:594)
at kotlinx.coroutines.scheduling.CoroutineScheduler.access$getSchedulerName$p(SourceFile:60)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(SourceFile:742)
Happens 100% of the time AFAIK
Could you please make a consistent reproducer or unit-test in your project that always fails and have zero external dependencies?
Apologies for the delay, I'll see if I can get a sample going next week.
I upgraded a bunch of deps and can't repro anymore. Will reopen if I come across it again.
Which dependencies did you update? I am having the same issue. The debug build runs fine, but the release build which uses proguard is affected by this bug.
I'm also getting this issue in production, I'm not sure if it's related to, or caused by https://github.com/JakeWharton/retrofit2-kotlin-coroutines-adapter/issues/45
I've been investigating it, and it only seems to occur after proguard, when attempting to handle exceptions in a coroutine, I'll post more info when I get it.
I've created a generified gist showing an instance in our codebase that causes cast exception.
In this example, the server returns a 401 with an appropriate error body including code and message because the user is not logged in.
https://gist.github.com/ashdavies/a4d852c773c15aaf1086ac6c85c41ec3
We've found that it happens only after Kotlin version 1.3.21, so we've rolled back for now
I got around the problem by letting the CoroutineContext use Dispatchers.IO instead of Dispatchers.Main. Where I need to work on main, I am now using withContext(Dispatchers.Main).
override val coroutineContext: CoroutineContext
get() = Dispatchers.IO + job
Wouldn't that just be crashing your IO thread instead?
The crashes I had vanished. I could reproduce the issue when I started a long running coroutine, quickly changed to another screen where a new coroutine was started and got the crash when the first coroutine finished and tried to deliver its result.
I still have them 😢
The same bug on release build with obfuscation in network call with retrofit2 with suspended function (native one, not adapter).
Workaround to launch withContext(Dispatchers.IO)
If someone would find the cause of error, pls provide a solution
It occurs for me only with ProGuard and optimisation enabled (proguard-android-optimize.txt). I didn't try it with R8 instead of ProGuard.
The workaround in my case is to disable some of optimisations via adding these line to my custom proguard config:
-optimizations !method/*,!code/removal/advanced
I tried to limit the amount of disabled optimisations, but it seems to be the issue appears because of several of them.
I don't use retrofit though. Probably it's a bug of ProGuard, not Kotlin.
Could someone reopen the issue?
After changing my sample project from #1288 a bit I now can also reproduce the java.lang.ClassCastException: a.g$b cannot be cast to <Type>: https://github.com/awenger/coroutinesobfuscationcrash/tree/1035
The problem is related to aggressive optimization of ProGuard. The problem can be avoided by disabling certain optimizations or using a flag to use a more conservative approach:
-Doptimize.conservatively=true
Can also be added like that in the gradle.properties:
systemProp.optimize.conservatively=true
We will further track this issue and improve ProGuard asap.
We are encountering a similar issue due to proguard. Where is this issue currently tracked ? I would like keep track of it to know when it is resolved and update accordingly.
Trace to issue at ProGuard @ github: https://github.com/Guardsquare/proguard/issues/1
The issue is already fixed for DexGuard, but will be applied asap to ProGuard as well.
Current workaround is to use conservative optimization using -Doptimize.conservatively=true
Update for ProGuard: the issue is fixed in the code repository and the fix will be included in the upcoming version ProGuard 6.2.0
To those having issues:
Which Android Gradle Plugin issues were you using?
@awenger Can you reproduce the issue on a release build made with AGP 3.5.0-rc-03?
You can try from command line with ./gradlew :yourAppModule:installRelease and revert after build if you don't want to use Android Studio 3.5 until it's out of RC.
We updated the Kotlin version to org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.40 and got the crash. After reverting back to 1.3.21, everything is fine. Android build tool version is 3.3.2.
@endru-kargo Can your try on a release build with AGP 3.5-rc-03, Kotlin 1.3.41 and kotlinx.coroutines 1.3.0-RC2?
This will help know if R8 is also affected or if this is a Proguard only issue.
@LouisCAD , really sorry. I tried to create the release AAB with the above mentioned configs. But the Android Studio throws buildOutput.apkData must not be null error. I tried to clean up the build folders and did all recommended solutions from StackOverflow, but still unsuccessful.
@endru-kargo Please report that issue on b.android.com and link it here.
Does the same error get thrown from command line build using ./gradlew :yourAppModule:bundleRelease, or ./gradlew :yourAppModule:installRelease?
I has the same problem, when server return HTTP code 401.tried kotlin 1.3.41 and coroutines 1.3.1 not ok.
Do you also have the issue with Kotlin 1.3.50?
On Mon, Sep 23, 2019, 07:23 Callmepeanut notifications@github.com wrote:
I has the same problem, tried kotlin 1.3.41 and coroutines 1.3.1 not ok.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/Kotlin/kotlinx.coroutines/issues/1035?email_source=notifications&email_token=ABVG6BKTSDDXQIBJDUHCTYLQLBHFPA5CNFSM4G43WXO2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7J2CLY#issuecomment-533963055,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABVG6BP5INXHM4ASLFHD5HTQLBHFPANCNFSM4G43WXOQ
.
@LouisCAD Yes, Kotlin 1.3.50 do not solve the problem. Add "systemProp.optimize.conservatively=true" to gradle.properties doesn't work too.
ProGuard 6.2.0 has now been released including a fix for this problem.
Most helpful comment
The problem is related to aggressive optimization of ProGuard. The problem can be avoided by disabling certain optimizations or using a flag to use a more conservative approach:
-Doptimize.conservatively=true
Can also be added like that in the gradle.properties:
systemProp.optimize.conservatively=true
We will further track this issue and improve ProGuard asap.