171

I am testing the new Crash tool: https://firebase.google.com/docs/crash/

After going through the steps, the app launches and it crashes saying:

05-18 17:28:18.870 28743 28743 E AndroidRuntime: java.lang.RuntimeException: Unable to get provider com.google.firebase.provider.FirebaseInitProvider: java.lang.IllegalStateException: Incorrect provider authority in manifest. Most likely due to a missing applicationId variable in application's build.gradle.
05-18 17:28:18.870 28743 28743 E AndroidRuntime:    at android.app.ActivityThread.installProvider(ActivityThread.java:5156)
05-18 17:28:18.870 28743 28743 E AndroidRuntime:    at android.app.ActivityThread.installContentProviders(ActivityThread.java:4748)
05-18 17:28:18.870 28743 28743 E AndroidRuntime:    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4688)
05-18 17:28:18.870 28743 28743 E AndroidRuntime:    at android.app.ActivityThread.-wrap1(ActivityThread.java)
05-18 17:28:18.870 28743 28743 E AndroidRuntime:    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
05-18 17:28:18.870 28743 28743 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:102)
05-18 17:28:18.870 28743 28743 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:148)
05-18 17:28:18.870 28743 28743 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:5417)
05-18 17:28:18.870 28743 28743 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)
05-18 17:28:18.870 28743 28743 E AndroidRuntime:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
05-18 17:28:18.870 28743 28743 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-18 17:28:18.870 28743 28743 E AndroidRuntime: Caused by: java.lang.IllegalStateException: Incorrect provider authority in manifest. Most likely due to a missing applicationId variable in application's build.gradle.
05-18 17:28:18.870 28743 28743 E AndroidRuntime:    at com.google.firebase.provider.FirebaseInitProvider.zza(Unknown Source)
05-18 17:28:18.870 28743 28743 E AndroidRuntime:    at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source)
05-18 17:28:18.870 28743 28743 E AndroidRuntime:    at android.app.ActivityThread.installProvider(ActivityThread.java:5153)
05-18 17:28:18.870 28743 28743 E AndroidRuntime:    ... 10 more
Macarse
  • 91,829
  • 44
  • 175
  • 230

20 Answers20

163

1.

Add the applicationId to the application's build.gradle:

android {
    ...
    defaultConfig {
        applicationId "com.example.my.app"
        ...
    }
}

And than Clean Project -> Build or Rebuild Project


2. If your minSdkVersion <= 20 (https://developer.android.com/studio/build/multidex)

Use Multidex correctly.

application's build.gradle

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

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

manifest.xml

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

3.

If you use a custom Application class

public class MyApplication extends MultiDexApplication {
    @Override
    protected void attachBaseContext(Context context) {
        super.attachBaseContext(context);
        MultiDex.install(this);
    }
}

manifest.xml

<application
    ...
    android:name="com.example.my.app.MyApplication" >
    ...
norbDEV
  • 4,795
  • 2
  • 37
  • 28
  • 1
    I got the same error and it has been fixed by the solution. I don't know the why, but it works. – FUJI Goro May 21 '16 at 14:27
  • This saved the day for me. Got the error while I was trying to run a Cordova app to an android device. – manosim May 25 '16 at 10:42
  • 1
    I have the same problem and the applicationId on my build.gradle. What can I do in that case? – Maria Mercedes Wyss Alvarez May 25 '16 at 21:09
  • @ekonstantinidis How did you fix it for your cordova app? Can it be added to a config somewhere that correctly adds it when build.gradle is generated? (During `cordova platform add android`) ? – Zach May 26 '16 at 20:33
  • @Zach I don't think so. I added it after I ran `cordova platform add android`. I guess it will get fixed once cordova gets an updated? Ps. I'm not using Firebase at all. – manosim May 26 '16 at 20:35
  • I have to comment, that, in my case it doen't work, even more, I got the applicationId in the build.gradle before I implemented the FCM .. is there any other knwon solution? – Shudy May 30 '16 at 11:50
  • @Barthy I tested it, but didnt worked. Even though, I solve the problem. Was some kind of pc errror. I get BLue Screen on windows, it restored some files, etc, and worked again all the project :/ . With Windows.. my mind is going to explode some day.. XD – Shudy Jun 13 '16 at 08:53
  • Strange that this didn't get set, never had this issue before. Thanks for the fix. – Jack Vial Jun 14 '16 at 20:56
  • Accurate answer, thanks, worked for me... but, missing an explanation, why? And how did you figured it out? – Hugo Allexis Cardona Oct 31 '17 at 12:56
  • I had `force close` on application start on API 15 and this solution wiped it out. – mammadalius Jul 28 '19 at 12:42
  • This answer is no longer functional in 2020 – Aditya Anand Feb 14 '20 at 10:29
  • What do do if I'm getting this error inside another library module, where I cannot add the applicationId? – Stefan Dec 02 '22 at 07:52
94

I was with the same problem in devices with SDK < 22, but for me the reason is the MultiDex, the MultiDex.install must be in the attachBaseContext method.

If you are using MultiDex, try this:

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 

    }
}

app/build.gradle:

android {

    compileSdkVersion 24
    buildToolsVersion "24.0.2"

    defaultConfig {
        applicationId "com.test.app"
        minSdkVersion 16
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        multiDexEnabled true

    }

    dexOptions {
        javaMaxHeapSize "4g"
    }

....
}

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

        compile 'com.android.support:appcompat-v7:24.2.1'
        ...
        compile 'com.google.firebase:firebase-messaging:9.6.1'
        compile 'com.google.android.gms:play-services:9.6.1'
        compile 'com.android.support:multidex:1.0.1'

    }
...
luizMello
  • 2,793
  • 20
  • 17
  • 4
    It works, but there's a special class to do that instead of overriding one function and it's called MultiDexApplication, see answer below http://stackoverflow.com/questions/37312103/unable-to-get-provider-com-google-firebase-provider-firebaseinitprovider/40603347#40603347 – GregoryK Nov 26 '16 at 15:40
  • Yeah it worked for me also, I always writing MultiDex.install(this) inside onCreate() method thanks dude – dara Jan 21 '18 at 11:17
  • in my case I just updated compile 'com.android.support:multidex:1.0.1' to last available version – Sirop4ik May 27 '18 at 11:19
  • This should be the accepted answer. I tried all the other answers and was following Google and Firebase guides but this is the correct one, you're awesome man! – blueware Mar 28 '19 at 15:01
  • Thanks , how can you found it? but my question is my code lines app is not upper than 65000 why i should use it multudex and why i work on sdk>22 and it does not work on sdk<22 – milad salimi Jun 26 '19 at 05:13
  • If your minSdk is 21 or higher, you shouldn't have to do any of this as the docs say it's already enabled by default. And yet, this problem is coming for me with minSdk = 21, any solutions for this? – Aditya Anand Feb 14 '20 at 10:28
75

The accepted answer didn't solve my problem.

If you are using Multidex, your Application should extends MultiDexApplication instead of Application.

MyApplication.java

public class MyApplication extends MultiDexApplication{
     ...
}

AndroidManifest.xml

<application
      android:name="your.package.name.MyApplication"
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      ...
      />

Hope it helps.

Gene Bo
  • 11,284
  • 8
  • 90
  • 137
Ye Min Htut
  • 2,904
  • 15
  • 28
  • Worked perfectly! Thanks alot :) – Shujat Munawar Jan 30 '18 at 12:47
  • 3
    How do you know these things?? Who told you??? I just started having this issue after updating to Android Studio 3 and installing "debug" apps. The "Release" version worked well. Now, with your solution also works the "Debug" version. I am so happy people like you exist!! – Ton Mar 06 '18 at 22:59
  • That's perfect answer – androidXP Apr 03 '18 at 10:28
  • In all of these answers, none actually explains what adding multidex actually does to solve the problem (a perplexingly random thing to do in the face of such an issue) – Aditya Anand Feb 14 '20 at 10:05
37

If you get the same problem but already have the applicationId set in build.gradle, you can also try the following:

  • in Android Studio: Build > Clean Project
  • in other IDEs: Clean, Rebuild, whatever...
  • delete the build directory and possibly other caches or intermediate build files
Barthy
  • 3,151
  • 1
  • 25
  • 42
18

I had the same problem in Pre Lollipop devices. To solve that I did as follows. Meantime I was using multiDex in the project.

1. add this for build.gradle in module: app

multiDexEnabled = true

dexOptions {
    javaMaxHeapSize "4g"
}

2. add this dependancy

compile 'com.android.support:multidex:1.0.1'

3.Then in the MainApplication

public class MainApplication extends MultiDexApplication {

private static MainApplication mainApplication;

@Override
public void onCreate() {
    super.onCreate();
    mainApplication = this;
}

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


public static synchronized MainApplication getInstance() {
    return mainApplication;
}
}

4.In the manifests file

<application
    android:allowBackup="true"
    android:name="android.support.multidex.MultiDexApplication"

This works for me. Hope this Helps you too :)

thilina Kj
  • 1,300
  • 13
  • 25
  • 4
    You shouldn't do all three jobs (extend MultiDexApplication, implement attacheBaseContext and change the menifest file) in order to make enable multidex only one is enough :D. https://developer.android.com/studio/build/multidex.html – Hossein Shahdoost May 31 '17 at 09:42
  • If your minSdk is 21 or higher, you shouldn't have to do any of this as the docs say it's already enabled by default. And yet, this problem is coming for me with minSdk = 21, any solutions for this? – Aditya Anand Feb 14 '20 at 10:25
15

you should be sure

to add this line at your manifest

https://developer.android.com/studio/run/index.html#instant-run

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.multidex.myapplication">
    <application
        ...
        android:name="android.support.multidex.MultiDexApplication">
        ...
    </application>
</manifest>
Juan Salamanca
  • 354
  • 4
  • 9
7

Don't include the whole play services library but use the one that you need.Replace the line in your build.gradle:

compile 'com.google.android.gms:play-services:9.6.1'

with the appropriate one from Google Play Services APIs,like for example:

compile 'com.google.android.gms:play-services-gcm:9.6.1'
Ayan
  • 8,192
  • 4
  • 46
  • 51
7

Add this to your module-level build.gradle :

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

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

If you override Application class then extend it from MultiDexApplication :

YourApplicationClass extends MultiDexApplication

If you cant extend it from MultiDexApplication class then override attachBaseContext() method as following :

protected void attachBaseContext(Context base) {
     super.attachBaseContext(context);
     Multidex.install(this);
  }

And dont run anything before MultiDex.install(this) is executed.

If you dont override the Application class simply edit your manifest file as following :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    .......>
     <application
         ......
            android:name="android.support.multidex.MultiDexApplication" >
        ...
    </application>
    ......
</manifest>
Nurlan Sofiyev
  • 499
  • 6
  • 8
3

This should works:

Step1:

defaultConfig {
    applicationId "service.ingreens.com.gpsautoon"
    minSdkVersion 17
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Step 2:

compile 'com.android.support:multidex:1.0.1'

Step 3: Take another class

public class MyApplication extends MultiDexApplication {
}

Step 4: Add this line on manifest

<application
    android:name="android.support.multidex.MultiDexApplication">
</application>
JRL
  • 3,363
  • 24
  • 36
Soumen Das
  • 1,292
  • 17
  • 12
2

In my case, the problem was solved by adding this line to the module build.gradle:

// ADD THIS AT THE BOTTOM

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

Source

Borja
  • 1,269
  • 1
  • 17
  • 30
1

For react native app, the error was java.lang.RuntimeException: Unable to get provider com.google.firebase.provider.FirebaseInitProvider: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.provider.FirebaseInitProvider" It was only getting for devices with Android Version < ~4.4

Solved it by just replacing Application in MainApplication.java with MultiDexApplication

NOTE: import android.support.multidex.MultiDexApplication;

Malith
  • 381
  • 6
  • 18
1

In my case the problem happened after we migrated to AndroidX. For some reason, app was calling MultiDex.install() with reflection:

    final Class<?> clazz = Class.forName("android.support.multidex.MultiDex");
    final Method method = clazz.getDeclaredMethod("install", Context.class);
    method.invoke(null, this);

I changed package from android.support.multidex.MultiDex to androidx.multidex.MultiDex. It worked.

cgr
  • 4,578
  • 2
  • 28
  • 52
1

If you enable "multidex" then the issue will be resolved but this issue is all about Android studio cache. So before adding "multidex" clear cache hope the issue will be resolved.

Delete .gradle, .idea and .build file after that clean cache.

Android studio > File > Invalidate Caches / Restart

enter image description here

Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60
0

Instead of manually adding the package name on the build.gradle, you can do it this way:

first add this line at the beggining

import java.util.regex.Pattern

Then add this on the defaultConfig

android {
    ...
    defaultConfig {
        ...
        applicationId = doExtractStringFromManifest("package")
        ...
    }
    ...
}

And finally add the doExtractStringFromManifest method

def doExtractStringFromManifest(name) {
     def manifestFile = file(android.sourceSets.main.manifest.srcFile)
     def pattern = Pattern.compile(name + "=\"(\\S+)\"")
     def matcher = pattern.matcher(manifestFile.getText())
     matcher.find()
     return matcher.group(1)
}

As there are a lot of Cordova comments on the answer, if you are working with Cordova, you shouldn't really edit the build.gradle yourself, it has a comment at the beggining that say

// GENERATED FILE! DO NOT EDIT!

So, if you are using a Cordova, the best thing you can do is to update to cordova-android 5.1.0 or greater where this changes are already present.

jcesarmobile
  • 51,328
  • 11
  • 132
  • 176
0

Mine was because Firebase SDK in Gradle file was set to a wrong number version.

I removed the Firebase debs from Gradle and re-installed them again using Firebase Assistant

Suhaib
  • 2,031
  • 3
  • 24
  • 36
0

Go to android studio setting (by pressing Ctrl+Alt+S in windows), search for Instant Run and uncheck Enable Instant Run.

By disabling Instant Run and running your application again, problem will be resolved.

VSB
  • 9,825
  • 16
  • 72
  • 145
0

I added the following code in proguard file.

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application

and it worked in my case.

Ananta Prasad
  • 3,655
  • 3
  • 23
  • 35
0

in my case, I forget to add (or deleted accidentally) firebase core in build gradle

implementation 'com.google.firebase:firebase-core:xx.x.x'
Alexa289
  • 8,089
  • 10
  • 74
  • 178
  • 2
    3 months have passed and this is now deprecated: DO NOT ADD THIS! Source = https://firebase.google.com/support/release-notes/android (Obligatory: Thanks Google) – Aditya Anand Feb 14 '20 at 10:21
0

I've got this error just on devices with API lower that 21. In my case, I have had to work with a project where multiDexEnabled option in build.gradle was already set to true. I checked dex file from APK and referenced methods number was less than 64k, so project doesn't need to be a multidex one, therefore I set multiDexEnabled to false. This solution worked for me.

0

I had the same issue and fixed by using project level crashlytics gradle version 2.1.1

classpath 'com.google.firebase:firebase-crashlytics-gradle:2.1.1'
Amir Raza
  • 2,320
  • 1
  • 23
  • 32