Search code examples
androidkotlinrealm

Error building project io.realm.DefaultRealmModule when switching to plugin android gradle 3.3


Android project has two modules that use Realm.

When assembling with

buildscript {
     dependencies {
          classpath 'com.android.tools.build:gradle:3.2.0'
          classpath "io.realm:realm-gradle-plugin:5.7.0"
     }
}

no problems.

However, when building with gradle: 3.3.0, the error "Error: Program type already present: io.realm.DefaultRealmModule" appears.

Tell me what is the reason and how to eliminate it.

Module 1

@RealmModule(classes = [
ResponseMetadata::class
 ])
class CourierModule

RealmConfiguration.Builder()
            .name("module1.realm")
            .schemaVersion(version)
            .modules(CourierModule())
            .deleteRealmIfMigrationNeeded()
            .build()

Module 2

@RealmModule(classes = [
CookiesAuthInstaller::class
])
class InstallerModule

RealmConfiguration.Builder()
            .name("module2.realm")
            .schemaVersion(version)
            .modules(InstallerModule())
            .deleteRealmIfMigrationNeeded()
            .build()

Solution

  • When you say android modules i assume you are referring these as library with plugin

     apply plugin: 'com.android.library'
    

    If this is the case, you need to modify your custom realm module declarations as follows

        @RealmModule(library = true, classes = [CookiesAuthInstaller::class])
        class InstallerModule
    
        RealmConfiguration.Builder()
                .name("module2.realm")
                .schemaVersion(version)
                .modules(InstallerModule())
                .deleteRealmIfMigrationNeeded()
                .build()
    

    Then for your second module:

       @RealmModule(library = true, classes = [ResponseMetadata::class])
       class CourierModule
    
       RealmConfiguration.Builder()
            .name("module1.realm")
            .schemaVersion(version)
            .modules(CourierModule())
            .deleteRealmIfMigrationNeeded()
            .build()
    

    Lastly don't forget to run clean and cleanBuildCache from the gradle task list on the right pane of your IDE. This prevents the DefaultRealmModule to be created during build time except the one used in your app module or the end user of your library that is using Realm DB. hope this helps.