171

I am getting the below exception on app launch.

java.lang.RuntimeException: Unable to get provider com.google.firebase.provider.FirebaseInitProvider: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.provider.FirebaseInitProvider" on path: DexPathList[[zip file "/data/app/com.vfirst.ifbagro-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.vfirst.ifbagro-1, /vendor/lib, /system/lib]]
at android.app.ActivityThread.installProvider(ActivityThread.java:4993)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4596)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4536)
at android.app.ActivityThread.access$1300(ActivityThread.java:149)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5214)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.provider.FirebaseInitProvider" on path: DexPathList[[zip file "/data/app/com.vfirst.ifbagro-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.vfirst.ifbagro-1, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.app.ActivityThread.installProvider(ActivityThread.java:4978)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4596) 
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4536) 
at android.app.ActivityThread.access$1300(ActivityThread.java:149) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:5214) 
at java.lang.reflect.Method.invokeNative(Native Method) 

Here is the the app level build.gradle

apply plugin: 'com.android.application'
apply plugin: 'android-apt' 

android {
compileSdkVersion 24
buildToolsVersion "24.0.1"

defaultConfig {
    applicationId "com.vfirst.ifbagro"
    minSdkVersion 17
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])


compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.android.support:support-v4:24.2.1'
compile 'com.google.android.gms:play-services-gcm:9.4.0'
compile 'com.google.android.gms:play-services-location:9.4.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.google.firebase:firebase-messaging:9.4.0'
compile 'com.google.android.gms:play-services:9.4.0'
testCompile 'junit:junit:4.12'
}

apply plugin: 'com.google.gms.google-services'

This is my application level build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
    jcenter()
    mavenCentral()
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.2.0'
    classpath 'com.google.gms:google-services:3.0.0'
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    classpath 'com.google.gms:google-services:3.0.0'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

allprojects {
repositories {
    jcenter()
}
}

task clean(type: Delete) {
     delete rootProject.buildDir
}

How to solve the issue?

5
  • Possible duplicate of stackoverflow.com/questions/37312103/…
    – Jay Rathod
    Oct 3, 2016 at 6:24
  • I had already tried all those answers. nothing works out
    – WISHY
    Oct 3, 2016 at 6:25
  • There seems to be a problem in your manifest or google-services.json Be sure they are correct. Can you post your manifest file? Oct 3, 2016 at 6:30
  • 6
    Did you make the manifest change needed for MultiDex:android:name="android.support.multidex.MultiDexApplication"? If MultiDex is correctly configured you will see logcat messages during app init like this one: I/MultiDex: install done.
    – Bob Snyder
    Oct 3, 2016 at 14:04
  • check instant run enable if yes, disable instant run. Dec 29, 2016 at 6:19

22 Answers 22

158

I had the same error and I solved it with MultiDex, like described on this link : https://developer.android.com/studio/build/multidex.html


Sometimes it is not enough just to enable MultiDex.

If any class that's required during startup is not provided in the primary DEX file, then your app crashes with the error java.lang.NoClassDefFoundError. https://developer.android.com/studio/build/multidex#keep

FirebaseInitProvider is required during startup.

So you must manually specify FirebaseInitProvider as required in the primary DEX file.

build.gradle file

android {
    buildTypes {
        release {
            multiDexKeepFile file('multidex-config.txt')
            ...
        }
    }
}

multidex-config.txt (in the same directory as the build.gradle file)

com/google/firebase/provider/FirebaseInitProvider.class
7
  • 26
    Hey this exception: Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.provider.FirebaseInitProvider" doesn´t have nothing to do with enabling multidex, in fact if you see the questions build.gradle multiDexEnabled is enabled!
    – Jorgesys
    Dec 5, 2016 at 16:43
  • 4
    No this error is from multidex, he just didn't complete all the steps, as you can't see his manifest or his main class.
    – AMAN77
    Mar 10, 2017 at 8:43
  • 4
    This worked. I had multidex enabled in my gradle file, but was missing the Android Manifest part!
    – krikara
    Apr 17, 2017 at 15:02
  • Thanks this worked. I had to enable multidex due to a build issue, then it could not find some of my services. @Jorgesys note that according to the link above you have to "Declare classes required in the primary DEX file"
    – Johnny
    Aug 7, 2018 at 22:20
  • 3
    Also, make sure that your Application class extends from MultiDexApplication(). This was missing in my case. Aug 29, 2019 at 21:41
87

I too faced the same issue and finally solved it by disabling Instant Run in an android studio.

Settings → Build, Execution, Deployment → Instant Run and uncheck Enable Instant Run

Update:

There is no Instant Run option available in latest Android Studio 3.5+. It should be applicable only for older versions.

14
  • 9
    Same for me. This was the cause of the problem -- not multidex or any of the other answers.
    – Stevey
    Mar 31, 2017 at 7:10
  • 3
    Seems this is an issue with Android Studio 2.3. Disabling the instant run did work for me too, but this isn't the ideal solution :( Jul 13, 2017 at 10:10
  • 1
    I inherited two projects from another firm. The code base, manifest and gradle configurations are practically the same expect for a couple of minor items. One project builds and runs fine, the other doesn't. Turned Instant Run off and they both run now. Strange. This is using Android Studio. 2.3.
    – kopi32
    Dec 8, 2017 at 16:27
  • 3
    I faced this very same issue with AS 3.1 and an emulator running API 26. Before I haven't encountered any problems with APIs 19-25. Disabling instant run indeed let me run the app. Thanks. Apr 7, 2018 at 20:33
  • 2
    Disabling Instant Run does not solve this problem for me.
    – Alyoshak
    Feb 14, 2019 at 0:18
51

In the build.gradle(Module:app) file, insert the code below into defaultConfig :

  defaultConfig {
        applicationId "com.***.****"
minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }

and insert into to dependencies :

implementation 'com.android.support:multidex:2.0.1'

Then add code to manifest :

<application 
    android:name="android.support.multidex.MultiDexApplication"
1
  • 1
    I,m trying it, if it will work I will up vote and comment Feb 18, 2017 at 7:57
50

I had the same problem in my (YouTube player project)... and the following solved the problem for me:

  1. Add this code into your build.gradle (module: app) inside defaultConfing:

    defaultConfig {
        ....
        ....
        multiDexEnabled = true
    }
    
  2. Add this code into your build.gradle (module: app) inside dependencies:

    dependencies {
        compile 'com.android.support:multidex:1.0.1'
        .....
        .....
    }
    
  3. Open AndroidManifest.xml and within application:

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

    or if you have your App class, extend it from MultiDexApplication like:

    public class MyApp extends MultiDexApplication {
    .....
    

And finally, I think you should have Android Support Repository downloaded, in the Extras in SDK Manager.

1
  • 1
    I did "MultiDex.install(this);" but it didn't work. Extending application class with MultiDexApplication did the trick.
    – Safeer
    Jan 15, 2019 at 7:34
38

Just override the following method in your application class.

public class YourApplication extends Application {

    @Override
    protected void attachBaseContext(Context context) {
        super.attachBaseContext(context);
        MultiDex.install(this);
    }
    @Override
    public void onCreate() {
        super.onCreate();
        Realm.init(this); //initialize other plugins 

    }
}
3
  • plus one for not falling for the android:name stuff in the Manifest
    – Chisko
    Apr 25, 2017 at 21:42
  • 1
    The simplest and the best answer May 18, 2017 at 10:57
  • If you are using a custom Application class then this is the answer. Other answers won't fix the issue. Jan 24, 2019 at 7:13
35

When your app and the libraries it references exceed 65,536 methods, you encounter a build error that indicates your app has reached the limit of the Android build architecture

To avoid this limitation you have to configure your app for multidex

If your minSdkVersion is set to 21 or higher, all you need to do is set multiDexEnabled to true in your module-level build.gradle file, as shown here:

android {
    defaultConfig {
        ...
        minSdkVersion 21 
        multiDexEnabled true
    }
    ...   
}

Else, if your minSdkVersion is set to 20 or lower, then you must use the multidex support library as follows:

1.Modify the module-level build.gradle file to enable multidex and add the multidex library as a dependency, as shown here:

android {
    defaultConfig {
        ...
        minSdkVersion 15 
        multiDexEnabled true
    }
    ...
 }

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

2.Depending on whether you override the Application class, perform one of the following:

  • If you do not override the Application class, edit your manifest file to set android:name in the tag as follows:

    <application
        android:name="android.support.multidex.MultiDexApplication" >
        ...
    </application>
  • Else If you do override the Application class, change it to extend MultiDexApplication (if possible) as follows:

   public class MyApplication extends MultiDexApplication { ... }
  • Else you do override the Application class but it's not possible to change the base class, then you can instead override the attachBaseContext() method and call MultiDex.install(this) to enable multidex:

   public class MyApplication extends SomeOtherApplication {
     @Override
     protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
     }
   }
4
  • Why does the application need to extend MultiDexApplication?
    – Jason
    Jan 14, 2019 at 22:47
  • 2
    MultiDexApplication is supportive library class. It implements Application class and overrides a method name attachBaseContext to do the job for MultiDex registration. You don't need to extend this class if you override the attachBaseContext method. Jan 21, 2019 at 19:57
  • 1
    This fixed it for me in 2019 with React Native but is missing 2 imports in MainApplication.java: import android.content.Context; and import android.support.multidex.MultiDex;
    – xaphod
    Jun 11, 2019 at 20:40
  • 2
    your answer helped me to make my app back compat. Aug 17, 2019 at 3:38
18

Enabling multidex is not a good solution because multidexhave another usage in android see this answer what is multidex

The solution is disabling instant run as @Shylendra Madda said

Settings → Build, Execution, Deployment → Instant Run and uncheck Enable Instant Run

I think the reason of this problem is when instant run is enabled, Android Studio don't put libraries such as firebase into generated apk to decreasing project build time because firebase library and other libraries such as maps and others is exist in play services and play services is installed on android device so if instant run enabled don't need to put them in generated apk to make build time faster.

So when you extract apk and install it on another device you will see this exception

4
15

I have also face the same issue after trying all solution I found the below solution.

If you have applied proguard rules then add below line in ProGuard Rules

-keep class com.google.firebase.provider.FirebaseInitProvider

and its solve my problem.

5
  • @gunavant patel bro please tell me how to add into proguard please Sep 10, 2018 at 11:27
  • 1
    @gunavant patel i think this will solved my problem...can you please post sample code where to keep this line -keep class com.google.firebase.provider.FirebaseInitProvider Sep 10, 2018 at 11:28
  • @GowthamanM First check you enable proguard in app/build.gradle file. if enable then you find proguard-rules file in app/proguard-rules.pro in that file add above line. If you not enable proguard then its not hepl you. Sep 10, 2018 at 13:40
  • buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } How to enable please tell me sir Sep 10, 2018 at 13:42
  • if you have not enabled proguard then problem is different . but I show you how enable that. add "minifyEnabled true " in release or if you want enable for debug then also add in debug type. Sep 10, 2018 at 13:45
8

1: Go to the gradle enable multiDexEnabled and add the multidex library in the dependencies.

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

dependencies {
  // add dependency 
  implementation 'com.android.support:multidex:1.0.1'
}

2: Go to the Manifest file and write android:name=".MyApplication" (Class name(MyApplication) is optional you can write whatever you want ).

    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        .
        .
        .
        .
        .
    </application>

3: As you wrote android:name=".MyApplication" Inside Application at Manifest file. it will give you an error because you didn't create MyApplication class. Create MyApplication Class extend it by "application" class or Simply click on.MyApplication, a small red balloon appear on the left side of a syntax click on it, you will see (create MyApplication class) in the menu, click on it and Include below Method Inside that class.

    public class MyApplication extends Application {

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

If you want to get more information then Click on this Link:[https://developer.android.com/studio/build/multidex.html]

Hopefully, It works for you.

7

First add the following into build.gradle(Module:app)---->

defaultConfig{
..
multiDexEnabled true
..
}

Then in same file add the following

dependencies{
...
compile 'com.android.support:multidex:1.0.1'
...
}

Then in AndroidManifest.xml write the following

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

That's it. It will definitely work

0
6

If you are using MultiDex in your App Gradle then change extends application to extends MultiDexApplication in your application class. It will defiantly work

4

I had the same issue and solved just add a piece of code, If you are getting this issue means you had already completed all the steps that need to use Firebase. You just need to create a class that extends the Application (public class Application_CrashReport extends Application ) and add this class in mainifest file and In that class just override the below method

  @Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
}

and add MultiDex.install(this); in that method means

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

Note :- Don't forget to follow above steps

3

This is a bit late for those coming in, but check your proguard rules! I wasted a lot of time on this. Your proguard rules could be changing the names to important firebase files. This really only proves a problem in production and instant run :)

proguard-rules.pro

-keep class com.google.firebase.** { *; }
-keep class com.firebase.** { *; }
-keep class org.apache.** { *; }
-keepnames class com.fasterxml.jackson.** { *; }
-keepnames class javax.servlet.** { *; }
-keepnames class org.ietf.jgss.** { *; }
-dontwarn org.apache.**
-dontwarn org.w3c.dom.**
1
  • This did the trick, enabling multidex did nothing but waste time!
    – Magnus
    May 18, 2019 at 12:11
3

As mentioned previously, this is a problem with multidex: you should add implementation to your build.gradle and MainApplication.java. But what you add really depends on whether you support AndroidX or not. Here is what you need to solve this problem:

  1. If you support AndroidX

Put these lines of code into your build.gradle (android/app/build.gradle):

defaultConfig {
  applicationId "com.your.application"
  versionCode 1
  versionName "1.0"
  ...
  multiDexEnabled true // <-- THIS LINE
}
...
...
dependencies {
  ...
  implementation "androidx.multidex:multidex:2.0.1" // <-- THIS LINE
  ...
}

Put these lines into your MainApplication.java (android/app/src/main/java/.../MainApplication.java):

package com.your.package;

import androidx.multidex.MultiDexApplication; // <-- THIS LINE
...
...
public class MainApplication extends MultiDexApplication { ... } // <-- THIS LINE
  1. If you don't support AndroidX

Put these lines of code into your build.gradle (android/app/build.gradle):

defaultConfig {
  applicationId "com.your.application"
  versionCode 1
  versionName "1.0"
  ...
  multiDexEnabled true // <-- THIS LINE
}
...
...
dependencies {
  ...
  implementation "com.android.support:multidex:1.0.3" // <-- THIS LINE
  ...
}

Put these lines into your MainApplication.java (android/app/src/main/java/.../MainApplication.java):

package com.your.package;

import android.support.multidex.MultiDex;; // <-- THIS LINE
...
...
public class MainApplication extends MultiDexApplication { ... } // <-- THIS LINE
0
2

This is due to MultiDex.

Steps to solve:

  1. In gradle->dependencies->(compile'com.android.support:multidex:1.0.1') Add this in dependencies

  2. In your project application class extends MultiDexApplication like this (public class MyApplication extends MultiDexApplication)

  3. Run and check

2

You should extend your Application class with MultiDexApplication instead of Application.

0

This issue occurred when I switched to Android Studio 3.4 with Android Gradle plugin 3.4.0. which works with the R8 compiler.

The Android Gradle plugin includes additional predefined ProGuard rules files, but it is recommended that you use proguard-android-optimize.txt. More info here.

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile(
          'proguard-android-optimize.txt'),
          // List additional ProGuard rules for the given build type here. By default,
          // Android Studio creates and includes an empty rules file for you (located
          // at the root directory of each module).
          'proguard-rules.pro'
    }
}
0

I was facing the same problem and i solved this issue by changing the fireabseStorage version the same as the Firebase database version

 implementation 'com.google.firebase:firebase-core:16.0.8'
 implementation 'com.google.firebase:firebase-database:16.0.1'
 implementation 'com.google.firebase:firebase-storage:17.0.0'

to

 implementation 'com.google.firebase:firebase-core:16.0.8'
 implementation 'com.google.firebase:firebase-database:16.0.1'
 implementation 'com.google.firebase:firebase-storage:16.0.1'
0

If you have > 20 minsdkversion, you need to use the latest Firebase Auth version i.e.

implementation 'com.google.firebase:firebase-auth:18.1.0'

and no need to setup multi-dex if you don't actually need it.

I encountered this issue when I've used 16.0.5 from the Firebase helper but was able to fix it when I've updated to 18.1.0.

0

If you're using Xamarin.Forms you might want to check out Didn't find class "com.google.firebase.provider.FirebaseInitProvider"? as this captures the issue with the dex error with google firebase on startup (unresolved at this time).

I've reverted to using no shrinking in the short-term and plan to use ProGuard until R8 is more stable.

0
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
  

// add the above code in your app level gradle file just after

defaultConfig{

}

-7

add in project root path google-services.json

dependencies {
compile 'com.android.support:support-v4:25.0.1'
**compile 'com.google.firebase:firebase-ads:9.0.2'**
compile files('libs/StartAppInApp-3.5.0.jar')
compile 'com.android.support:multidex:1.0.1'
  }
apply plugin: 'com.google.gms.google-services'
1
  • consider adding some information to ur answer and this code do not gonna help anyone
    – Ashu Kumar
    Oct 25, 2017 at 11:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.