Ashish Sahu
Ashish Sahu

Reputation: 1558

Realm along with Android library project

I have library project which contain some dependencies like fastadapter,nanotask, realm etc. I'm not able to get it worked along with main application. Somehow I need realm on library project and on main application too.

apply plugin: 'realm-android'

Build error:

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException:
com.android.ide.common.process.ProcessException:
java.util.concurrent.ExecutionException: com.android.dex.DexException:
Multiple dex files define Lio/realm/DefaultRealmModule;

If I'm apply plugin only with library project than there is no more building error. But getting this error while using realm.

Task is not part of the schema for this Realm.
                     at io.realm.internal.RealmProxyMediator.getMissingProxyClassException(RealmProxyMediator.java:242)
                     at io.realm.DefaultRealmModuleMediator.getTableName(DefaultRealmModuleMediator.java:107)
                     at io.realm.RealmSchema.getTable(RealmSchema.java:295)
                     at io.realm.Realm.checkHasPrimaryKey(Realm.java:1530)
                     at io.realm.Realm.copyToRealmOrUpdate(Realm.java:952)

Here is library gradle

Upvotes: 1

Views: 1135

Answers (1)

Christian Melchior
Christian Melchior

Reputation: 20126

If Realm is used in a library project, you must define a RealmModule with your schema. Otherwise, it will conflict with the schema being generated for the app. This is where the multiple DefaultRealmModule error comes from. You can read more about it here: https://realm.io/docs/java/latest/#sharing-schemas.

Specifically, it means that your RealmConfiguration in the library must look something like this:

@RealmModule(library = true, allClasses = true)
public class MyLibraryModule() {

}

RealmConfiguration config = new RealmConfiguration.Builder()
  .modules(new MyLibraryModule())
  .build();

Upvotes: 3

Related Questions