170

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?

Arpit Patel
  • 1,561
  • 13
  • 23
WISHY
  • 11,067
  • 25
  • 105
  • 197
  • Possible duplicate of http://stackoverflow.com/questions/37312103/unable-to-get-provider-com-google-firebase-provider-firebaseinitprovider – Jay Rathod Oct 03 '16 at 06:24
  • I had already tried all those answers. nothing works out – WISHY Oct 03 '16 at 06: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? – Rushi M Thakker Oct 03 '16 at 06: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 03 '16 at 14:04
  • check instant run enable if yes, disable instant run. – Prathap Badavath Dec 29 '16 at 06:19

22 Answers22

157

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
ElOjcar
  • 301
  • 2
  • 4
  • 12
johanvs
  • 4,273
  • 3
  • 24
  • 26
  • 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 05 '16 at 16:43
  • 3
    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 '17 at 08:43
  • 3
    This worked. I had multidex enabled in my gradle file, but was missing the Android Manifest part! – krikara Apr 17 '17 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 07 '18 at 22:20
  • This answer should be upvoted more, as stated before, this has nothing to do with multidex and can lead to confusion about what multidex really is and when you should care about it. AFAIK you don't have the need to add any dependencie nor configuration fort multidex prior to Android 5.0 API. – 4gus71n Dec 20 '18 at 18:37
  • 3
    Also, make sure that your `Application` class extends from `MultiDexApplication()`. This was missing in my case. – Nelson Almendra Aug 29 '19 at 21:41
  • Now my app works. But I can't reach internet. All requests give "Network Error" exception. – msalihbindak Dec 18 '20 at 12:57
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.

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
  • 9
    Same for me. This was the cause of the problem -- not multidex or any of the other answers. – Stevey Mar 31 '17 at 07: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 :( – Tharaka Devinda Jul 13 '17 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 08 '17 at 16:27
  • Though I have tried and the accepted answer and got it to work I also did this solution just for comparison and it ALSO worked. Maybe Android engineers should not keep doing more "bells and whistles" as it messes up Android Studio. They should KISS it more often!!! Keep it simple that is... – Marka A Feb 10 '18 at 21:44
  • Glad to help you happy coding.. :) @KKB – Shailendra Madda Apr 06 '18 at 11:57
  • 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. – Marcin Jedynak Apr 07 '18 at 20:33
  • Thanks. Using Android Studio 3.0.1. Strange to see such clueless issues still exists. Instant Run has a history of issues. – Muhammad Babar Apr 14 '18 at 16:43
  • Same issue with Android Studio 3.1.2 and emulator on API 27, got weird Firebase error, found this, disabled Instant Run, started working... – BjornW Apr 26 '18 at 13:18
  • Good to hear that it is working but waiting for to fix this issue by the Android Studio guys. – Shailendra Madda Apr 27 '18 at 08:33
  • 1
    Android Studio 3.2 beta2 here. Disabled Instant Run and it works. – passsy Jul 04 '18 at 15:32
  • 2
    Disabling Instant Run does not solve this problem for me. – Alyoshak Feb 14 '19 at 00:18
  • @SugoiReed Glad, it helped you :) – Shailendra Madda Aug 02 '19 at 11:43
  • In Android Studio 3.5.1, there is no Instant Run item in the Build, Execution, Deployment section. – Vergiliy Oct 11 '19 at 07:17
  • @Vergiliy Yes, in Android Studio 3.5.1 it removed because by default there is one extra option provided(Beside Run button) to apply changes without restarting the app. – Shailendra Madda Oct 11 '19 at 07:29
50

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"
Community
  • 1
  • 1
CodeArt
  • 858
  • 8
  • 10
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.

iLoveCode
  • 99
  • 2
  • 8
Firas Hashash
  • 501
  • 4
  • 3
  • 1
    I did "MultiDex.install(this);" but it didn't work. Extending application class with MultiDexApplication did the trick. – Safeer Jan 15 '19 at 07:34
36

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 

    }
}
Shaishav Jogani
  • 2,111
  • 3
  • 23
  • 33
SilverSky
  • 431
  • 4
  • 3
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);
     }
   }
Sokmean Pon
  • 361
  • 3
  • 8
  • Why does the application need to extend MultiDexApplication? – Jason Jan 14 '19 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. – Sokmean Pon Jan 21 '19 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 '19 at 20:40
  • 2
    your answer helped me to make my app back compat. – Alok Vishwakarma Aug 17 '19 at 03: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

Edalat Feizi
  • 1,371
  • 20
  • 32
  • 1
    This is perfect solution – Ghanshyam Bagul Sep 04 '17 at 11:29
  • Disabling Instant Run does not solve the problem for me. – Alyoshak Feb 14 '19 at 00:17
  • In Android Studio 3.5.1, there is no Instant Run item in the Build, Execution, Deployment section. – Vergiliy Oct 11 '19 at 07:18
  • @Vergiliy They replaced it with apply changes see the release blog post https://android-developers.googleblog.com/2019/08/android-studio-35-project-marble-goes.html for more info and the docs https://developer.android.com/studio/run#apply-changes for more info about apply changes – Edalat Feizi Oct 11 '19 at 13:47
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.

Gunavant Patel
  • 1,413
  • 1
  • 13
  • 17
  • @gunavant patel bro please tell me how to add into proguard please – Gowthaman M Sep 10 '18 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` – Gowthaman M Sep 10 '18 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. – Gunavant Patel Sep 10 '18 at 13:40
  • `buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }` How to enable please tell me sir – Gowthaman M Sep 10 '18 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. – Gunavant Patel Sep 10 '18 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.

Shahzad Afridi
  • 2,058
  • 26
  • 24
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

Hirak JD
  • 181
  • 2
  • 7
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

Suresh kumar
  • 789
  • 10
  • 13
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

Peter
  • 587
  • 6
  • 16
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.**
Keith Aylwin
  • 516
  • 4
  • 15
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
E. Dn
  • 940
  • 1
  • 9
  • 21
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

dferenc
  • 7,918
  • 12
  • 41
  • 49
2

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

Ali Akram
  • 4,803
  • 3
  • 29
  • 38
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'
    }
}
Levon Petrosyan
  • 8,815
  • 8
  • 54
  • 65
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.

RobbiewOnline
  • 1,350
  • 1
  • 16
  • 36
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'