Step by Step build a Kotlin Multiplatform Library (SDK) — Part 1
Recently, we decided to try out Kotlin Multiplatform by starting with building an in-house API SDK. We noticed there are lack of documentation and article talk about Kotlin Multiplatform, and we did realised Kotlin Multiplatform ain’t easy to start. So we decided to share our experience by providing a comprehensive guide for it.
Ok, let’s start.
Create a project
First of all, open your IntelliJ IDEA, and select File > New > Project. After that, select Kotlin in the left-hand panel. Enter a project name, then in the Multiplatform section select Library as the project template.
After you clicked Finish, the widget will create a sample multiplatform library with the following structure:
Before we go further, I will briefly explain what are those folder (such as commonMain, jsMain, etc) represents.
commonMain
This folder will include the shared business logic for all platform.
jsMain
This folder will include the business logic for web application (JavaScript).
jvmMain
This folder will include the business logic for JVM (Desktop, Android, etc).
nativeMain
This folder will include the business logic for native platform (iOS, linux, mingw, tvos, etc).
Android Platform Support
While Android supports most Java language features, but it doesn’t support every API that Java provides. In other words, JVM platform not always able to fulfil our needs, some Android device may not able to run JVM artifact. To overcome this, we will enable android platform manually. (Take note: io.revenuemonster.sdk is our sdk package name, you should always rename it to your package name)
- We create an androidMain folder.
2. Create a AndroidManifest.xml file inside the androidMain folder
3. Add plugin com.android.library.
4. Update build.gradle.kts in root, add android configuration and androidMain into sourceSets lambda.
Before we proceed further, there are some critical issues Jetbrains yet to solve at the moment. So I would suggest you always publish your android library with debug variant. Following are open issue link :
Meaning, don’t do this.
kotlin {
// setup for android
android {
publishLibraryVariants("release")
}
}
Do this instead (Always include the debug variant)
kotlin {
// setup for android
android {
publishLibraryVariants("release", "debug")
}
}// or kotlin {
// setup for android
android {
publishAllLibraryVariants()
}
}
5. Make sure you include google() and mavenCentral() in your repository, if you using any package from JitPack, you may add maven { url 'https://jitpack.io' }
. If you still using jcenter(), kindly remove it because it gonna deprecated.
6. Make sure your settings.gradle.kts use module of android gradle plugin, com.android.tools.build:gradle:4.2.0 (you may use the latest version anyway)
7. Lastly, remember to create local.properties file in your project root and locate your Android SDK. If you don’t know where your Android SDK path, can go to https://stackoverflow.com/questions/34532063/finding-android-sdk-on-mac-and-adding-to-path.
Else you will get an error like this.
Yeah, after you completed all the required steps, your Multiplatform library will able to generate android platform artifact. To test this out, you may verify this by using publishToMavenLocal.
Publish To Maven Local
Before you prepare to publish your library to Jitpack or Maven Central. You may publish it to MavenLocal first.
After you execute the publishToMavenLocal command, it will publish the artifacts to your local .m2 folder. (Usually it will reside in /Users/<name>/.m2)
In the next article, we will continue with iOS Setup.
Part 1 — https://sianloong90.medium.com/step-by-step-build-a-kotlin-multiplatform-library-sdk-part-1-e973bdbfa55d
Part 2 — https://sianloong90.medium.com/step-by-step-build-a-kotlin-multiplatform-library-sdk-part-2-1d3a2f58dce1