Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manifest merger failed : Attribute application@appComponentFactory - Androidx

When I'm building Gradle with androidx I get the following error. Please help me to solve this issue.

Error Message: 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).

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.kanwarpreet.dealmybook">

    <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">

        <activity android:name=".activities.SplashActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".activities.LoginActivity" />
        <activity android:name=".activities.RegisterActivity" />
        <activity
            android:name=".activities.HomeActivity"
            android:label="@string/title_activity_home"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".activities.BookDetailsActivity"
            android:label="@string/title_activity_book_details"
            android:theme="@style/AppTheme.NoActionBar"/>
        <activity android:name=".activities.AddBookActivity" />
    </application>

</manifest>

Build.Gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.kanwarpreet.dealmybook"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        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.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'com.jakewharton:butterknife:10.1.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
    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'
}
like image 779
Kanwarpreet Singh Avatar asked Mar 18 '19 09:03

Kanwarpreet Singh


Video Answer


4 Answers

Put these flags in your gradle.properties

android.enableJetifier=true
android.useAndroidX=true
like image 67
VIISHRUT MAVANII Avatar answered Oct 21 '22 04:10

VIISHRUT MAVANII


After hours of struggling, I solved it by including the following within app/build.gradle:

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

Put these flags in your gradle.properties

android.enableJetifier=true
android.useAndroidX=true

Changes in build.gradle:

implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.material:material:1.1.0-alpha04'

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

like image 42
Kanwarpreet Singh Avatar answered Oct 21 '22 05:10

Kanwarpreet Singh


Reason of this error-

Because after upgrade, androidx.core:core is accessed somewhere, when your project is still not using androidx. So classes like CoreComponentFactory and many others are now found at two places - androidx.core:core and com.android.support:support-compat. That's why this error occured.

What is solution?

You should migrate to AndroidX. If you don't know about AndroidX. Please read What is AndroidX?

How to migrate your project

After Android Studio 3.2 (September 2018), there is direct option to migrate existing project to AndroidX. This refract all packages automatically.

Before you migrate, it is strongly recommended to backup your project.

Existing project

  • Android Studio > Refactor Menu > Migrate to AndroidX...
  • It will analysis and will open Refractor window in bottom. Accept changes to be done.

image

New project

Put these flags in your gradle.properties

android.enableJetifier=true
android.useAndroidX=true

Check @Library mappings for equal AndroidX package.

Check @Official page of Migrate to AndroidX

like image 37
Khemraj Sharma Avatar answered Oct 21 '22 04:10

Khemraj Sharma


Error explicitly says-

[com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91 is also present at [androidx.core:core:1.0.0]

AndroidX is the latest support library from Google. It contains all previous components from all older appcompat versions. Do NOT use appcompat-v-any number. Instead, use a similar component from AndroidX libraries. Remove the numbered support libraries from your Gradle and your code wherever it is imported. Then sync your gradle.

Component similarity table can be found here. Also, follow the steps mentioned in Migrating to AndroidX.

Again, stop using any previous appcompat numbered versions. There's only AndroidX now.

Hope this helps.

like image 5
MD Naseem Ashraf Avatar answered Oct 21 '22 05:10

MD Naseem Ashraf