若想同時安裝 development 版本和 production 版本到同一的 Android 機上,應如何做?
因為 applicationId
相同,不能同時安裝兩種版本。想用一個 overwrite 另一個,以便保留 setting 嗎?但因為 certificate 不同,Android 規定必須刪掉重裝。打算自己改 application Id 嗎?抱歉,必須連 package 名一起改,天知道會不會做成更多問題啊。
buildTypes
現在有 gradle ,我們可以簡單做到。在 debug buildTypes
加進 applicationIdSuffix
和 versionNameSuffix
便會自動修改 debug build 的 applicationId
。
android {
...
defaultConfig {
minSdkVersion 8
versionCode 10
}
buildTypes {
debug {
applicationIdSuffix '.debug'
versionNameSuffix '-SNAPSHOT'
}
release {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
這樣便可以同時裝 production 和 debug 版了。
Google Cloud Service 錯誤
如果有使用 GCM,在安裝時會有以下錯誤:
Failure [INSTALL_FAILED_DUPLICATE_PERMISSION perm=com.mysuperapp.app1.permission.C2D_MESSAGE pkg=com.mysuperapp.app1]
這是因為 AndroidManifest.xml
中有以下 permission
<permission android:name="com.thirtysparks.sunny.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.thirtysparks.sunny.permission.C2D_MESSAGE" />
Release 和 debug 版本都使用了同一 permission ,所以有 error 。解決方法很簡單,使用 ${applicationId}
代替你的 package name 便可。
<permission android:name="${applicationId}.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
若有使用 <provider>
的也是同一解決方法。
不同圖示和名稱
不過 Release 和 debug build 的名稱和圖示一樣,現在分不清那個才是 release 那個才是 debug 版,怎辦?幸好,Gradle 也有方法解決。
不同的 buildType
有其自家的 source directory。Release build 的 source files 會放在 /app/src/main
。Debug build 的 directory 則是 /app/src/debug/
。在 build 不同 buildType
時,Gradle 會用其所屬的 directory 去取代相關資料。
例如我們想改 launcher icons,我們可以在 app/src/debug
下新增 res/mipmap-hdpi
,如下:
再將新 icons 放進去,這樣 debug build 便會有自己的 icons!
也可放新的 strings.xml
在 app/src/debug/res/values/
下,加進新的 app_name
,這樣便會有不同的程式名稱。
後話
若你的程式有分 demo 和 professional 版本,也可透過以上方法,簡單生成兩個不同的 apk。Gradle 還有其他強大的功能,大家可以研究一下再分享。