Install (Kotlin Multiplatform)
Prerequisites​
- Android Studio version 4.2.2 or higher.
- JDK 11 or higher
- Kotlin Plugin for Android Studio, version 1.5.20 or higher.
- Kotlin Multiplatform Mobile (KMM) Plugin for Android Studio, version 0.2.6 or higher.
- A KMM App created using the "KMM Application" template in Android Studio. Follow the instructions in the KMM documentation.
- An Android Virtual Device (AVD) using a compatible architecture (x86_64 or arm64).
x86 Devices are not Compatible with the Kotlin SDK
Open AVD Manager in Android Studio by selecting
Tools>AVD Manager.Click the
Create Virtual Devicebutton.Select a phone, such as
Nexus 5.Click the
Nextbutton.Navigate to the
x86 Imagestab.Click the
Downloadlink next to an x86_64 device image, such asS / x86_64 / Android API S (Google Play).Click the
Finishbutton to close the dialogue when the image finishes downloading.Click the
Nextbutton.Name your device and click the
Finishbutton to add it to your list of virtual devices.At the top of your Android Studio window, select your compatible device in the AVD dropdown.
You can track x86 support in this GitHub issue.
Installation​
- Realm Database
- Sync
To add local-only Realm Database to your application, make
the following changes to your shared module Gradle build
file, typically found at <project>/shared/build.gradle.kts:
- Add
io.realm.kotlin:library-baseto the dependencies block of your common module. - Add
io.realm.kotlinto your list of plugins. - To use coroutines with the SDK, add
org.jetbrains.kotlinx:kotlinx-coroutines-coreto the dependencies block of your common module.
plugins {
kotlin("multiplatform")
kotlin("native.cocoapods")
id("com.android.library")
id("io.realm.kotlin")
}
version = "1.0"
kotlin {
android()
iosX64()
iosArm64()
cocoapods {
summary = "Some description for the Shared Module"
homepage = "Link to the Shared Module homepage"
ios.deploymentTarget = "14.1"
podfile = project.file("../iosRealmKMMApp/Podfile")
framework {
baseName = "shared"
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0-native-mt")
implementation("io.realm.kotlin:library-base:0.10.0")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.2")
}
}
val iosX64Main by getting
val iosArm64Main by getting
val iosMain by creating {
dependsOn(commonMain)
iosX64Main.dependsOn(this)
iosArm64Main.dependsOn(this)
}
val iosX64Test by getting
val iosArm64Test by getting
val iosTest by creating {
dependsOn(commonTest)
iosX64Test.dependsOn(this)
iosArm64Test.dependsOn(this)
}
}
}
android {
compileSdk = 30
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdk = 28
targetSdk = 30
}
}
Then, add classpath("io.realm.kotlin:gradle-plugin:${variables.kotlin_sdk_version}")
to the buildscript.dependencies block of your project-level
Gradle build file, typically found at <project>/build.gradle.kts:
buildscript {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10")
classpath("com.android.tools.build:gradle:7.1.1")
classpath("io.realm.kotlin:gradle-plugin:0.10.0")
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
}
To add Realm Database and Sync to your application, make
the following changes to your shared module Gradle build
file, typically found at shared/build.gradle.kts:
- Add
io.realm.kotlin:library-syncto the dependencies block of your common module. - Add
io.realm.kotlinto your list of plugins. - To use coroutines with the SDK, add
org.jetbrains.kotlinx:kotlinx-coroutines-coreto the dependencies block of your common module.
plugins {
kotlin("multiplatform")
kotlin("native.cocoapods")
id("com.android.library")
id("io.realm.kotlin")
}
version = "1.0"
kotlin {
android()
iosX64()
iosArm64()
cocoapods {
summary = "Some description for the Shared Module"
homepage = "Link to the Shared Module homepage"
ios.deploymentTarget = "14.1"
podfile = project.file("../iosRealmKMMApp/Podfile")
framework {
baseName = "shared"
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0-native-mt")
implementation("io.realm.kotlin:library-sync:0.10.0")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.2")
}
}
val iosX64Main by getting
val iosArm64Main by getting
val iosMain by creating {
dependsOn(commonMain)
iosX64Main.dependsOn(this)
iosArm64Main.dependsOn(this)
}
val iosX64Test by getting
val iosArm64Test by getting
val iosTest by creating {
dependsOn(commonTest)
iosX64Test.dependsOn(this)
iosArm64Test.dependsOn(this)
}
}
}
android {
compileSdk = 30
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdk = 28
targetSdk = 30
}
}
Then, add io.realm.kotlin:gradle-plugin
to the buildscript.dependencies block of your project-level
Gradle build file, typically found at <project>/build.gradle.kts:
buildscript {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10")
classpath("com.android.tools.build:gradle:7.1.1")
classpath("io.realm.kotlin:gradle-plugin:0.10.0")
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
}
Android Module Dependencies
If you use any part of the SDK inside the Android module, add the
SDK's compile-time dependency to your Android module build.gradle.kts:
dependencies {
compileOnly("io.realm.kotlin:library:${variables.kotlin_sdk_version}")
}
After updating the Gradle configuration,
resolve the dependencies by clicking File >
Sync Project with Gradle Files in the Android Studio menu bar.
You can now use the Kotlin SDK in your application.