Kotlin Help

Adding Android dependencies

The workflow for adding Android-specific dependencies to a Kotlin Multiplatform module is the same as it is for pure Android projects: declare the dependency in your Gradle file and import the project. After that, you can use this dependency in your Kotlin code.

We recommend declaring Android dependencies in Multiplatform Mobile projects by adding them to a specific Android source set. For that, update your build.gradle(.kts) file in the shared directory of your project:

sourceSets["androidMain"].dependencies { implementation("com.example.android:app-magic:12.3") }
sourceSets { androidMain { dependencies { implementation 'com.example.android:app-magic:12.3' } } }

Moving what was a top-level dependency in an Android project to a specific source set in a Multiplatform Mobile project might be difficult if the top-level dependency had a non-trivial configuration name. For example, to move а debugImplementation dependency from the top level of an Android project, you’ll need to add an implementation dependency to the source set named androidDebug. To minimize the effort you have to put in to deal with migration problems like this, you can add a dependencies block inside the android block:

android { //... dependencies { implementation("com.example.android:app-magic:12.3") } }
android { //... dependencies { implementation 'com.example.android:app-magic:12.3' } }

Dependencies declared here will be treated exactly the same as dependencies from the top-level block, but declaring them this way will also separate Android dependencies visually in your build script and make it less confusing.

Putting dependencies into a standalone dependencies block at the end of the script, in a way that is idiomatic to Android projects, is also supported. However, we strongly recommend against doing this because configuring a build script with Android dependencies in the top-level block and other target dependencies in each source set is likely to cause confusion.

What's next?

Check out other resources on adding dependencies in multiplatform projects and learn more about:

Last modified: 30 March 2022