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

22

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?

Emplacement answered 3/10, 2016 at 6:20 Comment(5)
Possible duplicate of #37312603Planchet
I had already tried all those answers. nothing works outEmplacement
There seems to be a problem in your manifest or google-services.json Be sure they are correct. Can you post your manifest file?Wilhelmstrasse
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.Leasia
check instant run enable if yes, disable instant run.Fetish
A
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
Amenable answered 3/10, 2016 at 12:27 Comment(7)
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!Thoracoplasty
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.Expeller
This worked. I had multidex enabled in my gradle file, but was missing the Android Manifest part!Lawannalawbreaker
Thanks this worked. I had to enable multidex due to a build issue, then it could not find some of my services. @Thoracoplasty note that according to the link above you have to "Declare classes required in the primary DEX file"Brawl
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.Galligan
Also, make sure that your Application class extends from MultiDexApplication(). This was missing in my case.Damocles
Now my app works. But I can't reach internet. All requests give "Network Error" exception.Benefic
J
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.

Jasen answered 24/3, 2017 at 11:46 Comment(14)
Same for me. This was the cause of the problem -- not multidex or any of the other answers.Clytemnestra
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 :(Lafountain
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.Flaming
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...Offensive
Glad to help you happy coding.. :) @KKBJasen
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.Laplante
Thanks. Using Android Studio 3.0.1. Strange to see such clueless issues still exists. Instant Run has a history of issues.Bondage
Same issue with Android Studio 3.1.2 and emulator on API 27, got weird Firebase error, found this, disabled Instant Run, started working...Exhalation
Good to hear that it is working but waiting for to fix this issue by the Android Studio guys.Jasen
Android Studio 3.2 beta2 here. Disabled Instant Run and it works.Normy
Disabling Instant Run does not solve this problem for me.Costin
@SugoiReed Glad, it helped you :)Jasen
In Android Studio 3.5.1, there is no Instant Run item in the Build, Execution, Deployment section.Especially
@Especially 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.Jasen
S
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"
Swiss answered 31/1, 2017 at 7:54 Comment(1)
I,m trying it, if it will work I will up vote and commentPerfectionism
T
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.

Thoughtless answered 11/2, 2017 at 11:59 Comment(1)
I did "MultiDex.install(this);" but it didn't work. Extending application class with MultiDexApplication did the trick.Mosley
F
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 

    }
}
Fluviomarine answered 10/4, 2017 at 8:22 Comment(3)
plus one for not falling for the android:name stuff in the ManifestYoungs
The simplest and the best answerMandel
If you are using a custom Application class then this is the answer. Other answers won't fix the issue.Doglike
E
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);
     }
   }
Eartha answered 7/3, 2018 at 17:18 Comment(4)
Why does the application need to extend MultiDexApplication?Deary
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.Eartha
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;Chiasma
your answer helped me to make my app back compat.Aileneaileron
S
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

Shearin answered 7/8, 2017 at 13:28 Comment(4)
This is perfect solutionBracken
Disabling Instant Run does not solve the problem for me.Costin
In Android Studio 3.5.1, there is no Instant Run item in the Build, Execution, Deployment section.Especially
@Especially They replaced it with apply changes see the release blog post android-developers.googleblog.com/2019/08/… for more info and the docs developer.android.com/studio/run#apply-changes for more info about apply changesShearin
M
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.

Monied answered 3/5, 2018 at 7:8 Comment(5)
@gunavant patel bro please tell me how to add into proguard pleaseGlycerin
@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.FirebaseInitProviderGlycerin
@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.Monied
buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } How to enable please tell me sirGlycerin
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.Monied
M
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.

Massproduce answered 13/11, 2017 at 13:40 Comment(0)
A
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

Antisyphilitic answered 24/2, 2017 at 14:2 Comment(0)
W
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

Whiffletree answered 22/2, 2017 at 10:45 Comment(0)
P
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

Pissed answered 6/12, 2017 at 11:43 Comment(0)
S
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.**
Silicon answered 19/10, 2018 at 4:45 Comment(1)
This did the trick, enabling multidex did nothing but waste time!Acrefoot
D
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
Diviner answered 9/12, 2019 at 5:6 Comment(0)
M
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

Marismarisa answered 31/12, 2017 at 13:4 Comment(0)
R
2

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

Rialto answered 25/5, 2018 at 21:12 Comment(0)
B
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'
    }
}
Breakage answered 30/4, 2019 at 2:43 Comment(0)
R
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'
Renata answered 11/5, 2019 at 11:18 Comment(0)
C
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.

Carbolize answered 8/8, 2019 at 5:57 Comment(0)
P
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.

Prefab answered 21/8, 2019 at 10:22 Comment(0)
A
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{

}

Angularity answered 4/2, 2022 at 11:29 Comment(0)
J
-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'
Jansenism answered 20/12, 2016 at 8:55 Comment(1)
consider adding some information to ur answer and this code do not gonna help anyoneBuckling

© 2022 - 2024 — McMap. All rights reserved.