React Native Android Crash: java.lang.NoClassDefFoundError

Viewed 3143

I just recently launched my React Native Android app into production and a bunch of users are experiencing the following crash:

java.lang.NoClassDefFoundError

enter image description here

I'm not sure how to go about debugging this. I can't seem to reproduce it locally on any of my physical devices, and because it's Java and not JavaScript, I'm a bit unfamiliar. It looks like these users are all on Android 4.x, could that have something to do with it?

If you could help point me in the right direction, I would really appreciate it!

2 Answers

I had the same issue that i was not able to reproduce but i get the following error in a simulator Android 4.4

I'm not sure if it's android vitals that just return a partial error or if it's actually not the same error.

java.lang.RuntimeException:
Unable to get provider com.google.firebase.provider.FirebaseInitProvider: java.lang.ClassNotFoundException
E/AndroidRuntime( 3446):    at android.app.ActivityThread.installProvider(ActivityThread.java:4793)
E/AndroidRuntime( 3446):    at android.app.ActivityThread.installContentProviders(ActivityThread.java:4385)
E/AndroidRuntime( 3446):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4325)
E/AndroidRuntime( 3446):    at android.app.ActivityThread.access$1500(ActivityThread.java:135)
E/AndroidRuntime( 3446):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
E/AndroidRuntime( 3446):    at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime( 3446):    at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime( 3446):    at android.app.ActivityThread.main(ActivityThread.java:5017)
E/AndroidRuntime( 3446):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 3446):    at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime( 3446):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E/AndroidRuntime( 3446):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
E/AndroidRuntime( 3446):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 3446): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.provider.FirebaseInitProvider"
E/AndroidRuntime( 3446):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
E/AndroidRuntime( 3446):    at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
E/AndroidRuntime( 3446):    at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
E/AndroidRuntime( 3446):    at android.app.ActivityThread.installProvider(ActivityThread.java:4778)
E/AndroidRuntime( 3446):    ... 12 more

This error was making the app crash on startup I was able to fix it with this answer : https://stackoverflow.com/a/37317943/11170097

Use Multidex.

in your app/build.gradle

android {
...               
    defaultConfig {
    ....
        multiDexEnabled true
    }
    ...
}

dependencies {
    implementation 'com.android.support:multidex:1.0.3'
    ...
}

in your app/src/main/java/.../MainApplication.java

...
import android.support.multidex.MultiDex;
import android.content.Context;

public class MyApplication extends ... {
  ...

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

I have currently encountered this issue at my project. The root cause of this issue is the unavailability of certain classes at specific API level.

For my case, I used RoleManager which is only available since API 29. Even I added a runtime checking like:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
  // Use `RoleManager` ...
}

The above line of codes are run smoothly in Native Android project but React Native throws me the error. This is actually caused by the line of import !

import android.app.role.RoleManager

Although I cannot find any RN official documentation about this phenomenon at the moment, I believe RN checks if all imports are valid before running developer's codes! Therefore, it is a totally different idea with Native Android programming flow!

Solution could be moving everything you're using into a Util.java / Util.kt and complete you logic in another class!

Related