项目运行重新编译,编译到一半就报错了,真是一脸懵逼。好好的怎么就突然报错了?错误信息如下:
• Error
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-91is also present at [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory).Suggestion: add 'tools:replace="android:appComponentFactory"' toelement at AndroidManifest.xml:6:5-21:19 to override.复制代码
按照日志提示添加在<application>标签里面加
上:tools:replace="android:appComponentFactory"
复制代码
添加完后,重新编译依旧错误:
Caused by: java.lang.RuntimeException: Manifest merger failed with multiple errors, see logs at com.android.builder.core.AndroidBuilder.mergeManifestsForApplication(AndroidBuilder.java:556) at com.android.build.gradle.tasks.ProcessApplicationManifest.doFullTaskAction(ProcessApplicationManifest.java:198) at com.android.build.gradle.internal.tasks.IncrementalTask.taskAction(IncrementalTask.java:106) at sun.reflect.GeneratedMethodAccessor378.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73) at org.gradle.api.internal.project.taskfactory.IncrementalTaskAction.doExecute(IncrementalTaskAction.java:50) at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:39) at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:26) at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:131) at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:300) at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:292) at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:174) at org.gradle.internal.operations.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:90) at org.gradle.internal.operations.DelegatingBuildOperationExecutor.run(DelegatingBuildOperationExecutor.java:31) at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:120) at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:99) ... 115 more复制代码
几番折腾后,发现是 Androidx库和 Android Support库不能共存的问题,一山不容二虎。可是项目并没迁移Androidx库,怎么会出现 Androidx库呢?
•SolutionNo1. 转换成Androidx 在 AS 3.2 及以上版本提供了更加方便快捷的方法一键迁移到 AndroidX。
No2. 转换成Android Support 1)在 Terminal 中输入gradlew :app:dependencies
命令,查看第三方库的依赖树并找出引用了 androidx的库;
implementation 'com.github.chrisbanes:PhotoView:latest.release'
引用了 Androidx库; 3)进入PhotoView 项目地址,查看更新内容; 4)根据更新内容发现,从 2.2.0
版本开始引用 androidx库,那我们尝试回退到 2.1.4
版本; implementation 'com.github.chrisbanes:PhotoView:2.1.4'
5)点击 Sync now 重新编译,发现 Gradle编译成功,依赖 androidx库也消失了。看来问题解决了,和我们之前分析预想的一样。 • Notes 1)许多第三方库并没有迁移 androidx库,所以项目在使用 androidx库时应当注意兼容问题;
2)在引用依赖库的时候,尽量避免使用latest.release或+方式,而是使用具体的版本。以免出现新旧库兼容,编译时间久等问题。