Android Material 与 appcompat Manifest 合并失败

Android Material and appcompat Manifest merger failed

我有下一年级

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    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'
    implementation 'com.google.android.material:material:1.0.0-rc01'
}

但是当我想要构建应用程序时,我得到下一条日志:

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

好的!转到清单并执行它:

<?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="ru.chopcode.myapplication">

    <application
        tools:replace="android:appComponentFactory"
        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">
    </application>

</manifest>

然后我在 Logcat:

中收到此错误
Manifest merger failed with multiple errors, see logs that I have Linked with it

创建新项目并比较您的 build.gradle 文件并替换所有

implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.1'

和其他与新项目中相同的依赖项 像那样

implementation 'androidx.appcompat:appcompat:1.0.0-alpha3'

implementation 'androidx.constraintlayout:constraintlayout:1.1.2'

implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-alpha1'

androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'

androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'

implementation 'androidx.core:core-ktx:1.0.0-alpha3'

然后修复导入以在 kotlin 文件中使用 androidx。

只需将依赖项中第一行和最后一行的 "rc01" 更改为 "alpha1" 这对我有用

只需删除 android.support 依赖项

    implementation 'com.android.support:appcompat-v7:28.0.0-rc01'

引用自Material 指南

If you don’t want to switch over to the new androidx and com.google.android.material packages yet, you can use Material Components via the com.android.support:design:28.0.0-alpha3 dependency.

Note: You should not use the com.android.support and com.google.android.dependencies in your app at the same time.

只需删除此依赖项即可正常工作

    implementation 'com.android.support:appcompat-v7:28.0.0-rc01'

这是完整的依赖示例

    dependencies {
       implementation fileTree(dir: 'libs', include: ['*.jar'])
       implementation 'com.google.android.material:material:1.0.0-beta01'
       implementation 'com.android.support.constraint:constraint-layout:1.1.2'
       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'
   }

从 Android P 开始,支持库已移至 AndroidX

请重构您的 libs from this page

https://developer.android.com/topic/libraries/support-library/refactor

我有类似的问题。在 gradle.properties 文件中添加了两行:

android.useAndroidX=true
android.enableJetifier=true

这两行自动解决了我在google的文件和第三方依赖项之间的依赖项冲突。以下是 link:https://developer.android.com/topic/libraries/support-library/androidx-overview#new-project

就我而言,这是完美的.. 我在清单文件中添加了下面两行代码

tools:replace="android:appComponentFactory"
android:appComponentFactory="whateverString"

功劳 to this answer

我在尝试将 Kotlin-KTX 库添加到我的项目时遇到了同样的错误。

我尝试迁移到 AndroidX,问题已解决!

首先一定要在清单标签

中添加这一行
xmlns:tools="https://schemas.android.com/tools"

然后在 Android studio

中添加工具替换您建议的工具

仅当你不想使用androidx时

所以在我的例子中,我使用的是一个使用 androidx 的库,但我没有使用 androidx,所以当我降级那个库版本时,问题就解决了。

以我为例:

implementation 'com.github.turing-tech:MaterialScrollBar:13.+'

上述库出现问题13.+会自动获取一个使用androidx的新版本库。所以我将库版本降级为:

implementation 'com.github.turing-tech:MaterialScrollBar:13.2.5'

问题已解决。

将您的 build.gradle 依赖项更改为

 implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'

 implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'

按照以下步骤操作:

  • 转到重构并单击迁移到 AndroidX
  • 单击执行重构

我在使用 Groupie 时遇到了同样的错误。 implementation 'com.xwray:groupie:2.3.0'

我把版本改成implementation 'com.xwray:groupie:2.1.0'

解决了

失败原因

您正在使用 material 库,它是 AndroidX 的一部分。 如果您不知道 AndroidX,请通过 .

One app should use either AndroidX or old Android Support libraries. That's why you faced this issue.

例如-

在您的 gradle 中,您正在使用

  • com.android.support:appcompat-v7(旧的一部分--Android支持库--)
  • com.google.android.material:material(AndroidX 的一部分)(AndroidX com.android.support:design 的构建工件)

解决方案

所以解决方案是使用 AndroidX 或旧的支持库。我推荐使用AndroidX,因为Android在28.0.0版本之后不会更新支持库。请参阅支持库的 release notes

只需迁移到AndroidX。是我迁移到AndroidX的详细答案。我把这个答案的必要步骤放在这里。

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

Existing project

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

New project

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

android.enableJetifier=true
android.useAndroidX=true

检查@.

检查@

此问题主要发生在旧依赖项

有2种解法:

第一个:

更新 Project level gradle 个文件中的所有 old dependenciesClassPaths

classpath 'com.android.tools.build:gradle:3.3.1'
classpath 'com.google.gms:google-services:4.2.0'

第二个:

你的项目Migrate to AndroidX

来自 Android Studio 菜单 -> Refanctor -> Migrate to AndroidX

谢谢,如果有人对此答案有帮助,请告诉我。

在某些情况下我不想传递给 androidX。而且我必须检查我的最后更改。我发现新组件是 --> lottie 2.8.0

所以我将其降级为:implementation 'com.airbnb.android:lottie:2.7.0'

将 build.gradle 中的 proguardFile(模块:app)更改为以下内容

proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

而不是

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

这对我有用

1.Added 这些代码在你的 app/build.gradle:

的末尾
configurations.all {
   resolutionStrategy.force 'com.android.support:support-v4:28.0.0' 
   // the above lib may be old dependencies version       
    }

2.Modified sdk 和工具版本到 28:

compileSdkVersion 28
buildToolsVersion '28.0.3'
targetSdkVersion  28

3.In 你的 AndroidManifest.xml 文件,你应该添加两行:

<application
    android:name=".YourApplication"
    android:appComponentFactory="AnyStrings"
    tools:replace="android:appComponentFactory"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar">

或者干脆

  • 转到重构(工作室 -> 菜单 -> 重构)
  • 点击迁移到 AndroidX。正常工作
  • 它正在工作。

简单的解决方案 - 迁移到 AndroidX

gradle.properties中,只需添加以下两个脚本

android.useAndroidX=true
android.enableJetifier=true

这是什么原因↓

All packages in AndroidX live in a consistent namespace starting with the string androidx. The Support Library packages have been mapped into the corresponding androidx.* packages. For a full mapping of all the old classes and build artifacts to the new ones, see the Package Refactoring page.

请看the Package Refactoring page

  • 转到重构 (Image )
  • 点击迁移到 AndroidX。正常工作
  • 它正在工作。

只需将这两行代码添加到您的清单文件中

tools:replace="android:appComponentFactory"
android:appComponentFactory="whateverString"

通过在 android/build.gradle 文件中添加两行,终于找到了一个快速简便的解决方案。

googlePlayServicesVersion = "16.+"

firebaseVersion = "17.6.0"

请100%正确。

在 AndroidManifest.xml 文件中添加这些行。

tools:replace="android:appComponentFactory"

android:appComponentFactory="whateverString"

我遇到同样的问题已经三天了。

就像 android 希望我们使用 Kotlin 或 AndroidX,他们正在取消 Android。

但我就是这样解决的。

  • 我将我的项目迁移到了 AndroidX。这不会带来与此问题相关的任何错误,它会分别更新文件。无需在您的应用程序中更改任何方法、class 或变量名称。

注意:您必须在 android studio 3.4.1

的最新稳定版本中使用最新的库(build.grudle 文件)

如果您不打算迁移到 AndroidX,请按照以下步骤操作

  1. 在终端输入 gradlew app:dependencies
  2. 在 window 中显示依赖关系后,按 Ctrl+f 并输入 androidX
  3. 您会发现所有在内部使用 androidx 库的依赖项,您可能必须对其进行降级或使用替代方案,以确保它不再在内部使用 androidX 库。

我所做的只是转到顶部菜单上的 "Refactor" 选项。

然后select"Migrate to AndroidX"

接受将项目另存为 zip 文件。

请更新 Android Studio 以及 Gradle 以确保不会遇到问题。

为了解决这个问题,我建议明确定义 ext 变量的版本 在 android/build.gradle 在你的根项目

ext {
    googlePlayServicesVersion = "16.1.0" // default: "+"
    firebaseVersion = "15.0.2" // default: "+"

    // Other settings
    compileSdkVersion = <Your compile SDK version> // default: 23
    buildToolsVersion = "<Your build tools version>" // default: "23.0.1"
    targetSdkVersion = <Your target SDK version> // default: 23
    supportLibVersion = "<Your support lib version>" // default: 23.1.1
}

参考 https://github.com/zo0r/react-native-push-notification/issues/1109#issuecomment-506414941

一切都是关于库版本兼容性

我面对这个奇怪的错误有两个小时了。 我通过执行这些步骤解决了这个错误

将您的 build.gradle 依赖项更改为

  implementation 'com.google.android.gms:play-services-maps:17.0.0'

  implementation 'com.google.android.gms:play-services-maps:15.0.0'
  1. 请转到重构->迁移->迁移到 Android X。

  2. 请将此添加到您的 gradle.properties 文件中:

    android.enableJetifier=true
    android.useAndroidX=true
    

并执行同步。

如果是现有项目,请迁移到 androidX。如果是新项目,将这两个标志添加到 Gradle.propeties

android.enableJetifier=true  
android.useAndroidX=true

更多详细步骤https://developer.android.com/jetpack/androidx 或检查此

查看此图片并将此行添加到您的 android AndroidManifest.xml

tools:replace="android:appComponentFactory"
android:appComponentFactory="whateverString"

我也遇到了同样的问题, 对于您的 android 工作室,您只需将 android Gradle 插件版本更改为 3.3.2 Gradle 版本是 5.1.1

我不知道这是不是正确的答案,但它对我有用

增加Gridle Wrapper 属性

distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip

并构建 Gridle 以

    classpath 'com.android.tools.build:gradle:3.4.2'

由于版本原因显示错误。

在我的例子中,我在清单文件中添加了`

tools:replace="android:appComponentFactory"
android:appComponentFactory="whateverString"

当然是应用标签, 它会起作用

如果您使用的是电容器,请在 package.json

中升级到 capacitor/core 2.0.1 或更高版本

试试这个

tools:replace="android:appComponentFactory"
android:appComponentFactory="android.support.v4.app.CoreComponentFactory"

重建您的项目。