清单合并失败:属性 application@appComponentFactory - Androidx

Manifest merger failed : Attribute application@appComponentFactory - Androidx

当我使用 androidx 构建 Gradle 时,出现以下错误。请帮我解决这个问题。

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).

清单:

<?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'
}

找出确切原因的一个建议是打开清单文件,在底部您会看到一个 Merge Manifest 选项,您会在其中看到失败的确切原因。

见下图

将这些标志放入您的 gradle.properties

android.enableJetifier=true
android.useAndroidX=true

错误明确地说-

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

AndroidX 是来自 Google 的最新支持库。它包含 all older appcompat versions 中的所有先前组件。 不要使用 appcompat-v-任何数字。相反,使用 AndroidX 库中的类似组件。从您的 Gradle 和导入的代码中删除带编号的支持库。然后同步你的 gradle.

组件相似性 table 可以找到 here. Also, follow the steps mentioned in Migrating to AndroidX

再次,停止使用任何以前的 appcompat 编号版本。现在只有AndroidX.

希望对您有所帮助。

经过几个小时的努力,我通过在 app/build.gradle 中包含以下内容解决了这个问题:

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

将这些标志放入您的 gradle.properties

android.enableJetifier=true
android.useAndroidX=true

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'

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

这个错误的原因-

因为升级后,androidx.core:core在某处被访问,而你的项目还没有使用androidx。所以 类 和 CoreComponentFactory 一样,现在可以在两个地方找到 - androidx.core:corecom.android.support:support-compat。这就是发生此错误的原因。

什么是解决方案?

您应该迁移到 AndroidX。如果您不知道 AndroidX。请阅读

如何迁移您的项目

在 Android Studio 3.2(2018 年 9 月)之后,可以直接选择将现有项目迁移到 AndroidX。这会自动折射所有包。

迁移之前,强烈建议备份您的项目。

Existing project

  • Android Studio > 重构菜单 > 迁移到 AndroidX...
  • 它将分析并在底部打开 Refractor window。接受要完成的更改。

New project

将这些标志放入您的 gradle.properties

android.enableJetifier=true
android.useAndroidX=true

检查@Library mappings for equal AndroidX package.

检查@Official page of Migrate to AndroidX

项目范围 Gradle 设置。 IDE(例如 Android Studio)用户: Gradle 通过 IDE 配置的设置将覆盖 此文件中指定的任何设置。有关如何配置构建环境的更多详细信息,请访问 http://www.gradle.org/docs/current/userguide/build_environment.html

指定用于守护进程的 JVM 参数。该设置对于调整内存设置特别有用

org.gradle.jvmargs=-Xmx1536m
android.enableJetifier=true
android.useAndroidX=true

配置后,Gradle 将 运行 处于孵化并行模式。此选项应仅用于解耦项目。更多详情,请访问 http://www.gradle.org/docs/current/userguide/multi_project_builds.html

sec:decoupled_projectsvorg.gradle.parallel=true

只需在 gradle.properties

中添加一行
android.enableJetifier=true
android.useAndroidX=true

我也遇到了这个问题,因为我在我的项目中使用了一些外部库,其中一个没有转换成 AndroidX.

将以下代码添加到 buildscript ext

下的 android/build.gradle
googlePlayServicesVersion = "16.0.0"
googlePlayServicesVisionVersion = "17.0.2" 

和下面的代码 gradle.properties

android.enableJetifier=true
android.useAndroidX=true

您必须继续使用 Androidx,因为您的项目正在使用某些功能 来自 there.so 您需要迁移到 AndroidX 按照这些片段

看看第二个片段

我让 Android Studio 将我的 Relative layout 视图转换为 Constraint layout。所以 Android Studio 添加了 com.andriod.support... 之一,而我添加了 androidx... 依赖项,当我删除第二个时,错误消失了。

dependencies {
    implementation "androidx.constraintlayout:constraintlayout:2.1.0"
    implementation 'com.android.support.constraint:constraint-layout:2.0.4'
}

这是我的错误:

Manifest merger failed : Attribute application@appComponentFactory value=(androidx.core.app.CoreComponentFactory) from [androidx.core:core:1.3.2] AndroidManifest.xml:24: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 <application> element at AndroidManifest.xml:7:3-26:17 to override.
 implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:one.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

在清单中将 tools:replace="android:theme" 添加到您的应用程序