Kotlinx.coroutines' bug

I add dependencies :implementation ‘org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1’ into Gradle,but the program crashes.I use Android Studio 3.2.1,kotlin version is 1.3.10,and my mobile phone system version is Android 6.

You are going to need to provide more information.

What are you trying to do?

You say the program crashes, so one can assume it compiles, right? By what you write it sounds like you are saying the app crashed because you added the dependency. But it is more likely that you are trying to say that the app is crashing despite adding the dependency. But that does not make it clear what you were trying to do.

What do you see in the logs when you run in debug mode.

Can you share some code?

It’s just that app crashes.I only add that dependency into Gradle,no change in code.When download to device,the app can’t work.It crashes immediately.
It compiles ok.

What happens when you comment it out?

Can you paste the error message from the logs here?

This is the debug log:

11/23 15:33:57: Launching app
$ adb push D:\MyTextProjects\TestAppKotlin\app\build\outputs\apk\debug\app-debug.apk /data/local/tmp/suntront.testappkotlin
$ adb shell pm install -t -r “/data/local/tmp/suntront.testappkotlin”
pkg: /data/local/tmp/suntront.testappkotlin
Success
APK installed in 11 s 690 ms
$ adb shell am start -n “suntront.testappkotlin/suntront.testappkotlin.MainActivity” -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: suntront.testappkotlin | suntront.testappkotlin.test
Waiting for application to come online: suntront.testappkotlin | suntront.testappkotlin.test
Waiting for application to come online: suntront.testappkotlin | suntront.testappkotlin.test
Waiting for application to come online: suntront.testappkotlin | suntront.testappkotlin.test
Connecting to suntront.testappkotlin
Connected to the target VM, address: ‘localhost:8600’, transport: ‘socket’
Capturing and displaying logcat messages from application. This behavior can be disabled in the “Logcat output” section of the “Debugger” settings page.
D/Zygote: CtrlSocket libc.so ctrl_sockets_set_addr pfunc is not exist!
I/art: Late-enabling -Xcheck:jni
D/ActivityThread: ActivityThread,attachApplication
D/HwCust: Create obj success use class android.content.res.HwCustHwResourcesImpl
W/ActivityThread: Application suntront.testappkotlin is waiting for the debugger on port 8100…
I/System.out: Sending WAIT chunk
I/art: Debugger is no longer active
I/art: Debugger is active
I/System.out: Debugger has connected
waiting for debugger to settle…
I/System.out: waiting for debugger to settle…
I/System.out: waiting for debugger to settle…
I/System.out: waiting for debugger to settle…
I/System.out: waiting for debugger to settle…
I/System.out: waiting for debugger to settle…
I/System.out: waiting for debugger to settle…
I/System.out: waiting for debugger to settle…
I/System.out: waiting for debugger to settle…
I/System.out: waiting for debugger to settle…
I/System.out: waiting for debugger to settle…
I/System.out: waiting for debugger to settle…
I/System.out: waiting for debugger to settle…
I/System.out: waiting for debugger to settle…
I/System.out: waiting for debugger to settle…
I/System.out: debugger has settled (1481)
W/System: ClassLoader referenced unknown path: /data/app/suntront.testappkotlin-2/lib/arm64
V/HwPolicyFactory: : success to get AllImpl object and return…
V/HwWidgetFactory: : successes to get AllImpl object and return…
V/ActivityThread: ActivityThread,callActivityOnCreate
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
Disconnected from the target VM, address: ‘localhost:8600’, transport: ‘socket’

This does not show an error or crash.

But the app can’t work.

This looks like a purely test application, can you put it on GitHub (or similar) for me to look at?

Just not add that dependency,the app works ok.

There must be something going on, just adding a dependency should not crash your app.

And to find out what that is requires information that is not evident here.

I find another issue: If add “org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1” dependency,the anko layout preview can’t work,layout doesn’t show.All dependencies I use are below:
dependencies{
implementation fileTree(include :[‘*.jar’],dir;‘libs’)
implementation “org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version”
implementation “org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version”

implementation ‘com.android.support:appcompat-v7:27.1.1’
//implementation ‘com.android.support:appcompat-v7:22.2.0’
implementation ‘com.android.support.constraint:constraint-layout:1.1.3’
implementation “org.jetbrains.anko:anko:$anko_version”
implementation “org.jetbrains.anko:anko-commons:$anko_version”
implementation “org.jetbrains.anko:commons-base:$anko_version”
implementation “org.jetbrains.anko:anko-constraint-layout:$anko_version”
implementation “org.jetbrains.anko:anko-sdk27:$anko_version”
implementation “org.jetbrains.anko:anko-appcompat-v7:$anko_version”
implementation “org.jetbrains.anko:anko-appcompat-v7-commons:$anko_version”
implementation “org.jetbrains.anko:anko-coroutines:$anko_version”
implementation “org.jetbrains.anko:anko-sdk27-coroutines:$anko_version”
implementation “org.jetbrains.anko:anko-appcompat-v7-coroutines:$anko_version”

implementation “org.jetbrains.anko:anko-design:$anko_version”
implementation “org.jetbrains.anko:anko-design-coroutines:$anko_version”
implementation “org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1”
implementation “org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1”
testImplementation ‘junit:junit:4.12’
androidTestImplementation ‘com.android.support.test:runner:1.0.2’
androidTestImplementation ‘com.android.support.test.espresso:espresso-core:3.0.2’
}
Are there any problems?
The code coundn’t have problem,it works ok.

Just of the bat.

You are including basically the same libraries twice (or more).

There is going to be a clash between Anko, which uses coroutines from 0.27.x (if I recall), and 1.01.

Why are you importing the stdlib twice?

You are also importing the same Anko code several times, why?

When you import
“org.jetbrains.anko:anko:$anko_version”, you get everything from Anko. Every other import is a duplicate.

Oh,I’m a learnner for Kotlin and anko.Please tell me how to do that.

I don’t know if, or when, Anko is getting an update, but looking into the build.gradle I see:

ext.kotlin_version = '1.2.71'
...
ext.kotlinx_coroutines_version = '0.23.4'

So if you are going to use Anko you are probably advised to stick to those versions.

But without seeing what the app does I cannot see what is causing the problem.

How is this thing even compiling. I just copied your dependencies into a test app, and it’s red [errors] everywhere.

Okay, it seems like the code was “stylized” either when you copied it, or in presentation.

Put your code snippets in-between two sets of three back-ticks (```), if you want it to stay clean.

dependencies{
    implementation fileTree(dir: 'libs', include: ['*.jar'])

//    You only need one 
//    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    implementation 'com.android.support:appcompat-v7:27.1.1'

    implementation "com.android.support.constraint:constraint-layout:1.1.3"
    implementation "org.jetbrains.anko:anko:$anko_version"

//    This is all duplication
//    implementation "org.jetbrains.anko:anko-commons:$anko_version"
//    implementation "org.jetbrains.anko:commons-base:$anko_version"
//    implementation "org.jetbrains.anko:anko-constraint-layout:$anko_version"
//    implementation "org.jetbrains.anko:anko-sdk27:$anko_version"
//    implementation "org.jetbrains.anko:anko-appcompat-v7:$anko_version"
//    implementation "org.jetbrains.anko:anko-appcompat-v7-commons:$anko_version"
//    implementation "org.jetbrains.anko:anko-coroutines:$anko_version"
//    implementation "org.jetbrains.anko:anko-sdk27-coroutines:$anko_version"
//    implementation "org.jetbrains.anko:anko-design:$anko_version"
//    implementation "org.jetbrains.anko:anko-design-coroutines:$anko_version"

//    This is likely clashing with coroutine code imported by Anko     
//    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1"
//    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1"

    testImplementation "junit:junit:4.12"
    androidTestImplementation "com.android.support.test:runner:1.0.2"
    androidTestImplementation "com.android.support.test.espresso:espresso-core:3.0.2"
}

Ok,I’ll try it.