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.
Overview
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.
Requisitos previos
Estudio Android versión 1.5.1 o superior.
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).
Instalación
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.
Configuración del proyecto Gradle
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
buildscriptblock that contains arepositoriesblock and adependenciesblock.Añade el repositorio
mavenCentral()al bloquebuildscript.repositories.Añade la dependencia
io.realm:realm-gradle-pluginal bloquebuildscript.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-pluginal bloquebuildscript.dependencies.Añade el repositorio
mavenCentral()al bloqueallprojects.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 }
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-kaptplugin if your application uses KotlinBeneath the
pluginsblock, apply therealm-androidplugin.
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-kaptplugin if your application uses KotlinApply the
realm-androidplugin
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.
Habilitar 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 }
Plataformas compatibles
El Java SDK de Realm le permite compilar aplicaciones para las siguientes plataformas:
Android
Wear OS
Android Automotive OS
Android TV
Android Things