Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /
SDK de Dispositivo Atlas

Instala Realm - Java SDK

Nota

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.

Obtén más información sobre cómo Migrar del SDK de Java al SDK de Kotlin.

Nota

Las nuevas aplicaciones SDK de Java no pueden usar RealmAny

Nuevo Servicios de aplicaciones Las aplicaciones no podrán sincronizar modelos de datos con propiedades de tipo RealmAny.

Esta página detalla cómo instalar Realm usando el SDK de Java en su proyecto y comenzar.

Puede usar varios SDK en su proyecto. Dado que el Java SDK ya no recibe nuevos desarrollos, esto resulta útil si quiere usar nuevas funcionalidades en su aplicación. Por ejemplo, puedes agregar la funcionalidad Ingesta de datos de Atlas Device Sync a tu aplicación usando el SDK de Kotlin y continuar usando Java para todo lo demás.

Realm solo admite el sistema de compilación Gradle. Sigue estos pasos para añadir el Java SDK de Realm a tu proyecto.

Nota

ProGuard

Dado que Realm proporciona una configuración de ProGuard como parte de la biblioteca de Realm, no es necesario agregar ninguna regla específica de Realm a su configuración de ProGuard.

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

El Java SDK aún no ofrece soporte para la sintaxis del plugin de Gradle. Afortunadamente, todavía puedes agregar el SDK a proyectos que usan esta sintaxis.

  • Add a buildscript block that contains a repositories block and a dependencies block.

  • Añade el repositorio mavenCentral() al bloque buildscript.repositories.

  • Añade la dependencia io.realm:realm-gradle-plugin al bloque buildscript.dependencies.

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
}
  • Añade la dependencia io.realm:realm-gradle-plugin al bloque buildscript.dependencies.

  • Añade el repositorio mavenCentral() al bloque allprojects.repositories.

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
}

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 Kotlin

  • Beneath the plugins block, apply the realm-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 Kotlin

  • Apply 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"
}

Después de actualizar los archivos build.gradle, resuelva las dependencias haciendo clic en File > Sync Project with Gradle Files.

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
}

El Java SDK de Realm le permite compilar aplicaciones para las siguientes plataformas:

  • Android

  • Wear OS

  • Android Automotive OS

  • Android TV

  • Android Things

Next

Bienvenido a la Docs de Atlas Device SDK

En esta página