最近接了一個案子,需要將整個APP包成一個SDK(Library),記錄一些注意事項
(1) Android Studio 目前提供將SDK包成 .aar 檔案格式的方式,此方式除了將 class 包入之外,也會將資源檔、圖片等,都一起包入。而以前所使用的 .jar 檔只會將相關的 class 包入,所以在以前將資源檔一起包入會是個頭痛的問題。
(2) 所有的資源檔案會被 merge 在一起,什麼意思呢?就是如果你自己製作的SDK中包了一個 layout 叫做 abc.xml,當有個 project 使用你的SDK,而且這個專案也有一個 layout/abc.xml,在將你的 SDK include 到專案以後,build 的過程中,SDK中的abc.xml會和主專案中的abc.xml合併(或是取代)。
(3) 因為(2)的原因,所有的 resource file name 或是 resource id 都記得加上 prefix 或是 postfix,像我通常是用 darkwing_co_abc.xml 或是 abc_darkwing_co.xml 的方式命名。才不會因為合併或取代造成未知的錯誤。
(4) 有時候在編譯的時候,遇到 attribute 重複的狀況會回報錯誤,類似下面狀況
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 設定,與主專案中的 minifeast 的 attribute 設定有重複,像是上面的例子,是說有兩個地方都設定了 ic_launcher,所以編譯器不知道要使用哪一個。
這個時候可以透過下列方式指定給編譯器知道,
<?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>
這個告訴你要提換哪些東西
tools:replace="android:icon,android:theme"
這個告訴你要使用 tools 命名空間
xmlns:tools="http://schemas.android.com/tools"