Skip to main content

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
The Kotlin SDK does not support x86 devices. Since x86 devices are the default option in Android Studio, you must create an x86_64 or arm64 device in AVD Manager to run applications that use the SDK. You can find images for compatible devices in AVD Manager. Follow these steps to create and use a compatible device:
  1. Open AVD Manager in Android Studio by selecting Tools > AVD Manager.

  2. Click the Create Virtual Device button.

  3. Select a phone, such as Nexus 5.

  4. Click the Next button.

  5. Navigate to the x86 Images tab.

  6. Click the Download link next to an x86_64 device image, such as S / x86_64 / Android API S (Google Play).

  7. Click the Finish button to close the dialogue when the image finishes downloading.

  8. Click the Next button.

  9. Name your device and click the Finish button to add it to your list of virtual devices.

  10. 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​

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-base to the dependencies block of your common module.
  • Add io.realm.kotlin to your list of plugins.
  • To use coroutines with the SDK, add org.jetbrains.kotlinx:kotlinx-coroutines-core to 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)
}
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.