Didn't find class "com.google.firebase.provider.FirebaseInitProvider"?

AndroidFirebase

Android Problem Overview


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:4596at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4536at android.app.ActivityThread.access$1300(ActivityThread.java:149at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353at android.os.Handler.dispatchMessage(Handler.java:99at android.os.Looper.loop(Looper.java:137at android.app.ActivityThread.main(ActivityThread.java:5214at 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?

Android Solutions


Solution 1 - Android

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


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

Solution 2 - Android

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.

Solution 3 - Android

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"

Solution 4 - Android

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.

Solution 5 - Android

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 

    }
}

Solution 6 - Android

> 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: > > > > > android:name="android.support.multidex.MultiDexApplication" > > ... > > > > > - 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); > } > }

Solution 7 - Android

Enabling multidex is not a good solution because multidex have 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

Solution 8 - Android

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.

Solution 9 - Android

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.

Solution 10 - Android

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

Solution 11 - Android

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

Solution 12 - Android

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

Solution 13 - Android

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.**

Solution 14 - Android

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

Solution 15 - Android

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

Solution 16 - Android

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

Solution 17 - Android

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'
    }
}

Solution 18 - Android

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'

Solution 19 - Android

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.

Solution 20 - Android

If you're using Xamarin.Forms you might want to check out https://stackoverflow.com/questions/39825543/didnt-find-class-com-google-firebase-provider-firebaseinitprovider/39831657 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.

Solution 21 - Android

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
  

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

defaultConfig{

}

Solution 22 - Android

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'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWISHYView Question on Stackoverflow
Solution 1 - AndroidjohanvsView Answer on Stackoverflow
Solution 2 - AndroidShailendra MaddaView Answer on Stackoverflow
Solution 3 - AndroidCodeArtView Answer on Stackoverflow
Solution 4 - AndroidFiras HashashView Answer on Stackoverflow
Solution 5 - AndroidSilverSkyView Answer on Stackoverflow
Solution 6 - AndroidSokmean PonView Answer on Stackoverflow
Solution 7 - AndroidEdalat FeiziView Answer on Stackoverflow
Solution 8 - AndroidGunavant PatelView Answer on Stackoverflow
Solution 9 - AndroidShahzad AfridiView Answer on Stackoverflow
Solution 10 - AndroidHirak JDView Answer on Stackoverflow
Solution 11 - AndroidSuresh kumarView Answer on Stackoverflow
Solution 12 - AndroidPeterView Answer on Stackoverflow
Solution 13 - AndroidKeith AylwinView Answer on Stackoverflow
Solution 14 - AndroidE. DnView Answer on Stackoverflow
Solution 15 - AndroidSukhdeep SainiView Answer on Stackoverflow
Solution 16 - AndroidAli AkramView Answer on Stackoverflow
Solution 17 - AndroidLevon PetrosyanView Answer on Stackoverflow
Solution 18 - AndroidUmer Waqas CEO FluttydevView Answer on Stackoverflow
Solution 19 - AndroidSilverArrowView Answer on Stackoverflow
Solution 20 - AndroidDevology LtdView Answer on Stackoverflow
Solution 21 - AndroidSourabh KumarView Answer on Stackoverflow
Solution 22 - AndroidMuhammad AzeemView Answer on Stackoverflow