Home > Software design >  What’s the use of the coroutines dependencies on an Android app?
What’s the use of the coroutines dependencies on an Android app?

Time:12-20

My question is what’s the use of the coroutines dependencies on an Android app?

Specifically the following dependency that is mentioned in the Android Developers site

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'

(or the newer version 1.6.4)

Seems to me that everything that is related to coroutines, works fine even without this dependency. So what is the purpose of adding this dependency to an Android project?

The rest of the dependencies of the Android project

implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation "androidx.recyclerview:recyclerview:1.3.0-alpha02"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'

CodePudding user response:

According to the docs:

This gives you access to the Android Dispatchers.Main coroutine dispatcher and also makes sure that in case of a crashed coroutine with an unhandled exception that this exception is logged before crashing the Android application, similarly to the way uncaught exceptions in threads are handled by the Android runtime.

So if you don't use Dispatchers.Main and don't want exceptions to be logged, you can remove this dependency. And make sure to test the app after removing it.

CodePudding user response:

Just for your information: coroutines are already included in libraries:


dependencies {
    implementation('androidx.appcompat:appcompat:1.5.1'){
        exclude group: 'org.jetbrains.kotlinx', module: 'kotlinx-coroutines-core-jvm'
        exclude group: 'org.jetbrains.kotlinx', module: 'kotlinx-coroutines-android'
        exclude group: 'org.jetbrains.kotlinx', module: 'kotlinx-coroutines-core'
    }
    //implementation 'com.google.android.material:material:1.7.0' <- include coroutines
    //implementation 'androidx.constraintlayout:constraintlayout:2.1.4' <- include coroutines
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
}

That's why you see and can use coroutines features.

  • Related