Android Manifest Merger fails after adding Depende

2020-08-10 07:41发布

问题:

We would like to use the most current material design ui elements presented by Google. Therefore I added com.google.android.material:material:1.0.0-beta01 in our projects gradle file. But since then I get the following error.

Manifest merger failed : Attribute application@appComponentFactory value=(androidx.core.app.CoreComponentFactory) from [androidx.core:core:1.0.0-beta01] AndroidManifest.xml:22:18-86 is also present at [com.android.support:support-compat:28.0.0-beta01] AndroidManifest.xml:22:18-91 value=(android.support.v4.app.CoreComponentFactory). Suggestion: add 'tools:replace="android:appComponentFactory"' to element at AndroidManifest.xml:5:5-40:19 to override.

Without the material library everything works fine.

What I already tried

  1. I changed the ordering of the dependencies (which did obviously not work)
  2. I also tried to add tools:replace="android:appComponentFactory" to our Manifest file as suggested in the error message. But this left me with a new error message:

java.lang.RuntimeException: Manifest merger failed with multiple errors, see logs at com.android.builder.core.AndroidBuilder.mergeManifestsForApplication(AndroidBuilder.java:524) at com.android.build.gradle.tasks.ProcessManifest.doFullTaskAction(ProcessManifest.java:75) at com.android.build.gradle.internal.tasks.IncrementalTask.taskAction(IncrementalTask.java:106)

I checked the log file but the only warning shown in idea.log is

2018-08-08 11:08:21,500 [thread 267] WARN - .tools.idea.model.ManifestInfo - getMergedManifest failed Manifest merger failed with multiple errors, see logs 2018-08-08 11:08:21,642 [thread 267] WARN - .tools.idea.model.ManifestInfo - getMergedManifest failed Manifest merger failed with multiple errors, see logs 2018-08-08 11:08:21,917 [J pool 0/7]

More information

API Version

android {
compileSdkVersion 28
buildToolsVersion '28.0.0'

defaultConfig {
    minSdkVersion 19
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
}

(the same API is chosen in file > project structure in Android Studio)

Dependencies:

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

/** ANDROID LIBRARIES  **/
implementation 'com.android.support:multidex:1.0.2'
implementation 'com.android.support:appcompat-v7:28.0.0-beta01'
api 'com.android.support:support-v13:28.0.0-beta01'
api 'com.google.android.material:material:1.0.0-beta01'
api 'com.android.support:design:28.0.0-beta01'
api 'com.android.support:preference-v7:28.0.0-beta01'
api 'com.android.support:cardview-v7:28.0.0-beta01'
api 'com.android.support:customtabs:28.0.0-beta01'
api 'com.android.support:support-v4:28.0.0-beta01'

/** GOOGLE PLAY SERVICE  **/
api 'com.google.android.gms:play-services-auth:15.0.1'

api 'com.google.android.gms:play-services-drive:15.0.1'
api 'com.google.android.gms:play-services-fitness:15.0.1'
api 'com.google.android.gms:play-services-maps:15.0.1'
api 'com.google.android.gms:play-services-wearable:15.0.1'
api 'com.google.android.gms:play-services-vision:15.0.2'

api 'com.google.firebase:firebase-core:16.0.1'
api 'com.google.firebase:firebase-perf:16.0.0'
api 'com.crashlytics.sdk.android:crashlytics:2.9.4'

//sunrise and sundown calculation for map day and night mode
api 'com.luckycatlabs:SunriseSunsetCalculator:1.2'

/** GOOGLE MAPS  **/
implementation 'com.google.maps.android:android-maps-utils:0.5'
implementation 'com.google.code.gson:gson:2.8.0'

/** EXTERNAL LIBRARIES  **/

implementation 'org.osmdroid:osmdroid-android:5.6.5'
implementation 'org.osmdroid:osmdroid-mapsforge:5.6.5'
implementation 'org.mapsforge:mapsforge-map-android:0.6.1'
implementation 'org.mapsforge:mapsforge-map:0.6.1'
implementation 'com.caverock:androidsvg:1.2.2-beta-1'

//dropbox api
implementation 'com.dropbox.core:dropbox-core-sdk:3.0.6'    

androidTestImplementation 'junit:junit:4.12'
implementation files('libs/samsung-health-data-v1.3.0.jar')}

Manifest

<manifest
package="com.user.libraries.bluetooth"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BODY_SENSORS" />
<uses-permission android:name="android.permission.VIBRATE"/>    

<application
    tools:replace="android:appComponentFactory"
    android:allowBackup="true"
    android:supportsRtl="true">
    <service android:name="com.user.libraries.location.LocationService" />
    <service android:name="com.user.libraries.location.BleLocationTrackerService" />
    <service android:name="com.user.libraries.sensors.SensorService"/>

    <service android:name="com.user.libraries.sharing.DataExchangerService"/>

    <receiver
        android:name="com.user.libraries.bluetooth.BluetoothStateReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
        </intent-filter>
    </receiver>

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

</application>

Any idea how I can solve this issue?

回答1:

Main problem here is that you are adding

com.google.android.material:material:1.0.0-beta01

that belongs to newly released androidx package and nowadays it is not compatible with android support library that you have in your dependencies.

You have 2 options:

  1. Replace com.google.android.material:material:1.0.0-beta01 with com.android.support:design:28.0.0-beta01 (see all support libraries https://developer.android.com/topic/libraries/support-library/packages) (that I recommend)
  2. Use Android Studio option Refactor to AndroidX (see https://material.io/develop/android/docs/getting-started/) (I do not recommend)

Here is sample of working code:

 <android.support.design.widget.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:boxStrokeColor="@color/colorAccent"
        app:boxStrokeWidth="5dp">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/messageEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

 </android.support.design.widget.TextInputLayout>


回答2:

Don't forget to add

android.useAndroidX=true
android.enableJetifier=true

to your gradle.properties. I always forget this when I get on a new machine because gradle.properties is not in source control. Would be great if we got a sensible error in this case.



回答3:

I have this problem too. When use this library in dependencies

implementation 'com.google.android.material:material:1.0.0'

Give this :

Manifest merger failed : Attribute application@appComponentFactory value=(androidx.core.app.CoreComponentFactory) from [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 is also present at [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91 value=(android.support.v4.app.CoreComponentFactory). Suggestion: add 'tools:replace="android:appComponentFactory"' to element at AndroidManifest.xml:5:5-40:19 to override.

I have this problem by switching to AndroidX

Going to Refactor->Migrate to AndoridX (in toolbar of android studio 3.2 and above)

And my problem solve

refrence



回答4:

The problem is that AndroidX packages are not compatible with AppCompat support packages. You will need to choose one type or the other. AndroidX is newer and designed to simplify working with compatibilities of different Android versions. Google has a guide on Migrating to AndroidX from AppCompat.

The upgrade path is fairly straightforward but a bit cumbersome, but I'd recommend doing that in order to stay up to date rather than downgrading to the older design package.

Upgrading to use AndroidX

You can specify that you'd like to use AndroidX by adding the following to your gradle.properties file:

android.useAndroidX=true
android.enableJetifier=true

I found I needed to also add the following to my app/build.gradle file to get around the exception Default interface methods are only supported starting with Android N (--min-api 24), likely related to one of the newer packages:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

Next, you'll need to change all your AppCompat packages in the app/build.gradle to use the matching AndroidX package. You can see a list in the Artifact mappings section of the above-mentioned documentation which will provide the AppCompat to AndroidX mappings for all packages.

For example, the following are the same package, so you would ensure you're on the AndroidX version instead of the AppCompat one:

implementation 'com.android.support:design' # AppCompat version
implementation 'com.google.android.material:material:1.1.0-alpha08' # AndroidX version

When you try to compile, you should get errors because you likely need to migrate your Activities to use the newer packages. For me, I had to make the following changes, removing the support library version and replacing it with the AndroidX version:

# remove
-import android.support.design.widget.FloatingActionButton
-import android.support.design.widget.Snackbar
-import android.support.v4.app.ActivityCompat
-import android.support.v4.content.ContextCompat
-import android.support.v7.app.AlertDialog
-import android.support.v7.app.AppCompatActivity
-import android.support.v7.widget.Toolbar
# add
+import com.google.android.material.floatingactionbutton.FloatingActionButton
+import com.google.android.material.snackbar.Snackbar
+import androidx.appcompat.app.AlertDialog
+import androidx.appcompat.app.AppCompatActivity
+import androidx.appcompat.widget.Toolbar
+import androidx.core.app.ActivityCompat
+import androidx.core.content.ContextCompat

In the Kotlin and Java files, this is straightforward (as above), but the compiler is not helpful when it comes to the layout files, which will throw layout inflation exceptions when they are inflated at runtime. You will need to also change them in your layout XML files:

-        <android.support.design.widget.TextInputLayout
+        <com.google.android.material.textfield.TextInputLayout

During this process you will likely be prompted to upgrade Gradle which is a good idea.

Though somewhat cumbersome, I think the upgrade path is worth it to be able to continue to use new libraries as they come out.

It appears as though newer versions of Android Studio have some automation in place to help you migrate, available in the menu: Refactor > Migrate to AndroidX.



回答5:

Recently google wants to separate the dependency. They re-name the packages and they suggest to use androidx from now. So latest google gradle version's are all under androidx.

You need to migrate your project android app compact to androidx for this. You need to use some properties into prgradle.properties file:

 android.useAndroidX=true
 android.enableJetifier=true

and you need to migrate your project from AppCompact to Androidx for this you can do it using tools just

Going to Refactor->Migrate to AndoridX (in toolbar of android studio 3.2 and above)

After that you need to upgrade your regular gradle to androidx gradle you will find it in google official page like

https://developer.android.com/jetpack/androidx/migrate

From this list just pick the alter androidx gradle and build your project will be fine.



回答6:

using Android stuido 3.4.1 I was able to select my project, select Refactor from main toolbar and then Migrate to AndroidX... this took a few moments and then there was a button 'Do Build', this again took a few moments and completed successfully.



回答7:

implementation 'com.google.android.material:material:1.0.0' 
to 
implementation 'com.android.support:design:28.0.0' 

replace

import com.google.android.material.textfield.TextInputLayout; 

with

import android.support.design.widget.TextInputLayout;

in your xml change

 <com.google.android.material.textfield.TextInputLayout
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    app:boxStrokeColor="@color/colorAccent"
    app:boxStrokeWidth="5dp">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/messageEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    </com.google.android.material.textfield.TextInputLayout>

to

   <android.support.design.widget.TextInputLayout
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    app:boxStrokeColor="@color/colorAccent"
    app:boxStrokeWidth="5dp">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/messageEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

   </android.support.design.widget.TextInputLayout>

finally in AndroidManifest add android:theme="@style/anothertheme" tools:replace="android:appComponentFactory"

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:name="com.yours.app"
        android:theme="@style/anothertheme"
        tools:replace="android:appComponentFactory"
        android:appComponentFactory="androidx">