Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crash combining Firebase and Realm - Failed resolution of: Lio/reactivex/Observable;

I am trying to download objects from Firebase and store them in Realm. I am receiving a crash when combining these two excellent libraries.

I've created a very simple demo app which illustrates my problem. It seems Firebase gets upset when creating an object that extends RealmObject.

I can make this work by removing the RealmObject extension but then I have to maintain two identical objects. Over time this becomes complicated and frustrating which is far from ideal.

Firebase:

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("players");

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.d(TAG, "onDataChange: " + dataSnapshot.getChildrenCount());

            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                //***CRASH here*****
                Player player = snapshot.getValue(Player.class);
                Log.d(TAG, "onDataChange: " + player.getFirstname());
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.e(TAG, "onCancelled: ", databaseError.toException());
        }
    });

Player:

public class Player extends RealmObject{
private int age;
private String firstname;

public Player() {
}

//getters and setters
}

Gradle:

compile 'com.google.firebase:firebase-database:11.8.0'
compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.1.6'

Realm:

classpath 'io.realm:realm-gradle-plugin:4.3.1'

Error:

FATAL EXCEPTION: main
Process: com.test.testrealm, PID: 7616
java.lang.NoClassDefFoundError: Failed resolution of: Lio/reactivex/Observable;

  at java.lang.reflect.Executable.getMethodReturnTypeInternal(Native Method)
  at java.lang.reflect.Method.getReturnType(Method.java:141)
  at java.lang.Class.getDeclaredMethods(Class.java:1880)
  at com.google.android.gms.internal.zzelx.<init>(Unknown Source:310)
  at com.google.android.gms.internal.zzelw.zzf(Unknown Source:12)
  at com.google.android.gms.internal.zzelw.zzb(Unknown Source:182)
  at com.google.android.gms.internal.zzelw.zza(Unknown Source:0)
  at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
  at com.test.testrealm.MainActivity$2.onDataChange(MainActivity.java:41)
  at com.google.android.gms.internal.zzegf.zza(Unknown Source:13)
  at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source:2)
  at com.google.android.gms.internal.zzeig.run(Unknown Source:71)
  at android.os.Handler.handleCallback(Handler.java:790)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:164)
  at android.app.ActivityThread.main(ActivityThread.java:6494)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.ClassNotFoundException: Didn't find class "io.reactivex.Observable" on path: DexPathList[[zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/base.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_dependencies_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_resources_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_0_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_1_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_2_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_3_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_4_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_5_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_6_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_7_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_8_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.test.testrealm-Mj0Zh1FV_CGS7fFGuEYfWQ==/lib/arm64, /data/app/com.test.testrealm-Mj0Zh1FV_CGS7fFGuEYfWQ==/base.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_dependencies_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_resources_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_0_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_1_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_2_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_3_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_4_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_5_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_6_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_7_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_8_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_9_apk.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]]
01-10 10:24:06.881 7616-7616/com.test.testrealm E/AndroidRuntime:     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    ... 19 more
Suppressed: java.io.IOException: No original dex files found for dex location /data/app/com.test.testrealm-Mj0Zh1FV_CGS7fFGuEYfWQ==/split_lib_resources_apk.apk
  at dalvik.system.DexFile.openDexFileNative(Native Method)
  at dalvik.system.DexFile.openDexFile(DexFile.java:353)
  at dalvik.system.DexFile.<init>(DexFile.java:100)
  at dalvik.system.DexFile.<init>(DexFile.java:74)
  at dalvik.system.DexPathList.loadDexFile(DexPathList.java:374)
  at dalvik.system.DexPathList.makeDexElements(DexPathList.java:337)
  at dalvik.system.DexPathList.<init>(DexPathList.java:157)
  at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:65)
  at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:64)
  at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:73)
  at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:88)
  at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:69)
  at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:35)
  at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:693)
  at android.app.LoadedApk.getClassLoader(LoadedApk.java:727)
  at android.app.LoadedApk.getResources(LoadedApk.java:954)
  at android.app.ContextImpl.createAppContext(ContextImpl.java:2270)
  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5639)
  at android.app.ActivityThread.-wrap1(Unknown Source:0)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
  at android.os.Handler.dispatchMessage(Handler.java:106)
like image 406
James Avatar asked Jan 10 '18 10:01

James


2 Answers

These kind of class loader issues can be solved in two ways:

1.) either add a dummy class for whatever missing, see this answer for details

2.) add compile "io.reactivex.rxjava2:rxjava:2.1.8" to dependencies

like image 117
EpicPandaForce Avatar answered Oct 12 '22 10:10

EpicPandaForce


implementation "io.reactivex.rxjava2:rxjava:2.1.8

add to dependencies

like image 1
Davis Kazad Avatar answered Oct 12 '22 09:10

Davis Kazad