Install Realm - Java SDK
On this page
Note
Java SDK in Maintenance Mode
The Java SDK is in best-effort maintenance mode and no longer receives new development or non-critical bug fixes. To develop your app with new features, use the Kotlin SDK. You can use the Java SDK with the Kotlin SDK in the same project.
Learn more about how to Migrate from the Java SDK to the Kotlin SDK.
Note
New Java SDK apps cannot use RealmAny
New App Services Apps
will not be able to synchronize data models with properties of type RealmAny
.
Overview
This page details how to install Realm using the Java SDK in your project and get started.
You can use multiple SDKs in your project. Because the Java SDK is no longer receiving new development, this is useful if you want to use new features in your app. For example, you can add Atlas Device Sync's Data Ingest feature to your app using the Kotlin SDK while continuing to use Java for everything else.
Prerequisites
Android Studio version 1.5.1 or higher.
Java Development Kit (JDK) 11 or higher.
An emulated or hardware Android device for testing.
Android API Level 16 or higher (Android 4.1 and above).
Installation
Realm only supports the Gradle build system. Follow these steps to add the Realm Java SDK to your project.
Note
ProGuard
Because Realm provides a ProGuard configuration as part of the Realm library, you do not need to add any Realm-specific rules to your ProGuard configuration.
Project Gradle Configuration
To add local-only Realm to your application, make
the following changes to your project-level Gradle build
file, typically found at <project>/build.gradle
:
Tip
The Java SDK does not yet support the Gradle Plugin syntax. Fortunately, you can still add the SDK to projects that use this syntax.
Add a
buildscript
block that contains arepositories
block and adependencies
block.Add the
mavenCentral()
repository to thebuildscript.repositories
block.Add the
io.realm:realm-gradle-plugin
dependency to thebuildscript.dependencies
block.
buildscript { repositories { mavenCentral() } dependencies { classpath "io.realm:realm-gradle-plugin:10.18.0" } } plugins { id 'com.android.application' version '7.1.2' apply false id 'com.android.library' version '7.1.2' apply false id 'org.jetbrains.kotlin.android' version '1.6.10' apply false id "org.jetbrains.kotlin.kapt" version "1.6.20" apply false } task clean(type: Delete) { delete rootProject.buildDir }
Add the
io.realm:realm-gradle-plugin
dependency to thebuildscript.dependencies
block.Add the
mavenCentral()
repository to theallprojects.repositories
block.
buildscript { repositories { google() } dependencies { classpath "com.android.tools.build:gradle:3.5.1" classpath "io.realm:realm-gradle-plugin:10.18.0" } } allprojects { repositories { google() mavenCentral() } dependencies { } } task clean(type: Delete) { delete rootProject.buildDir }
Application Module Gradle Configuration
Then, make the following changes to your application-level
Gradle build file, typically found at <project>/app/build.gradle
:
Apply the
kotlin-kapt
plugin if your application uses KotlinBeneath the
plugins
block, apply therealm-android
plugin.
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'org.jetbrains.kotlin.kapt' } apply plugin: "realm-android" android { compileSdk 31 defaultConfig { applicationId "com.mongodb.example-realm-application" minSdk 28 targetSdk 31 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11 } kotlinOptions { jvmTarget = '11' } } dependencies { implementation 'io.realm:realm-gradle-plugin:10.10.1' implementation 'androidx.core:core-ktx:1.7.0' implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'com.google.android.material:material:1.5.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.3' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' }
Apply the
kotlin-kapt
plugin if your application uses KotlinApply the
realm-android
plugin
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' apply plugin: 'realm-android' android { compileSdkVersion 31 buildToolsVersion "29.0.2" defaultConfig { applicationId "com.mongodb.example-realm-application" minSdkVersion 28 targetSdkVersion 31 } compileOptions { sourceCompatibility 1.11 targetCompatibility 1.11 } kotlinOptions { jvmTarget = '11' } } 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" }
After updating the build.gradle
files, resolve the dependencies by
clicking File > Sync Project with Gradle Files.
Enable Atlas Device Sync
To use Atlas Device Sync in your application, create a realm
block in your
application module build.gradle
. Within this block, set syncEnabled
to true
.
realm { syncEnabled = true }
Supported Platforms
Realm's Java SDK enables you to build apps for the following platforms:
Android
Wear OS
Android Automotive OS
Android TV
Android Things