Docs Menu

Install Realm - Kotlin Multiplatform SDK

  • Android Studio version 4.2.2 or higher.
  • Kotlin Multiplatform Mobile (KMM) Plugin for Android Studio, version 0.2.6 or higher.
  • Kotlin Plugin for Android Studio, version 1.5.20 or higher.
  • JDK 11 or higher
  • An Android Virtual Device (AVD) using either the x86_64 or arm64 architecture.
Warning
x86 Devices are not Supported

The Realm Kotlin Multiplatform SDK does not yet 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.

Follow these steps to add the Realm Kotlin Multiplatform SDK to your KMM project.

1

Create a KMM App using the "KMM Application" template in Android Studio. Follow the instructions at the KMM documentation.

2

Open the shared module build.gradle.kts file in the root of the shared module directory of your project. By default, this module is called "shared". Add the io.realm.kotlin dependency to plugins and implementation("io.realm.kotlin:library:0.6.0") to kotlin.sourceSets.commonMain.dependencies:

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
kotlin("native.cocoapods")
id("com.android.library")
id("io.realm.kotlin") version "0.6.0"
}
version = "1.0"
kotlin {
android()
val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
::iosArm64
else
::iosX64
iosTarget("ios") {}
cocoapods {
summary = "Some description for the Shared Module"
homepage = "Link to the Shared Module homepage"
ios.deploymentTarget = "14.4"
frameworkName = "shared"
podfile = project.file("../iosApp/Podfile")
}
sourceSets {
val commonMain by getting {
dependencies {
implementation("io.realm.kotlin:library-base:0.6.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 iosMain by getting
val iosTest by getting
}
}
android {
compileSdkVersion(30)
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdkVersion(16)
targetSdkVersion(30)
}
}
Note
Android Module Dependencies

If you use any part of the SDK, including model classes and query results, inside the Android module (named "androidApp" by default), add the SDK's compile-time dependency:

dependencies {
compileOnly("io.realm.kotlin:library:0.6.0")
}
3

Now that you have updated the Gradle configuration, resolve the dependencies by clicking File > Sync Project with Gradle Files.

4

Once Android Studio has successfully resolved the dependencies, you can get started with Realm. Import the Realm SDK with the following import statement in your .kt files in commonMain:

import io.realm.Realm

Then, click the Run button (a green triangle) to run the application in your compatible emulator.

Give Feedback
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.