ProductFavorl 多變種多渠道打包
運行場景
同一款軟件,因爲定製和其他原因,需要打包多個版本,每個版本都有各自的特色和區別(整體顯示大致一樣),而且能同時安裝到一個手機(具有不同的包名);技術點不是很難,但是確實很實用,作者就遇到過這樣的需求,想當初eclipse開發的時候,定製了10多個版本,svn都亂套了,同事接手瞬間懵逼,一旦修改需求…..都是淚!
運行技術
AndroidStudio 打包中的productFlavors
使用方法
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.wzgiceman.productfavorl"
minSdkVersion 17
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
productFlavors {
fulls {
applicationId "com.wzgiceman.productfavorl.fulls"
versionName "1.0-fulls"
}
demo {
applicationId "com.wzgiceman.productfavorl.demo"
versionName "1.0-demo"
}
}
}
defaultConfig定義的是默認的屬性,productFlavors的是每一個單獨的屬性,productFlavors會覆蓋defaultConfig屬性,所以上面的寫法可以理解成:
productFlavors {
fulls {
applicationId "com.wzgiceman.productfavorl.fulls"
versionName "1.0-fulls"
minSdkVersion 17
targetSdkVersion 23
versionCode 1
}
demo {
applicationId "com.wzgiceman.productfavorl.demo"
versionName "1.0-demo"
minSdkVersion 17
targetSdkVersion 23
versionCode 1
}
}
這樣AS默認就會構建兩個項目,其中一個是fulls,一個是demo,他們公用一個項目main(com.wzgiceman.productfavorl);但是可惜的是AS並不會自動
生成這兩個的工程文件,需要手動創建如下:
效果
- demo渠道-包名:com.wzgiceman.productfavorl.demo
- full渠道-包名:com.wzgiceman.productfavorl.fulls
實現方法
我們分別在demo和fulls中創建一個SecendActivity
構建完成後的工程目錄
分別創建對應的xml-layout,區分不同:
demo-layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/activity_secend"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<TextView android:layout_width="wrap_content"
android:textColor="@color/colorAccent"
android:text="@string/text"
android:layout_height="wrap_content"/>
</RelativeLayout>
fulls-layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/activity_secend"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<TextView android:layout_width="wrap_content"
android:text="@string/text"
android:textColor="@color/colorPrimary"
android:textSize="33sp"
android:layout_height="wrap_content"/>
</RelativeLayout>
通過build variant選擇當前要構建的工程
可以明顯的看出demo是工程build模式,但是fulls沒有build;這個需要開發者自己去切換
注意: 當你修改了項目中的構建文件, Android Studio 需要一個項目同步來導入構建配置的改變。點擊出現在 Android Studio 黃色通知欄上的 Sync Now 按鈕來導入這些變化
配置 AndroidManifest
<activity android:name=".SecendActivity"> </activity>
運行
測試模式下需要選擇對應的debug模式運行
打包
注意:雖然只有 release 的構建類型出現在默認的 build.gradle 文件中,但每個構建都提供了 release 和 debug 的構建類型。
在這個實例中,產品口味和構建類型創建了下面的構建變種:
- demoDebug
- demoRelease
- fullsDebug
- fullsRelease
比較簡單大家自己打包的時候自己配置吧
打完收工!