Gradle sync fails due to Manifest Merger

2019-08-17 08:36发布

问题:

I have been trying to link my app with Firebase, but after adding json file, and adding some codes in gradle , My android project fails at syncing gradle, this is the error I get when syncing gadle

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 <application> element at AndroidManifest.xml:13:5-31:19 to override.

This is my app-level Gradle file:

apply plugin: 'com.android.application'

android {
compileSdkVersion 28
buildToolsVersion "29.0.0"
defaultConfig {
    applicationId "app.freeairdrop.io"
    minSdkVersion 16
    targetSdkVersion 28
    versionCode 4
    versionName "1.2"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.firebase:firebase-core:17.0.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'

回答1:

The reason why it is happening is that Google encourages developers to move to AndroidX flavored dependencies. Old support libraries will be deprecated. So if you still use old support libraries and add a newer updated dependency that makes use of AndroidX, there is a clash between them, hence such error is produced.

Go to Refactor tab and select "Migrate to AndroidX". Follow the instructions. When the conversion is done, clean/rebuild the project. Chances are, after the conversion, your code may contain errors(Unresolved references on imported classes), but that is perfectly fine. All you have to do is to reimport the code you have written but this time with androidx libraries.