Navigation

Install Realm - Android SDK

  • Android Studio version 1.5.1 or higher.
  • JDK version 7.0 or higher.
  • A recent version of Google's Android SDK
  • Android API Level 16 or higher (Android 4.1 and above).

MongoDB Realm only supports the Gradle build system. Follow these steps to add the MongoDB Realm Android SDK to your project.

Info With Circle IconCreated with Sketch.Note
A note on ProGuard

Because MongoDB Realm provides a ProGuard configuration as part of the MongoDB Realm library, you do not need to add any MongoDB Realm-specific rules to your ProGuard configuration.

1

Create or open your project in Android Studio. Select the "Project" view in the top-left panel:

The project view is in the top-left panel.

Open the project-level build.gradle file:

Location of the project-level build.gradle file.

Paste the Realm classpath into the buildscript.dependencies block:

classpath "io.realm:realm-gradle-plugin:10.3.1"

When done, your project-level build.gradle should look something like this:

buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:3.5.1"
classpath "io.realm:realm-gradle-plugin:10.3.1"
}
}
allprojects {
repositories {
google()
jcenter()
}
dependencies {
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
2

Expand the app folder on the "Project" view in the top-left panel to reveal the application files. Open the application-level build.gradle file in this folder:

Location of the application-level build.gradle file.

If your application uses the Kotlin programming language, you need to apply the kotlin-kapt plugin:

apply plugin: 'kotlin-kapt'

Apply the realm-android plugin near the top of your application level build.gradle file:

apply plugin: 'realm-android'
Info With Circle IconCreated with Sketch.Note
Application Order Matters

Application order matters for android plugins. Because the realm-android plugin depends upon the kotlin-kapt plugin for applications written in Kotlin, you must apply the realm-android plugin after the kotlin-kapt plugin if your application uses the Kotlin programming language.

If your application uses sync, you must enable the sync features of the Android SDK here:

realm {
syncEnabled = true
}

When done, your application-level build.gradle file should look something like this:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.mongodb.realmsnippets"
minSdkVersion 21
targetSdkVersion 29
}
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}
realm {
syncEnabled = true
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "androidx.appcompat:appcompat:1.1.0"
implementation "androidx.core:core-ktx:1.2.0"
}
Info With Circle IconCreated with Sketch.Note
Enabling Sync is Optional

The Realm Android SDK includes functionality that allows you to sync data between mobile applications and a MongoDB Realm backend. If your application only uses local Realm Database, you can omit the following snippet from your application level build.gradle file to disable that functionality at the SDK level:

realm {
syncEnabled = true
}
3

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

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

import io.realm.Realm
Give Feedback