Realm-java: App crashing 4.X devices. java.lang.NoClassDefFoundError: io.realm.internal.RealmCore

Created on 19 May 2016  Â·  41Comments  Â·  Source: realm/realm-java

App crashing while initializing Realm configuration. Getting the error

05-19 10:12:49.810 10049-10067/? E/AndroidRuntime: FATAL EXCEPTION: Thread-341 Process: PID: 10049 java.lang.NoClassDefFoundError: io.realm.internal.RealmCore at io.realm.RealmConfiguration$Builder.<init>(RealmConfiguration.java:352)

I'm using realm version io.realm:realm-gradle-plugin:0.90.1

and build tools version buildToolsVersion '23.0.3'

T-Help

Most helpful comment

Hi @leoouc

To confirm that it's an issue of multides,
Would you try following configuration?

  • create following file named multi-dex-keep.txt in the same directory as the proguard-rules.pro
-keep public class io.realm.internal.RealmCore  {
 *;
}
  • add following line to build.gradle right after multiDexEnabled true
multiDexKeepProguard file('multi-dex-keep.txt')

All 41 comments

Hello @knsarath,

In order to help you we need more informations than the ones you provide. We have a template you can use here: https://github.com/realm/realm-java/blob/master/.github/ISSUE_TEMPLATE.md

Also, since this looks like a build issue, it would be nice if you provided your build.gradle files as well.

I came to the same problem, android 4.4.4,realm 0.90.1,this is my build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'realm-android'
android {
    compileSdkVersion 23
    buildToolsVersion '23.0.3'
    useLibrary 'org.apache.http.legacy'
    defaultConfig {
        applicationId "com.bcgogo.lyx"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName '1.0.9'
        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
    lintOptions {
        abortOnError false
    }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'

    compile project(':pull_to_refresh_library')
    compile project(':vitamio')
    compile project(':mylibrary')

    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'io.netty:netty-all:4.0.36.Final'
    compile 'com.google.protobuf:protobuf-java:2.6.1'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.7.5'
    compile 'com.mcxiaoke.volley:library:1.0.19'
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'org.greenrobot:eventbus:3.0.0'
    compile 'com.android.support:support-v4:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'org.apache.httpcomponents:httpcore:4.4.2'
}

I can see you have multidex enabled. I wonder if it's a case of multidex being naughty and not placing classes in the right dex. Would you be able to stay below the method count limit if you enabled proguard?

@emanuelez I enable proguard,but still exceed the limit,so how can i do?

Hi @leoouc

To confirm that it's an issue of multides,
Would you try following configuration?

  • create following file named multi-dex-keep.txt in the same directory as the proguard-rules.pro
-keep public class io.realm.internal.RealmCore  {
 *;
}
  • add following line to build.gradle right after multiDexEnabled true
multiDexKeepProguard file('multi-dex-keep.txt')

@zaki50 I tried multi-dex-keep.txt as follows, realm works, but another 3rd jar out, multidex digs a big hole for me /(ã„’oã„’)/~~
-keep public class io.realm.* {
*;
}

I have the same problem!

The Realm 0.9x has this problem, in Android 4.x app is crashed on the build of Realm instance!

After hours trying to solve the problem I removed the library google play service in app/build.gradle (compile 'com.google.android.gms: play-services: 8.4.0') and put only the ones I'm using in my project and Realm work again in 4.x versions of Android :+1:

In that case, it really was a multidexing issue.

I have the same issue. But I need to keep

multiDexEnabled true

How can I fixe this issue ?

Working as wanted on 5.x and above.

@bunjix has your app extends MulitDexApplication?

No. Should I expends MultiDexApplication ? I thought I just need to add the multiDexEnabled flag on my gradle flag.

Huh? You have to call Multidex.install() from the application o-o

Note: If your app uses extends the Application class, 
   you can override the attachBaseContext() method 
   and call MultiDex.install(this) to enable multidex.

or set Multidex application from the Manifest (if you don't extend Application) o-o

<application
    ...
    android:name="android.support.multidex.MultiDexApplication">

Ok thanks. Now it works.
But at runtime I got some exception like

MyClass.java is not part of the schema for this Realm schema

Just found this new section of the doc about this error with Kotlin.

@bunjix Does adding @RealmClass annotation to model classes solve your problem?

@zaki50 In fact it depends. Sometimes it works and sometimes not.
When it does not work

class com.blabla.User is not part of the schema for this Realm.

I have to clean the project multiple times, then rebuild it. And then it works (sometimes).

@bunjix Which do you place apply plugin: 'realm-android' or `apply plugin: 'kotlin-android' first?

If you place apply plugin: 'realm-android' first, please move it after apply plugin: 'kotlin-android'.

I close the issue due to lack of activity. @bunjix if the suggests above don't solve your issue, please either reopen or create a new issue.

Facing the same issue.
Is there a way out without making the application Multidex ?

multiDexKeepProguard file('multi-dex-keep.txt') doesn't work?

Multidex will work. But is that only the solution? Because I am making a android library project which has realm plugin. And I can't force the applications that integrate my library to make it MultidexApp

Well if you have multiDexEnabled true then you need to make it multi dex application

but this should not be a problem since Realm.init(context) was introduced.

btw http://stackoverflow.com/questions/43872317/java-lang-noclassdeffounderror-io-realm-realm-on-android-4-x-devices#comment74781884_43872317

That sounds different issue from this.
Reducing method count (by hand or by something like Proguard) is the only way to avoid multidex.

i had the same problem and i solve creating a PrefsApplication, like that

    public class PrefsApplication extends Application {
         @Override
         public void onCreate() {
               super.onCreate();

              MultiDex.install(this);

             Realm.init(this);
       }
    }

in AndroidManifest.xml

    <application android:name=".code.util.PrefsApplication" ....

I'm facing same issue. is anyone found the solution.
I enabled multidex correctly but not working

thanks @Zhuinden fixed my project
issue is related to alpha version of build gradle

i still have the issue even using https://github.com/realm/realm-java/issues/2837#issuecomment-355956410 and https://github.com/realm/realm-java/issues/2837#issuecomment-318484127

public class PontotelApplication extends Application {
@Override
    public void onCreate() {
        super.onCreate();
        MultiDex.install(this);
        Realm.init(this);
...

project gradle:

        classpath('io.realm:realm-gradle-plugin:4.3.1') {
            exclude group: 'com.android.tools.build', module: 'gradle'
        }

app gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'realm-android'


...
 defaultConfig {
...
        multiDexEnabled true
...
    }
   java.lang.NoClassDefFoundError: io.realm.Realm
      at lar21.pontotel.PontotelApplication.initRealm(PontotelApplication.java:137)
      at lar21.pontotel.PontotelApplication.onCreate(PontotelApplication.java:77)
      at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007)
      at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4541)
      at android.app.ActivityThread.access$1500(ActivityThread.java:151)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1381)
      at android.os.Handler.dispatchMessage(Handler.java:110)
      at android.os.Looper.loop(Looper.java:193)
      at android.app.ActivityThread.main(ActivityThread.java:5299)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:515)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
      at dalvik.system.NativeStart.main(Native Method)

@defaultbr that's because you need to call MultiDex.install(this); in attachBaseContext() and not in onCreate().

Read the relevant guide https://developer.android.com/studio/build/multidex.html

public class MyApplication extends SomeOtherApplication {
  @Override
  protected void attachBaseContext(Context base) {
     super.attachBaseContext(base);
     MultiDex.install(this);
  }
}

@Zhuinden changed and still have the same crash =(

    @Override
    public void onCreate() {
        super.onCreate();
        initRealm();
        appComponent.inject(this);
    }

    @Override
    protected void attachBaseContext(Context context) {
        super.attachBaseContext(context);
        MultiDex.install(this);
    }

OBS: its really only for android 4.x

then you can check following possible problems

1.) instant run is screwing up so do clean+rebuild

2.) you might have another version of Realm on classpath for example compile 'io.realm.realm-android:0.87.5' or so which you should remove from dependencies

@Zhuinden no, i checked everything

Android 5.0 Works
Android 7.0 Works

Android 4.x crash

ouch

@defaultbr okay that is weird, does it by chance work with Realm 4.2.0?

@Zhuinden i will try and post here again

@defaultbr please try realm 4.3.2

@Zhuinden 4.2.0 and 4.3.2 still same error

the strange thing about 4.2.0 is that i lost the .sort and .addOnChangeListener methods, they doesnt exist (documentation say that 4.2.0 has )

@Zhuinden i solved removing some dependencies and reducing the methods < 40.000 and disabling MULTIDEX

would be good to choose to keep Realm things in the first dex (dont know if its possible and if its extract in this order)

The error continues with Realm 4.3.3 with alpha versions of build gradle (like alpha08, 09)

But when changing gradle to beta versión:
classpath 'com.android.tools.build:gradle:3.0.1'
The error did not come back (_or maybe, the solution was to clean the project a hundred times xD_)

By the way, it was not solved with any of the comments above, neither with MultiDex correctly installed

I got the same error so I decided to move the component injector just after the MultiDex.install(this).

@Override
public void onCreate() {
    super.onCreate();
    //initializeInjector();
    //this.initializeLeakDetection();
}

@Override
public void attachBaseContext(Context base){
    super.attachBaseContext(base);
    MultiDex.install(this);
    initializeInjector();
}

private void initializeInjector() {
    log = new L();
    mApplicationComponent =
            DaggerApplicationComponent.builder()
                    .applicationModule(new ApplicationModule(this, log))
                    .netModule(new NetModule(this))
                    .realmModule(new RealmModule(this))
                    .build();
}

Now this works with Android 4.4.2 or greater.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wezley98 picture wezley98  Â·  3Comments

mithrann picture mithrann  Â·  3Comments

AlbertVilaCalvo picture AlbertVilaCalvo  Â·  3Comments

Frasprite picture Frasprite  Â·  3Comments

harshvishu picture harshvishu  Â·  3Comments