Kotlinx.coroutines: Support for Android JetPack

Created on 23 May 2018  路  20Comments  路  Source: Kotlin/kotlinx.coroutines

The kotlinx-coroutines-android package is not compatible with android JetPack.
See #366 .

Most helpful comment

Also just ran into this. The compile error I get is Type mismatch: inferred type is HandlerContext; but CoroutineContext was expected and then if I cast UI to CoroutineContext it becomes Cannot access class 'Lkotlinx.coroutines.experimental.android.HandlerContext;'. Check your module classpath for missing or conflicting dependencies. Unfortunately, I don't think disabling jetifier is an option for me.

All 20 comments

Using the jetifier it works fine. Are you sure you configured it correctly? There should be no work done here until the androidx version of the annotations artifact is stable.

I've created a new application with the empty activity template. Added coroutine dependencies and wrote a simple launch with UI context that updated the textview added by the template. Then used the refactor to androidx feature in android studio, then got error when I tried to make the project. Then I changed android.enableJetifier from true to false in gradle.properties and it compiled fine.

yup. happened to my project as well. only happen when making the project/apk.
Weird thing is there's no error in the code as Android Studio recognize the lib/class.

Disabling jetifier in gradle.properties works.

Also just ran into this. The compile error I get is Type mismatch: inferred type is HandlerContext; but CoroutineContext was expected and then if I cast UI to CoroutineContext it becomes Cannot access class 'Lkotlinx.coroutines.experimental.android.HandlerContext;'. Check your module classpath for missing or conflicting dependencies. Unfortunately, I don't think disabling jetifier is an option for me.

Same problem here. Disabling Jetifier would break many other things so it is not an option for me too. The only way I managed to get it to work is copying kotlinx.coroutines.experimental.android.HandlerContext and UI to my own project. This is obviously not a proper way of fixing this but at least I can continue developing and preparing my app for the future with AndroidX until it all works together without any hacks.

I can confirm that the workaround offered by @NamtarR works. Be sure to manually correct the support annotations import in UI.kt, otherwise it's as simple as copying that file into your project.

It's an ugly hack, but after spending the whole morning on this, it's the best approach until the problem is resolved upstream.

Can you, please, create and publish a small project that would reproduce and demonstrate this problem, so that we can study how to fix (and verify that our fix works).

@elizarov, created a simple project that reproduces exactly those errors as stated here.

e: C:\Users\Anton\Documents\Projects\Android\CoroutinesTest\app\src\main\java\com\namtarr\coroutinestest\MainActivity.kt: (15, 16): Cannot access class 'Lkotlinx.coroutines.experimental.android.HandlerContext;'. Check your module classpath for missing or conflicting dependencies
e: C:\Users\Anton\Documents\Projects\Android\CoroutinesTest\app\src\main\java\com\namtarr\coroutinestest\MainActivity.kt: (15, 16): Type mismatch: inferred type is 'HandlerContext'; but CoroutineContext was expected
> Task :app:compileDebugKotlin FAILED

It contains one simple launch that works perfectly with Jetifier disabled.
Also, it works fine with Jetifier enabled and this hack.

It appears to be a bug in the Android toolchain.
Downgrade com.android.tools.build from 3.3.0-alpha06 to 3.2.0-beta05 in the sample project fixes the problem.
@JakeWharton could you please confirm?

I can confirm @choongyouqi changing the com.android.tools.build works. Only 3.2.0-beta-05 is currently incompatible and changed to 3.2.0-rc01.

I get this if I try to switch to 3.2.0-rc01, though.
Don't know if that's an Android Studio Canary thing.
image

@NamtarR it works copy kotlinx.coroutines.experimental.android.HandlerContext to my project

Now that the androidx.annotation:annotation:1.0.0 artifact is stable, are there plans to migrate to it?

@cckroets coroutines are also stable when Kotlin 1.3 arrives

Is this still an issue?

@madewulf The kotlinx.coroutines suggested there is outdated (0.26.1). You can use 0.30.2 with Kotlin 1.2 and 1.0+ with Kotlin 1.3+

That's what I just found out, thanks!

I'm closing this issues as outdated.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jaozinfs picture jaozinfs  路  3Comments

elizarov picture elizarov  路  3Comments

Leftwitch picture Leftwitch  路  3Comments

IgorKey picture IgorKey  路  3Comments

iTanChi picture iTanChi  路  3Comments