Diederik
Diederik

Reputation: 6488

Build realm sync project with new Android (Java) project

I'm having trouble getting my realm dependencies right for an Android Java project.

I started a new project to try and isolate the problem.

The project is using Android Gradle Plugin Version 7.1.0 and Gradle version 7.2 and Android studio 2021.1.1

I followed the instructions for installing realm in the project. And it builds and runs when I just call

Realm.init(context);

When I try to initialize a synced realm however (buy following the getting Sync quick start guide) I can't seem to get the right imports for the App and AppConfiguration classes.

The build fails with

error: cannot find symbol
        App app = new App(new AppConfiguration.Builder("XXXYYYZZZ")

In my older project I see the imports were

import io.realm.mongodb.App;
import io.realm.mongodb.AppConfiguration;

But that doesn't seem to be found by Android studio.

(The RealmTransformer part of the import io.realm.transformer.RealmTransformer line is in read, which is probably not a great sign, but I can't see any warnings in the IDE about it, and it seems to build fine)

Application level gradle file as below

I've tried various combinations of api vs implementation and kapt vs annotationProcessor. Also did "invalidate caches and restart". But nothing worked.

buildscript {
    ext.kotlin_version = '1.5.21'
    ext.realm_version = '10.10.0'
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath "io.realm:realm-transformer:$realm_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

plugins {
    id 'com.android.application'
}

//apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

import io.realm.transformer.RealmTransformer
android.registerTransform(new RealmTransformer(project))


android {
    compileSdk 31

    defaultConfig {
        applicationId "guru.diederik.myapplication"
        minSdk 21
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.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 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:2.0.4'

    api "io.realm:realm-annotations:$realm_version"
    api "io.realm:realm-android-library:$realm_version"
    api "io.realm:realm-android-kotlin-extensions:$realm_version"
    kapt "io.realm:realm-annotations-processor:$realm_version"

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

}

Upvotes: 0

Views: 2355

Answers (1)

Diederik
Diederik

Reputation: 6488

It seems that the install instructions have been updated. Applying them have now worked, and the project builds.

Project level gradle file that works.

buildscript {
    ext.realm_version = '10.10.1'
    dependencies {
        classpath "io.realm:realm-gradle-plugin:$realm_version"
    }
}

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.1.0' apply false
    id 'com.android.library' version '7.1.0' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

And app level gradle file that works

buildscript {
    ext.kotlin_version = '1.6.10'
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath "io.realm:realm-transformer:$realm_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

plugins {
    id 'com.android.application'
}

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'

android {
    compileSdk 31

    defaultConfig {
        applicationId "guru.diederik.myapplication"
        minSdk 21
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.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
    }
}

realm {
    syncEnabled = true
}

dependencies {

    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:2.0.4'

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

}

Upvotes: 4

Related Questions