26

App crashes when i apply crashlytics on it

FATAL EXCEPTION: main

Process: com.ehs.pk, PID: 20963
java.lang.RuntimeException: Unable to get provider com.crashlytics.android.CrashlyticsInitProvider: java.lang.ClassNotFoundException: Didn't find class "com.crashlytics.android.CrashlyticsInitProvider" on path: DexPathList[[zip file "/data/app/com.ehs.pk-8.apk"],nativeLibraryDirectories=[/data/app-lib/com.ehs.pk-8, /vendor/lib, /system/lib]]
    at android.app.ActivityThread.installProvider(ActivityThread.java:4993)
    at android.app.ActivityThread.installContentProviders(ActivityThread.java:4585)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4499)
    at android.app.ActivityThread.access$1500(ActivityThread.java:157)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5293)
    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:1265)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.crashlytics.android.CrashlyticsInitProvider" on path: DexPathList[[zip file "/data/app/com.ehs.pk-8.apk"],nativeLibraryDirectories=[/data/app-lib/com.ehs.pk-8, /vendor/lib, /system/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
    at android.app.ActivityThread.installProvider(ActivityThread.java:4978)
    at android.app.ActivityThread.installContentProviders(ActivityThread.java:4585) 
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4499) 
    at android.app.ActivityThread.access$1500(ActivityThread.java:157) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:157) 
    at android.app.ActivityThread.main(ActivityThread.java:5293) 
    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:1265) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 
    at dalvik.system.NativeStart.main(Native Method) 
Zoe
  • 27,060
  • 21
  • 118
  • 148
Amir Jehangir
  • 285
  • 1
  • 3
  • 7

8 Answers8

37

I was facing the same issue, is related to the 64k limit "multidex".

I was using multiDexEnabled true property in build.gradle in defaultConfig block, under android one.

As stated in Google documentation

for devices with Android API prior to 21, we need to include the multidex library (implementation 'com.android.support:multidex:1.0.2')

and extend MultiDexApplication in our Application class

  • If anyone facing this issue on API level **above 21**, check out https://stackoverflow.com/a/52813496/5460053. As I faced this issue on **API 27 Android Pie** – Monish Kamble Nov 17 '18 at 06:42
10

I have solved this problem by doing the following:

In the Application class:

@Override
public void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    try {
        MultiDex.install(this);
    } catch (RuntimeException multiDexException) {
        multiDexException.printStackTrace();
    }
}
user2796148
  • 101
  • 1
  • 3
10

We were having the same issue, and disabling instant run in Android Studio seemed to get it working.

On mac Android Studio Settings or Preferences -> Build,Execution,Deployment -> Instant Run.

Alistair Sykes
  • 283
  • 1
  • 3
  • 9
7

By looking at this part...

Didn't find class ... on path: DexPathList ... dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoade‌​r.java:67)

of the stack trace you can figure out that your test device is probably pre-Lollipop (Android 5.0) and that you just hit the 64k limit. You can fix it as described here.

Peppermint Paddy
  • 1,269
  • 9
  • 14
  • 3
    I am having such issue with Android API 26 and above, Android Studio 3.1 – neobie Apr 04 '18 at 06:35
  • 1
    @neobie I got the same when I updated, using multidex doesn't seem to help. Did you solve it? – emillime Apr 17 '18 at 08:44
  • @fuskaren what is your min API level? – Peppermint Paddy Apr 17 '18 at 09:25
  • @PeppermintPaddyIts It's 15 but it crashes on any version directly on start. – emillime Apr 17 '18 at 09:34
  • Or actually it might just crash on versions over 25. – emillime Apr 17 '18 at 10:09
  • did you do all 3 steps described in the link I posted? first insert gradle dependency, then `multiDexEnabled true` and then `MyApplication extends MultiDexApplication` or add `MultiDexApplication` to manifest? – Peppermint Paddy Apr 17 '18 at 10:11
  • @PeppermintPaddy Yeah, I did all those things. The crashes started after I updated android studio to 3.1. Before that it was all working fine without multidex. – emillime Apr 17 '18 at 13:53
  • 1
    @fuskaren I am adding more proguard rules for those library causing error.. not sure why it only happens after Android Studio 3.1 – neobie Apr 19 '18 at 09:26
  • @neobie I'll try that. Are you also using multidex or is that not the issue? Mind showing what rules you added? – emillime Apr 19 '18 at 10:25
  • @neobie Did you solve it? I'm still not getting it to work. – emillime Apr 23 '18 at 08:50
  • This is not a dex limit problem. If you hit the 65k limit you'd know it at compile time. You'd never even make it to runtime to see the crash OP shared. It *may* be a multidex problem (i.e. you already setup multidex and this class isn't being loaded as part of first dex file) – tir38 Jun 05 '18 at 17:28
7

This could be an issue with Instant Run. Turn off the Instant Run and run the build again. I faced the same issue and it resolved it.

Sadeshkumar Periyasamy
  • 4,848
  • 1
  • 26
  • 31
4

When minifyEnabled is true while using firebase or crashlytics this problem may happens. Enabling minifyEnabled shrinks methods and classes names to lowest possible characters (changing names and that raises ClassNotFoundException). Unfortunately, firebase and crashlytics versions had many problem with that. To solve it just tell proguard to not shrink those classes by writing the next lines in proguard file "proguard-rules.pro" as:

-keep class com.crashlytics.** { *; }
-keep class com.google.firebase.*.* { *; }

Now if the problem not go away or it didn't find other classes like yours, then check your base project build.gradle dependencies version of gradle and others. I found these version is free of that error:

    dependencies {
    classpath 'com.android.tools.build:gradle:3.0.1'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    classpath 'com.google.gms:google-services:3.2.0'
    classpath 'io.fabric.tools:gradle:1.25.1'

}

Remeber you can put any classes path into that proguard file so it did not change its name or methods when minify is enabled, and that will help you with other classes "ClassNotFoundException" as well.

Also to test this in debugging, you can adjust debug setting in build.gradle as:

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

    debug {
        debuggable true
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }

}
MohammadL
  • 2,398
  • 1
  • 19
  • 36
  • 1
    These are overly broad proguard rules. I don't think you need to keep *all* of Crashlytics and *all* of Firebase to solve the original problem. – tir38 Jun 05 '18 at 17:29
1

Just one more case:

In my case, I disabled instant run, added multiDexEnabled true in my gradle, and added the 'com.android.support:multidex:1.0.3' to my dependencies, called the MultiDex.install(this); directly in my custom application, but I still got the same error. The error only occurs in the system Android 4.4. Everything works well on Android 7.0 and 9.0.

Finally, moving the MultiDex.install(this); from the onCreate(Context) method to the attachBaseContext(Context) solved my problem.

So, you should call MultiDex.install(this); in attachBaseContext(Context) instead of onCreate(Context) when you don't plan to extend the MultiDexApplication directly.

Shawn Wong
  • 554
  • 6
  • 15
0

If this happens when you run with minifyEnabled true, add this rule in your proguard-rules.pro file:

-keep public class com.crashlytics.android.CrashlyticsInitProvider
Minas Mina
  • 2,058
  • 3
  • 21
  • 35