Android Support库和AndroidX冲突问题
2019-03-18 本文已影响15人
翻译不了的声响
项目运行重新编译,编译到一半就报错了,真是一脸懵逼。好好的怎么就突然报错了?错误信息如下:
- 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-91
is 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"' to <application> element at AndroidManifest.xml:6:5-21:19 to override.
按照日志提示添加在<application>
标签里面加上:tools:replace="android:appComponentFactory"
<application
android:name=".application.MyApplication"
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"
tools:replace="android:appComponentFactory">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
添加完后,重新编译依旧错误:
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库呢?
依赖架包- Solution
No1. 转换成Androidx
在AS 3.2 及以上版本提供了更加方便快捷的方法一键迁移到 AndroidX。选择菜单上的ReFactor —— Migrate to AndroidX... 即可;
No2. 转换成Android Support
1)在 Terminal 中输入gradlew :app:dependencies
命令,查看第三方库的依赖树并找出引用了 androidx的库;
2)经查找发现是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
或+
方式,而是使用具体的版本。以免出现新旧库兼容,编译时间久等问题。