Realm crashes when there is no model classes
Hi guys,
Getting this problem. Looks like class generation doesn't work. Any ideas?
Goals: Run project with included Realm and one call to it Realm.getInstance(context).
Expected results: It runs.
Actual results: It crashes with the exception: io.realm.exceptions.RealmException: Could not find io.realm.RealmJsonImpl
Steps to reproduce:
- Create empty Android project in Android Studio.
- Include Realm in Gradle dependencies by adding
compile 'io.realm:realm-android:0.79.0' - Call
Realm.getInstance(this)inonCreate()of the MainActivity. - Run the project.
- Get crash with the exception io.realm.exceptions.RealmException: Could not find io.realm.RealmJsonImpl
Code sample that highlights the issue (link to full Android Studio projects that we can compile ourselves are ideal): https://dl.dropboxusercontent.com/u/412096/RealmTest.zip
Version of Realm/Android Studio/OS: 0.79.0 / 1.1 RC1 / OS X 10.10.2
Stacktrace:
02-17 08:28:34.741 2354-2354/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.realmtest/com.realmtest.MainActivity}: io.realm.exceptions.RealmException: Could not find io.realm.RealmJsonImpl
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: io.realm.exceptions.RealmException: Could not find io.realm.RealmJsonImpl
at io.realm.Realm.getRealmJson(Realm.java:263)
at io.realm.Realm.<init>(Realm.java:161)
at io.realm.Realm.createAndValidate(Realm.java:516)
at io.realm.Realm.create(Realm.java:480)
at io.realm.Realm.getInstance(Realm.java:397)
at io.realm.Realm.getInstance(Realm.java:359)
at io.realm.Realm.getInstance(Realm.java:340)
at com.realmtest.MainActivity.onCreate(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: io.realm.RealmJsonImpl
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:204)
at java.lang.Class.forName(Class.java:169)
at io.realm.Realm.getRealmJson(Realm.java:258)
at io.realm.Realm.<init>(Realm.java:161)
at io.realm.Realm.createAndValidate(Realm.java:516)
at io.realm.Realm.create(Realm.java:480)
at io.realm.Realm.getInstance(Realm.java:397)
at io.realm.Realm.getInstance(Realm.java:359)
at io.realm.Realm.getInstance(Realm.java:340)
at com.realmtest.MainActivity.onCreate(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: io/realm/RealmJsonImpl
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:204)
at java.lang.Class.forName(Class.java:169)
at io.realm.Realm.getRealmJson(Realm.java:258)
at io.realm.Realm.<init>(Realm.java:161)
at io.realm.Realm.createAndValidate(Realm.java:516)
at io.realm.Realm.create(Realm.java:480)
at io.realm.Realm.getInstance(Realm.java:397)
at io.realm.Realm.getInstance(Realm.java:359)
at io.realm.Realm.getInstance(Realm.java:340)
at com.realmtest.MainActivity.onCreate(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "io.realm.RealmJsonImpl" on path: DexPathList[[zip file "/data/app/com.realmtest-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.realmtest-1, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:204)
at java.lang.Class.forName(Class.java:169)
at io.realm.Realm.getRealmJson(Realm.java:258)
at io.realm.Realm.<init>(Realm.java:161)
at io.realm.Realm.createAndValidate(Realm.java:516)
at io.realm.Realm.create(Realm.java:480)
at io.realm.Realm.getInstance(Realm.java:397)
at io.realm.Realm.getInstance(Realm.java:359)
at io.realm.Realm.getInstance(Realm.java:340)
at com.realmtest.MainActivity.onCreate(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Hello Roberto,
your app does not contain any model classes so that's the problem. We definitely need to make the error message clearer!
Hah, nice one :) Or maybe make it possible to run without the models. Why not.
I think we had that discussion in another issue, but cannot find it. You are right, in either case, this is not the intended behaviour.
I might be doing something radically wrong, but after I added a model class I still get the same exception. Sample Project: https://dl.dropboxusercontent.com/u/412096/RealmTest2.zip
public class User extends RealmObject {
@PrimaryKey
private String token;
private String displayName;
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
}
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Realm realm = Realm.getInstance(this);
realm.beginTransaction();
User user = realm.createObject(User.class);
user.setToken("token1");
user.setDisplayName("display_name1");
realm.commitTransaction();
}
}
Stacktrace:
02-27 05:50:09.672 8893-8893/com.the_roberto.realm E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.the_roberto.realm/com.the_roberto.realm.MainActivity}: io.realm.exceptions.RealmException: Could not find io.realm.RealmJsonImpl
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: io.realm.exceptions.RealmException: Could not find io.realm.RealmJsonImpl
at io.realm.Realm.getRealmJson(Realm.java:263)
at io.realm.Realm.<init>(Realm.java:161)
at io.realm.Realm.createAndValidate(Realm.java:516)
at io.realm.Realm.create(Realm.java:480)
at io.realm.Realm.getInstance(Realm.java:397)
at io.realm.Realm.getInstance(Realm.java:359)
at io.realm.Realm.getInstance(Realm.java:340)
at com.the_roberto.realm.MainActivity.onCreate(MainActivity.java:15)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: io.realm.RealmJsonImpl
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:204)
at java.lang.Class.forName(Class.java:169)
at io.realm.Realm.getRealmJson(Realm.java:258)
at io.realm.Realm.<init>(Realm.java:161)
at io.realm.Realm.createAndValidate(Realm.java:516)
at io.realm.Realm.create(Realm.java:480)
at io.realm.Realm.getInstance(Realm.java:397)
at io.realm.Realm.getInstance(Realm.java:359)
at io.realm.Realm.getInstance(Realm.java:340)
at com.the_roberto.realm.MainActivity.onCreate(MainActivity.java:15)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: io/realm/RealmJsonImpl
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:204)
at java.lang.Class.forName(Class.java:169)
at io.realm.Realm.getRealmJson(Realm.java:258)
at io.realm.Realm.<init>(Realm.java:161)
at io.realm.Realm.createAndValidate(Realm.java:516)
at io.realm.Realm.create(Realm.java:480)
at io.realm.Realm.getInstance(Realm.java:397)
at io.realm.Realm.getInstance(Realm.java:359)
at io.realm.Realm.getInstance(Realm.java:340)
at com.the_roberto.realm.MainActivity.onCreate(MainActivity.java:15)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "io.realm.RealmJsonImpl" on path: DexPathList[[zip file "/data/app/com.the_roberto.realm-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.the_roberto.realm-2, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:204)
at java.lang.Class.forName(Class.java:169)
at io.realm.Realm.getRealmJson(Realm.java:258)
at io.realm.Realm.<init>(Realm.java:161)
at io.realm.Realm.createAndValidate(Realm.java:516)
at io.realm.Realm.create(Realm.java:480)
at io.realm.Realm.getInstance(Realm.java:397)
at io.realm.Realm.getInstance(Realm.java:359)
at io.realm.Realm.getInstance(Realm.java:340)
at com.the_roberto.realm.MainActivity.onCreate(MainActivity.java:15)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Hi @RobertoArtiles I tried to import your project and it worked fine. So I would assume you either need to clean it and rebuild or perhaps try to import it again into Android Studio.
@cmelchior Yep. Clean build helped. Thanks!
I have the same problem, for each notice found in the compile logs (not all methods without variable create problems) I solved temporarily by defining a variable with the same name of the method and declaring it with @Ignore.
public class Category extends RealmObject {
@Ignore
private String label;
public String getLabel() {
// do something
}
}
This has been fixed when we merged https://github.com/realm/realm-java/pull/929 and should be available in 0.81-SNAPSHOT