情況是這樣子的,導入一個比較老的項目(兩年前),它依賴於一個 Libraray,已經先導入了 library,現在導入項目的時候出了錯
(1) Android Studio 目前提供將 SDK包成 .aar 檔案格式的方式,此方式除了將 class 包入之外,也會將資源、圖片等,都一起包入。而以前所使用的 .jar 只會將相關的 class 包入,所以在以前將資源文件一起導入有時候會出問題
(2) 所有的資源文件會被 merge 在一起,什么意思呢?就是如果你自己做的SDK中包了一個 layout 叫做 abc.xml,當有個 project 使用你的SDK,而且這個 project 也有一個 layout/abc.xml,在將你的 SDK include 到 project 以后,build 的過程中,SDK中的abc.xml會和 project 中的abc.xml合並(或是取代)。
(3) 因為(2)的原因,所有的 resource file name 或是 resource id 都記得加上 prefix 或是 postfix,用 darkwing_co_abc.xml 或是 abc_darkwing_co.xml 的方式命名。不會因為合並或取代造成未知的錯誤。
(4) 有時候在編譯的時候,遇到 attribute 重復的情況會回報錯誤,eg:
Error:Execution failed for task ':app:processDebugManifest'. > Manifest merger failed : Attribute application@icon value=(@mipmap/ic_launcher) from AndroidManifest.xml:10:9-43 is also present at [com.pnikosis:materialish-progress:1.0] AndroidManifest.xml:13:9-45 value=(@drawable/ic_launcher) Suggestion: add 'tools:replace="android:icon"' to element at AndroidManifest.xml:8:5-22:19 to override
這是因為 manifeast file 中某些 attribute 與 project 中的 minifeast 的 attribute 有重復,像是上面的例子,是說這兩個地方都有 ic_launcher,所以編譯器不知道用哪一個
這個時候可以指定下面的方式讓編譯器知道:
tools:replace=”android:icon,android:theme”
<?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="co.darkwing.bookingapp" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme_darkwing_co" tools:replace="android:icon,android:theme"> <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
注意, attribute 報錯時候,什么報錯就替換什么,我的 Theme 也報錯了,替換之后無效,我就直接去掉一些Activity 的Theme,然后成功運行
原文:https://blog.csdn.net/u011033906/article/details/59577340