Search code examples
javaandroidmongodbandroid-studiorealm

While installing mongodb realm, I can't make gradle file. I think the docs are old


I want to use realm.io for android application but I have problems during installation

I used these steps. I think the codes are outdated because it doesn't match my Gradle file but somehow I made it.

here is my build.gradle (Module :app) file

plugins {
    id 'com.android.application'
}

android {
    namespace 'com.example.ig'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.ig"
        minSdk 28
        targetSdk 33
        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_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
dependencies {

    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

//    for MongoDB
    implementation "io.realm:realm-gradle-plugin:10.16.1"
}

I couldn't add these codes here this

apply plugin: 'realm-android'

and this

realm {
    syncEnabled = true
}

after this stage I wrote this code and this code

Realm.init(this); // context, usually an Activity or Application

app = new App(new AppConfiguration.Builder(appID)
    .build());

and now getting an error message: enter image description here enter image description here

How can I resolve this error message?


Solution

  • I updated my build.gradle (Module :app) file as follows and now the codes are recognized by Android Studio.

    // START[for MongoDB Realm.io]
    buildscript {
        dependencies {
            classpath "io.realm:realm-gradle-plugin:10.11.1"
        }
    }
    // FINISH[for MongoDB Realm.io]
    
    plugins {
        id 'com.android.application'
    }
    
    // START[for MongoDB Realm.io]
    apply plugin: "realm-android"
    
    realm {
        syncEnabled = true
    }
    // FINISH[for MongoDB Realm.io]
    
    android {
        namespace 'com.example.ig'
        compileSdk 33
    
        defaultConfig {
            applicationId "com.example.ig"
            minSdk 28
            targetSdk 33
            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_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }
    dependencies {
    
        implementation 'androidx.appcompat:appcompat:1.6.1'
        implementation 'com.google.android.material:material:1.9.0'
        implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
        testImplementation 'junit:junit:4.13.2'
        androidTestImplementation 'androidx.test.ext:junit:1.1.5'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    }
    

    I added the buildscript block as Jay said