Android Manifest Merger fails after adding Dependency com.google.android.material

你说的曾经没有我的故事 提交于 2019-12-04 22:57:42

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>

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.

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

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.

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.

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.

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"> 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!