Quick Setup: Kotlin multiplatform Project(MPP)

Vikram Hooda
2 min readJul 7, 2019

--

Step: 1) Create a new project if you do not have already existed and then create a new java/android library module into your project. Let’s give this lib module name “mpp” and package name “com.example.mpp”.

Step: 2) Edit your “mpp” module’s build.gradle file and apply the “Kotlin- multiplatform” plug-in and set the target as given below:

apply plugin: 'kotlin-multiplatform'


kotlin {
targets {
final def iOSTarget = System.getenv('SDK_NAME')?.startsWith("iphoneos") \
? presets.iosArm64 : presets.iosX64

fromPreset(iOSTarget, 'iOS') {
compilations.main.outputKinds('FRAMEWORK')
}

fromPreset(presets.jvm, 'android')
}

sourceSets {
commonMain.dependencies {
api 'org.jetbrains.kotlin:kotlin-stdlib-common'
}

androidMain.dependencies {
api 'org.jetbrains.kotlin:kotlin-stdlib'
}
}
}

configurations {
compileClasspath
}

Now you will need to create new directories and files in “mpp” module as given below.

src/androidMain/kotlin/android.kt

package com.example.mpp

actual fun messageFromPlatform(): String {
return "Hello from Android to Kotlin Multi platform world!"
}

src/iosMain/kotlin/ios.kt

package com.example.mpp

actual fun messageFromPlatform(): String {
return "Hello from iOS to Kotlin Multi platform world!"
}

src/commonMain/kotlin/common.kt

package com.example.mppexpect fun messageFromPlatform(): String

fun greeting(): String {
return messageFromPlatform()
}

Note: Do not forget to add your “mpp” module into app’s build.gradle file as:

implementation project(':mpp')

You are all set now to call “greeting()” function from your android app module. If you find any issue in regards to import/reference then do clean/rebuild and gradle sync. Once sync finish, you will find binaries file under mpp’s build folder. That’s it.

You can find the source code here: Kotlin-Multi-Platform-Project(MPP)-Example

Happy learning!!!

--

--