'gradle not syncing when adding firebase to android studio
So I'm trying to add the Firebase SDK to my Android Studio app as per the instructions from the Firebase website (see picture).
Adding the line classpath 'com.google.gms:google-services:4.3.2'
to the project level gradle works fine. I can sync the gradle files.
However, adding the line apply plugin: 'com.google.gms.google-services'
to the app level gradle file breaks my app.
I get the following error:
ERROR: Manifest merger failed : Attribute application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91 is also present at [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory). Suggestion: add 'tools:replace="android:appComponentFactory"' to element at AndroidManifest.xml:5:5-21:19 to override.
So i go to do the suggestion. I add tools:replace="android:appComponentFactory"
to >application< element in AndroidManifest.xml file. This then gives me a Namespace "tools" not bound error. I press Alt+Enter to fix which adds xmlns:tools="http://schemas.android.com/tools"
to the top of my file.
Try sync again. Now i get a similar error: ERROR: Manifest merger failed with multiple errors, see logs. And this is where I'm lost. I'm not sure what to search in Google. a search for this specific error brings up a lot of answers that aren't related to my problem. This territory is completely new to me.
P.S. On top of all that, the line implementation 'com.android.support:appcompat-v7:28.0.0'
inside my build.grade(app level) file is underlined red after adding the previously mentioned 3 lines.
I noticed there's other questions related to this but none seem to have solid answers. So I'm taking the risk that this might be a repost.
EDIT:: Here are my gradle depencies.
Project Gradle
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
classpath 'com.google.gms:google-services:4.3.2'
}
App Gradle
dependencies {
implementation 'com.google.firebase:firebase-analytics:17.2.0'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
}
EDIT 2::
Ok after migrating to androidx, I updated all my import statements to use androidx. It still kept saying merger failed with multiple errors. I had to delete the line tools:replace="android:appComponentFactory"
that it had previously suggested to add. Finally I was able to sync and run my app! Except now the app just starts and stops immediately. I suppose that's progress. Any ideas on this?
EDIT 3::
I finally got it to work. The last issue was a RunTimeError "Error inflating class androidx.constraintlayout.ConstraintLayout". This is caused because all my xml files had the line androidx.constraintlayout.ConstraintLayout
. replaced this with the line androidx.constraintlayout.widget.ConstraintLayout
and everything is gravy now. Gradle syncs, app loads, doesn't crash and everything works as it's supposed to
Any help would be greatly appreciated. Thx in advance,
Vlad
Solution 1:[1]
The problem is your app is using android.support
libraries while the Firebase sdk you added to your project uses AndroidX
.
Note: AndroidX and android.support are not compatible.
So you have to either migrate your project to AndroidX or use a support version of Firebase.
Migrate code to use AndroidX
- From
Refactor
option choose Migrate to Androidx. - Sync the project and you should be fine.
- From
Use a Non-Androidx firebase.
- Go to Firebase release notes
- Find the last version that is before migrating library to AndroidX
(For instanceFirebase messaging
19 uses androidx and lower versions don't)
TL;DR
Migrate to AndroidX
or use a lower version library of Firebase.
Solution 2:[2]
In your application tag add tools:replace="android:appComponentFactory"
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:replace="android:appComponentFactory"
>
then gradle.properties add
android.enableJetifier=true
android.useAndroidX=true
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mahdi-Malv |
Solution 2 | Yang Yu |