Adding an Android Target to a Kotlin Multiplatform Project

For those times when pure JVM just isn’t enough

Kris Wong
VMware 360
3 min readJan 22, 2020

--

This is the third post in a series on Kotlin Multiplatform. In my second post, I walked you through the steps to build a fat framework for your iOS app. In this post, I’ll walk you through the steps to add an Android target to your project.

When using the Mobile Shared Library (or similar) template in IntelliJ IDEA to create a new Multiplatform project (MPP), it will create a target for JVM and a target for iOS. A JVM target will not allow you to use Android APIs or resources in your library. This can obviously be quite limiting for a library that is meant to be integrated into an Android app. When we create an Android library project within Android Studio, the new project template will take care of all the basic steps necessary to setup the project to build an Android library. We don’t have that luxury in this case. But never fear, I will walk you through the manual steps necessary to “convert” the JVM library target to Android.

The steps below must all be completed before you will be able to build your project, so the order isn’t of high importance. Feel free to complete them in any order you choose.

  • At the top of your build.gradle file, you will need to add the following code block:

This is the classpath for the Android Gradle plugin.

  • After your plugins block, paste the following 2 lines:

This will apply the Android library, and Kotlin Android Extensions plugins to your project.

  • Within your repositories block (not the block that’s nested within buildscript), add the google repository. I also change mavenCentral to jcenter.
  • Within gradle-wrapper.properties, update the distributionUrl to point to the latest version of Gradle (5.6.2 at the time of this writing).
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-bin.zip
  • Within the kotlin block in build.gradle, change the jvm() target to android().
  • Update your jvmMain and jvmTest source sets to androidMain and androidTest. You will need to do this for the directory names on disk, as well as the source set names in build.gradle (tip: to rename the directory, right click in the Project pane and check under Refactor).
  • Now, we need to configure the Android plugin, using the typical android block in build.gradle. The following is an example that you can adjust for your needs:

Most of this is pretty standard, however, you will notice that we need to inform the Android plugin about where to find the relevant sources. This is because the default source directories for the Kotlin Multiplatform plugin and Android Gradle plugin do not align, so we’ll need to pick one standard and configure the other.

Your project structure should look similar to the following:

Project structure

You may notice that there are no res directories under the androidMain and androidTest source sets. You can create those whenever you need them. They’re not required.

  • Create an AndroidManifest.xml underneath the androidMain directory. This is minimal content for the file:
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.example.android" />

After making this change, I needed to restart IDEA before it would import the build.gradle correctly. It was giving me an error about not being able to read the package name from the manifest.

You should now be able to build your project and see the Android artifacts under build/outputs/aar. Congrats on successfully adding an Android target to your Kotlin MPP!

In my next post, I will discuss the steps necessary to build an umbrella (aggregate) framework project in order to integrate multiple MPPs into your iOS app.

--

--

Kris Wong
VMware 360

Software engineer+architect. Entrepreneur. Real estate investor.