Dimitris
Dimitris

Reputation: 755

What’s the use of the coroutines dependencies on an Android app?

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'

Upvotes: 3

Views: 2401

Answers (3)

cactustictacs
cactustictacs

Reputation: 19554

I'm adding another answer because this is a broader issue with Android libraries in general, not just the coroutines library, so you need to be mindful of it. The libraries you use have their own dependencies, so they pull in certain versions of other libraries that they require.

These additional dependencies are called transitive dependencies, and it's why you can use e.g. LiveData without adding the androidx.lifecycle:lifecycle-livedata-ktx package - it's because something else is pulling in a version of that library, e.g. the activity library. You'll often see these dependency versions mentioned in the changelogs for a library, like this activity one:

Version 1.5.1

July 27, 2022

Dependency update

  • The Activity library now depends on the Lifecycle 2.5.1.

What this means is using version 1.5.1 of the activity library will pull in version 2.5.1 of the lifecycle libraries. That's the most recent dependency version bump, so that goes for the newer versions of activity too.


But often those transitive dependencies will be outdated - libraries usually won't require new versions of another dependency unless they've made significant changes that require an upgrade. Generally, they're not requiring the most recent versions of other libraries.

This means if you want to use newer versions of those libraries, you need to explicitly add them as dependencies yourself. For a long time, activity would pull in a really old version of lifecycle - if you wanted to use the newer features, you had to import lifecycle yourself, using the version number you wanted. When a dependency is declared multiple times with different versions, Gradle picks the highest version (unless you explicitly take steps to prevent that).

If you want to see the dependency tree for your app, you can run the dependencies Gradle task - it'll show you what dependencies each library requests, their version numbers, and whether they're the version that's ultimately being selected or if they're being superseded by another declaration elsewhere in the tree. You can do that with this button in the Gradle window:

The Gradle toolbar with the "run task" button highlighted

and running gradle app:dependencies (or using gradlew from the command line). There's some info about interpreting the output here.

Upvotes: 1

Yurii
Yurii

Reputation: 685

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.

Upvotes: 4

Sergio
Sergio

Reputation: 30655

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.

Upvotes: 2

Related Questions