Search code examples
androidkotlingradlerealm

Adding Realm Kotlin as a dependency in an Android Library


I'm trying to add Realm Kotlin to an Android Library module by following the steps outlined here https://www.mongodb.com/docs/realm/sdk/kotlin/install/#add-the-sdk-to-the-project. However I see that only the Android option is available (as in Android app, not Android library). The library will have only the module build.gradle file without the project build.gradle.

The idea is to simply have this library contain the Realm objects and use it in multiple apps, without the need for those apps to declare any dependency on realm, just my library.

I tried creating a demo app project and inside adding a module Android Library with the following build.gradle:

//build.gradle.kts (Module :lib)

plugins {
    id("com.android.library")
    id("org.jetbrains.kotlin.android")
    id("io.realm.kotlin") version "1.14.0"
}

android {
    namespace = "com.ba.lib"
    compileSdk = 34

    defaultConfig {
        minSdk = 33

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles("consumer-rules.pro")
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

dependencies {

    implementation("androidx.core:core-ktx:1.12.0")
    implementation("androidx.appcompat:appcompat:1.6.1")
    implementation("com.google.android.material:material:1.11.0")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")

    //realm
    implementation("io.realm.kotlin:library-base:1.11.0")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.0")
}

Created a dummy realm object in the library files:

class SomeObject : RealmObject {
    @PrimaryKey
    var id: String = ""
    var name: String = ""

}
var realm = Realm.open(RealmConfiguration.create(schema = setOf(
    SomeObject::class,
)))

And then in the main app when I try to access the object it gives the compile error:

Cannot access 'io.realm.kotlin.types.RealmObject' which is a supertype of 'com.ba.lib.SomeObject'. Check your module classpath for missing or conflicting dependencies.

enter image description here

enter image description here

If anyone has dealt with this issue before and solved it I would appreciate the help. Thanks!


Solution

  • The reason is that the Realm dependency you added is not transitively accessible from the app module, so you cannot interact with Realm-related entities.

    For this purpose, you can include it in the mylibrary module with the api keyword instead of implementation:

    api("io.realm.kotlin:library-base:1.11.0")