Realm-java: Realm proguard issue with @RealmModule

Created on 17 Jul 2015  Â·  21Comments  Â·  Source: realm/realm-java

I have a project with a single RealmModule annotated with @RealmModule. Build goes thru fine, however during run time, I get this following error:

Caused by: java.lang.IllegalArgumentException: java.lang.Class is not a RealmModule. Add @RealmModule to the class definition.
at io.realm.RealmConfiguration$Builder.checkModule(RealmConfiguration.java:397)
at io.realm.RealmConfiguration$Builder.addModule(RealmConfiguration.java:360)
at io.realm.RealmConfiguration$Builder.setModules(RealmConfiguration.java:348)

Has anyone seen this?

I have the following in my proguard config file.

keep realm classes

-keepnames public class * extends io.realm.RealmObject
-keepattributes _Annotation_
-keep @io.realm.annotations.RealmModule class *
-dontwarn javax.*
-dontwarn io.realm.
*

Thanks in advance. This issue is driving me nuts. I am getting this same error on another project and am almost 100% sure that this is something to do with proguard, which seems to remove the annotation from the class.

Most helpful comment

I solved it by adding below Realm rules in proguard

-keep @interface io.realm.annotations.RealmModule { ; }
-keep class io.realm.annotations.RealmModule { *; }
-keep class io.realm.annotations.RealmModule
-keep @io.realm.annotations.RealmModule class *
-keep class io.realm.internal.Keep
-keep @io.realm.internal.Keep class *
-dontwarn javax.
*
-dontwarn io.realm.**

// Additionally you need to keep your Realm Model classes as well: For example:
//# -keep class com.yourcompany.realm.** { *; }

All 21 comments

Doc for proguard updated
Try this to see if it helps:

-keep class io.realm.annotations.RealmModule
-keep @io.realm.annotations.RealmModule class *
-dontwarn javax.**
-dontwarn io.realm.**

BTW, Does it run fine without proguard?

I am a little confused now. The debug build works fine. However, the release build fails at runtime. I disabled proguard(minifyEnabled false) and ran the clean build and deployed again. No change, same message. So I am starting to wonder if its proguard or gradle or something else. This is my module class.

package com.customername.android.database.realm.module;

@io.realm.annotations.RealmModule(allClasses = true)
public class ExampleRealmModule {
}

And my prog config has the following:

keep realm classes

-keepnames public class * extends io.realm.RealmObject
-keep @io.realm.annotations.RealmModule class *
-keepattributes _Annotation_
-dontwarn javax.*
-dontwarn io.realm.
*

-keep class io.realm.annotations.RealmModule

Can you compare the build output directory for release and debug? Take a look on the difference between java files there, those are generated by Realm.

And from the log:
Caused by: java.lang.IllegalArgumentException: java.lang.Class is not a RealmModule.
It seems your ExampleRealmModule is optimized out by something.
Are you using android studio?

yep, studio is what i use.

On Thu, Jul 16, 2015 at 10:23 PM, Chen Mulong [email protected]
wrote:

Can you compare the build output directory for release and debug? Take a
look on the difference between java files there, those are generated by
Realm.

And from the log:
Caused by: java.lang.IllegalArgumentException: java.lang.Class is not a
RealmModule.
It seems your ExampleRealmModule is optimized out by something.
Are you using android studio?

—
Reply to this email directly or view it on GitHub
https://github.com/realm/realm-java/issues/1301#issuecomment-122158724.

Thanks & Regards,

Vicson.

_P_ Think Green- please do not print this email unless necessary.

@cmelchior @bmunkholm I have a situation where I have a lib gradle module where I have all my RealmObject classes and the RealmModule defined as well. And there are 3 app gradle modules which depend on this lib module. So when creating the RealmModule in the lib module, do I need to declare that it is a library and prevent the DefaultRealmModule creation?

To throw some more light on my setup. have a 'lib' module which has the realm objects and module declared and the RealmConfiguration is also setup there. I use Dagger to wire up stuff. I have another module which is a slim bootstrapping module on top of Realm. That is where I have my base factory which produces realm instances. I have an abstract method to set the modules on the configuration. And that method is implemented by a class which extends this base factory. This class resides in the 'lib' module. And then I have the app modules, which depend on the lib module. I am unable to get this setup working. I have not set the library =true on the realm module declared in the the lib module as I do want the default module to be generated.

@vvictor10
Why do you need the default module?

Doing the below in the library projects will automatically pick up any RealmObject classes in your library and would function as your "default" module (you would still have to set it manually though).

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

If I dont do that, I get this error.

 Caused by: java.lang.IllegalArgumentException: java.lang.Class is not a RealmModule. Add @RealmModule to the class definition.
        at io.realm.RealmConfiguration$Builder.checkModule(RealmConfiguration.java:397)
        at io.realm.RealmConfiguration$Builder.addModule(RealmConfiguration.java:360)
        at io.realm.RealmConfiguration$Builder.setModules(RealmConfiguration.java:348)

lib module contains the *Module class

@io.realm.annotations.RealmModule(allClasses = true)
public class IkinoRealmModule {
}

lib also contains the IkinoRealmInstanceFactory class(which extends a factory class(which lives in another common realm module, which is application agnostic) which has the meat of things), which sets the module.

@Override
public void setRealmModules(RealmConfiguration.Builder builder) {
    builder.setModules(Realm.getDefaultModule());
}

@vvictor10 Have you solved your issue? Is it possible to share your project with us? Or a simple project which can reproduce this issue would be good enough. to [email protected] .

Thanks!

was this solved ?

I've never seen this since. We used Realm with RealmModule and it worked. Although we did have some proguard rules for it just in case.

For some reason, I save more than 2000 data to realm and its working on debug flavor, but in release, it only give me 1 data.

If you reuse your Realm Models as JSON responses, then you probably don't have @SerializedName annotation. Meaning all your data is parsed wrong. And primary key for your single object is null

No, its actually working on debug mode with same code, release mode just
have minifyEnabled true

On Thu, Jun 28, 2018 at 12:16 PM, Gabor Varadi notifications@github.com
wrote:

If you reuse your Realm Models as JSON responses, then you probably don't
have @serializedname annotation. Meaning all your data is parsed wrong. And
primary key for your single object is null

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/realm/realm-java/issues/1301#issuecomment-400905646,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AG9e9I2FyaonsYaaXCHqlwKe6AaLAc1Gks5uBFiVgaJpZM4FaZD8
.

@ralphgabrielle yes that is exactly the case I am talking about in case you are parsing JSON with GSON but without @SerializedName

I got the same problem when I enabled progaurd else works fine

com.xbx.debug.dao.realm.MySchemaModule is not a RealmModule. Add @RealmModule to the class definition.

My class
@RealmModule(library = true, allClasses = true)
public class MySchemaModule {
}

Any one solved this problem?

Thanks,

@kongsonida
seems that problem solved here:
https://github.com/realm/realm-java/issues/1301#issuecomment-466480217

I solved it by adding below Realm rules in proguard

-keep @interface io.realm.annotations.RealmModule { ; }
-keep class io.realm.annotations.RealmModule { *; }
-keep class io.realm.annotations.RealmModule
-keep @io.realm.annotations.RealmModule class *
-keep class io.realm.internal.Keep
-keep @io.realm.internal.Keep class *
-dontwarn javax.
*
-dontwarn io.realm.**

// Additionally you need to keep your Realm Model classes as well: For example:
//# -keep class com.yourcompany.realm.** { *; }

The comment above did not work for me as it was missing a line and the semi-colons require an asterisk before them. Here is what I have set in my Proguard file and it is working with Minification set to true:

-keep @interface io.realm.annotations.RealmModule { *; }
-keep class io.realm.annotations.RealmModule { *; }
-keep class io.realm.annotations.RealmModule
-keep @io.realm.annotations.RealmModule class *
-keep class io.realm.internal.Keep
-keep @io.realm.internal.Keep class *
-dontwarn javax.
-dontwarn io.realm.**
-keepnames public class * extends io.realm.RealmObject
-keep class * extends io.realm.RealmObject

(That explains why suddenly the text in that comment turned italic)

This works:

-keep class io.realm.annotations.RealmModule { *; }
-keep @interface io.realm.annotations.RealmModule { *; }
-keep @io.realm.annotations.RealmModule class *
-keep class io.realm.internal.Keep
-keep @io.realm.internal.Keep class *
-dontwarn javax.**
-dontwarn io.realm.**

-keepnames public class * extends io.realm.RealmObject
-keep class * extends io.realm.RealmObject
Was this page helpful?
0 / 5 - 0 ratings

Related issues

wyvern610 picture wyvern610  Â·  3Comments

David-Kuper picture David-Kuper  Â·  3Comments

harshvishu picture harshvishu  Â·  3Comments

mithrann picture mithrann  Â·  3Comments

aschrijver picture aschrijver  Â·  3Comments