realm / realm-java

Realm is a mobile database: a replacement for SQLite & ORMs
http://realm.io
Apache License 2.0
11.46k stars 1.75k forks source link

Caused by: java.lang.ClassNotFoundException: io.realm.LocalModuleMediator #4136

Closed timur-st94 closed 7 years ago

timur-st94 commented 7 years ago

I'm getting this error in my project, when I try to use Dagger2

io.realm.exceptions.RealmException: Could not find io.realm.LocalModuleMediator at io.realm.RealmConfiguration.getModuleMediator(RealmConfiguration.java:300) at io.realm.RealmConfiguration.createSchemaMediator(RealmConfiguration.java:274) at io.realm.RealmConfiguration$Builder.build(RealmConfiguration.java:665) at kz.rps.reagent.db.LocalRealmConfig.get(LocalRealmConfig.java:23) at kz.rps.reagent.db.DBController.(DBController.java:15) at kz.rps.reagent.db.SessionInfoController.(SessionInfoController.java:9) at kz.rps.reagent.db.SessionInfoController.getSessionController(SessionInfoController.java:12) at kz.rps.reagent.model.manager.SessionManager.setToken(SessionManager.java:367) at kz.rps.reagent.fragments.check_in.LoginFragment$2.onResponse(LoginFragment.java:113) at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.ClassNotFoundException: io.realm.LocalModuleMediator at java.lang.Class.classForName(Native Method) at java.lang.Class.forName(Class.java:400) at java.lang.Class.forName(Class.java:326) at io.realm.RealmConfiguration.getModuleMediator(RealmConfiguration.java:295) at io.realm.RealmConfiguration.createSchemaMediator(RealmConfiguration.java:274)  at io.realm.RealmConfiguration$Builder.build(RealmConfiguration.java:665)  at kz.rps.reagent.db.LocalRealmConfig.get(LocalRealmConfig.java:23)  at kz.rps.reagent.db.DBController.(DBController.java:15)  at kz.rps.reagent.db.SessionInfoController.(SessionInfoController.java:9)  at kz.rps.reagent.db.SessionInfoController.getSessionController(SessionInfoController.java:12)  at kz.rps.reagent.model.manager.SessionManager.setToken(SessionManager.java:367)  at kz.rps.reagent.fragments.check_in.LoginFragment$2.onResponse(LoginFragment.java:113)  at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)  at android.os.Handler.handleCallback(Handler.java:751)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  Caused by: java.lang.ClassNotFoundException: Didn't find class "io.realm.LocalModuleMediator" on path: DexPathList[[zip file "/data/app/kz.rps.reagent-2/base.apk"],nativeLibraryDirectories=[/data/app/kz.rps.reagent-2/lib/arm64, /data/app/kz.rps.reagent-2/base.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:380) at java.lang.ClassLoader.loadClass(ClassLoader.java:312) at java.lang.Class.classForName(Native Method)  at java.lang.Class.forName(Class.java:400)  at java.lang.Class.forName(Class.java:326)  at io.realm.RealmConfiguration.getModuleMediator(RealmConfiguration.java:295)  at io.realm.RealmConfiguration.createSchemaMediator(RealmConfiguration.java:274)  at io.realm.RealmConfiguration$Builder.build(RealmConfiguration.java:665)  at kz.rps.reagent.db.LocalRealmConfig.get(LocalRealmConfig.java:23)  at kz.rps.reagent.db.DBController.(DBController.java:15)  at kz.rps.reagent.db.SessionInfoController.(SessionInfoController.java:9)  at kz.rps.reagent.db.SessionInfoController.getSessionController(SessionInfoController.java:12)  at kz.rps.reagent.model.manager.SessionManager.setToken(SessionManager.java:367)  at kz.rps.reagent.fragments.check_in.LoginFragment$2.onResponse(LoginFragment.java:113)  at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)  at android.os.Handler.handleCallback(Handler.java:751)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

My RealmConfig:

class LocalRealmConfig {
    private static RealmConfiguration localRealmConfig;

    static RealmConfiguration get() {
        if(localRealmConfig != null) {
            return localRealmConfig;
        }

        localRealmConfig = new RealmConfiguration.Builder()
                .modules(new LocalModule())
                .build();
        return localRealmConfig;
    }
}

My database controller:

class DBController {
    Realm mRealm;

    DBController() {
        RealmConfiguration mRealmConfig = LocalRealmConfig.get();
        try {
            mRealm = Realm.getInstance(mRealmConfig);
        } catch (RealmMigrationNeededException e) {
            Realm.deleteRealm(mRealmConfig);
            mRealm = Realm.getInstance(mRealmConfig);
        }
    }

    void close() {
        mRealm.close();
    }
}

Module class:

@RealmModule(classes = {SessionInfoEntry.class})
public class LocalModule {}

I am using Realm 2.3.0 and dagger 2.8. Please help to resolve this issue.

Zhuinden commented 7 years ago

What gradle plugins are you using and in what order?

Do you use Jack?

timur-st94 commented 7 years ago

build.gradle(Module):

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'realm-android'
apply plugin: 'com.neenbedankt.android-apt'

repositories {
    maven { url 'https://maven.fabric.io/public' }
}

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.1"
    defaultConfig {
        applicationId "..."
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 3
        versionName "1.0.3"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    testCompile 'junit:junit:4.12'
    apt "com.google.dagger:dagger-compiler:2.8"
    compile 'com.android.support:design:25.0.1'
    compile 'com.google.dagger:dagger:2.8'
    provided 'javax.annotation:jsr250-api:1.0'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.okhttp3:okhttp:3.4.2'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.google.android.gms:play-services-maps:9.4.0'
    compile 'com.android.support:leanback-v17:25.0.1'
    compile 'com.google.android.gms:play-services:9.4.0'
    compile 'fr.avianey.com.viewpagerindicator:library:2.4.1.1@aar'
    compile 'de.hdodenhof:circleimageview:2.1.0'
    compile('com.crashlytics.sdk.android:crashlytics:2.6.5@aar') {
        transitive = true;
    }
    compile('com.crashlytics.sdk.android:crashlytics-ndk:1.1.5@aar') {
        transitive = true;
    }
}

crashlytics {
    enableNdk true
    androidNdkOut 'src/main/obj'
    androidNdkLibsOut 'src/main/libs'
}

build.gradle(Project):

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.2'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath "io.realm:realm-gradle-plugin:2.3.0"
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
Zhuinden commented 7 years ago

Please apply plugins in this order( https://github.com/realm/realm-java/issues/3827 )

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'realm-android'
timur-st94 commented 7 years ago

If I apply plugins in this order, then compile dagger has an error:

Error:(21, 25) error: cannot find symbol class DaggerDaggerComponent

Zhuinden commented 7 years ago

Then you have some kind of error in your Dagger configuration. A @Provides fields could be missing or something similar. Or the auto-generated class is just not imported.

bmeike commented 7 years ago

@timur-st94 Is there anything else we can do for you? I'd like to close this ticket, if not.

bmeike commented 7 years ago

@timur-st94 Closing this ticket. Please feel absolutely free to re-open it, or to open a new ticket, if there is more that we can help you with!