Skip to content

invokeSuspend throws NullPointer while minifyEnabled is true #910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tRuNKator opened this issue Dec 21, 2018 · 6 comments
Closed

invokeSuspend throws NullPointer while minifyEnabled is true #910

tRuNKator opened this issue Dec 21, 2018 · 6 comments
Labels

Comments

@tRuNKator
Copy link

tRuNKator commented Dec 21, 2018

Hello, I used version 1.0.1, but found a problem when exceptions were not processed if the coroutine was canceled (same issue(fixed in new version)). Now I have updated the version to 1.1.0-alpha. And again I ran into another problem. Such code:
MainActivity.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        GlobalScope.launch {
            val result = safeApiCall { doDelay() }
            println(result)
        }
    }

    suspend fun doDelay(): Boolean {
        delay(2000)
        return true
    }
}

Utils.kt:

suspend fun safeApiCall(call: suspend () -> Boolean): Boolean {
    return try {
        call()
    } catch (throwable: Throwable) {
        false
    }
}

Generates such code for Utils.kt (see on last line):

.method public final invokeSuspend(Ljava/lang/Object;)Ljava/lang/Object;
    .registers 3
    .param p1    # Ljava/lang/Object;
        .annotation build Lorg/jetbrains/annotations/NotNull;
        .end annotation
    .end param
    .annotation build Lorg/jetbrains/annotations/Nullable;
    .end annotation

    iput-object p1, p0, Lcom/example/myapplication/UtilsKt$safeApiCall$1;->result:Ljava/lang/Object;
    iget p1, p0, Lcom/example/myapplication/UtilsKt$safeApiCall$1;->label:I
    const/high16 v0, -0x80000000
    or-int/2addr p1, v0
    iput p1, p0, Lcom/example/myapplication/UtilsKt$safeApiCall$1;->label:I
    const/4 p1, 0x0

    throw p1
.end method

Even with this rules:

-dontobfuscate
-dontoptimize
-dontnote
-dontobfuscate
-dontoptimize
-dontpreverify
-dontshrink

AS 3.4 Canary 9, android gradle plugin ver 3.4.0-alpha09, kotlin version 1.3.11

@tRuNKator tRuNKator changed the title Throws NullPointer while minifyEnabled is true invokeSuspend throws NullPointer while minifyEnabled is true Dec 21, 2018
@qwwdfsad
Copy link
Member

It is a bug in minifier, we can't fix it from kotlinx.coroutines.
Please report it to R8

@Pulimet
Copy link

Pulimet commented Jan 17, 2019

@Pulimet
Copy link

Pulimet commented Jan 17, 2019

@tRuNKator , R8 team fixed the issue, but how can I use the version with this fix?
We need to wait for next gradle tools version and it will include R8 version with the fix? Or there is other way to set it manually?
Thanks.

@tRuNKator
Copy link
Author

tRuNKator commented Jan 17, 2019

@tRuNKator , R8 team fixed the issue, but how can I use the version with this fix?
We need to wait for next gradle tools version and it will include R8 version with the fix? Or there is other way to set it manually?
Thanks.

You can apply new version manually. But I don't know if this fix entered this version.

buildscript {
    repositories {
        google()
        jcenter()
        mavenCentral()
        maven { url "http://storage.googleapis.com/r8-releases/raw/" }
    }

    dependencies {
        classpath 'com.android.tools:r8:1.4.25'  // Must be before the Gradle Plugin for Android.
        classpath "com.android.tools.build:gradle:androidGradlePluginVersion"
    }
}

@Cililing
Copy link

Hi, I have the same problem.
But in my case I am not able to use R8 - very old project that is based on experimental plugin (0.11.0), and there is no way that someone will rewrite ours build scripts.

So, no chance to use R8, but in "normal" proguard bug also exists.
Is there any way to fix that? Thanks.

@elizarov
Copy link
Contributor

You can work around by turning off minification or tweaking minification settings. I'm not sure there's anything more we can help with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants