每個版本的build variant代表了你可以建構的每一個版本。雖然你未直接配置build variants,你可以通過配置build type和product flavor。
比如,一個demo的product flavor可以聲明不同的特性和裝置需求,比如自定義源碼,資源和最小的API。但是debug的build type卻有不同建構和打包設定,比如調試選項和簽名檔案。
配置Build Types
你可以在子產品的build.gradle檔案裡的android{}區塊中來建立和配置建構類型。當你建立一個新的子產品的時候,Android Studio自動為你建立debug和release。雖然debug建構類型不出現在建構配置檔案中,Android Studio通過debuggable true來配置。這會允許你在一個安全的Android裝置中調試app,并且使用一個通用的debug的keystore對APK進行簽名。
如果你想添加或更高确定的設定,你可以添加debug建構類型到你的配置中。接下來的例子聲明為debug建構類型聲明了一個applicationIdSuffix。并且配置一個jnidebug建構類型,設定其根據debug建構類型進行初始化。
android {
...
defaultConfig {...}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
applicationIdSuffix ".debug"
}
/**
* The 'initWith' property allows you to copy configurations from other build types,
* so you don't have to configure one from the beginning. You can then configure
* just the settings you want to change. The following line initializes
* 'jnidebug' using the debug build type, and changes only the
* applicationIdSuffix and versionNameSuffix settings.
*/
jnidebug {
// This copies the debuggable attribute and debug signing configurations.
initWith debug
applicationIdSuffix ".jnidebug"
jniDebuggable true
}
}
}
配置Product Flavor
建立product flavor跟建立建構類型相同:将其添加到productFlavors{}區塊中,并且配置你需要的設定。Product flavors支援defaultConfit,這是因為defaultConfig屬于ProfuctFlavor類。這就意味着你可以為所有的Flavors在defaultConfig{}提供基本的配置。每個flavor可以複寫任何這些預設值。比如applicationId。
注意:你仍然需要在manifest檔案中的package屬性中來聲明包名。
以下是一個代碼示例,建立一個demo和full的product flavor,并設定了他們自己的applicationId和versionName。
android {
...
defaultConfig {...}
buildTypes {...}
productFlavors {
demo {
applicationId "com.example.myapp.demo"
versionName "1.0-demo"
}
full {
applicationId "com.example.myapp.full"
versionName "1.0-full"
}
}
}
在你建立和配置你的product flavor之後,點選消息提示條中出現的Sync Now按鈕。在同步完成後,Gradle根據你的建構類型和product flavor自動建立build variants,并且用<product-flavor>。比如,如果你建立demo和full兩個product flavor,并且保持debug和release為預設,Gradle則會建立以下build variants:
1、 demoDebug
2、 demoRelease
3、 fullDebug
4、 fullRelease
為Build Variants建立Source Sets
預設情況下,Android Studio建立main/ source set和目錄在所有的build variants中共享。然而,你可以建立新的source sets來控制Gradle編譯和特定的build types,product flavors和build variants。例如,你可以在main/ source set中定義基本的功能,使用product flavor source sets來改變你的app的東西,包括權限和日志等。
Gradle希望你通過确定的方式來組織source set檔案和目錄。
Android的Gradle插件提供了一個非常有用的Gradle 任務來顯示你如何為乜咯build type,product flavor和build variants來組織你的檔案。比如,以下的報告部分描述了Gradle希望找到debug 的build type的特定的檔案:
------------------------------------------------------------
Project :app
debug
Compile configuration: compile
build.gradle name: android.sourceSets.debug
Java sources: [app/src/debug/java]
Manifest file: app/src/debug/AndroidManifest.xml
Android resources: [app/src/debug/res]
Assets: [app/src/debug/assets]
AIDL sources: [app/src/debug/aidl]
RenderScript sources: [app/src/debug/rs]
JNI sources: [app/src/debug/jni]
JNI libraries: [app/src/debug/jniLibs]
Java-style resources: [app/src/debug/resources]
為了生成和檢視你的建構配置的報告,遵循如下:
1、 點選右面的IDE視窗
2、 找到MyApplication > Tasks > android,輕按兩下sourceSets
3、 檢視報告,點選IDE視窗底部的Gradle Console
當你建立一個新的build variant,Android Studio不會為你建立一個source set 目錄,但是會給你一些選項來幫助你。比如,為你的debug建構類型建立java/目錄:
1、 打開項目面闆,選擇Project視圖
2、 找到MyProject/app/src
3、 右鍵單擊src目錄,選擇New > Folder > Java Folder
4、 在下拉中找到Target Source Set,選擇debug
5、 點選Finish
Android Studio為你的debug建構類型建立一個source set目錄,然後建立一個java/目錄到裡面。你同樣可以讓Android Studio為特定的build variant建立的新的檔案來建立相應的目錄。比如,為你的debug建構類型建立一個values XML檔案:
1、 在Project面闆中,右鍵單擊src目錄,選擇New > XML > Values XML File
2、 輸入XML檔案名字,或者保持預設。
3、 在下拉中找到Target Source Set,選擇debug
4、 點選Finish