Android build system?編譯app資源和源碼文件并打包成apk。
Android Studio用Gradle?自動(dòng)化徘溢、定制化并管理android?app的build?process撩扒。通過配置管理實(shí)現(xiàn)app?resources?和 code的引用控制管理扬卷,做到代碼和資源的復(fù)用扩然。
Gradle及其Android plugin?獨(dú)立于Android Studio掸犬。你可以通過android?studio編譯打包apk珊楼,也可以通過command?line編譯打包apk乏奥。由于Gradle獨(dú)立于Android?studio,你需要單獨(dú)去更新此build?tools以匹配當(dāng)前的Android studio版本亥曹。具體版本對(duì)應(yīng)關(guān)系請(qǐng)參照:https://developer.android.google.cn/studio/releases/gradle-plugin
The build process
編譯過程通過一系列的工具和過程將你的project轉(zhuǎn)為apk邓了。
Custom build configurations
Build types
Build types define certain properties that Gradle uses when building and packaging your app, and are typically configured for different stages of your development lifecycle.?
在modules的build.gradle的android塊中創(chuàng)建和配置buileTypes.
android {
? ? defaultConfig {
? ? ? ? manifestPlaceholders = [hostName:"www.example.com"]
? ? ? ? ...
? ? }
? ? buildTypes {
? ? ? ? release {
? ? ? ? ? ? minifyEnabled true
? ? ? ? ? ? proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
? ? ? ? }
? ? ? ? debug {
? ? ? ? ? ? applicationIdSuffix ".debug"
? ? ? ? ? ? debuggable true
? ? ? ? }
? ? ? ? /**
? ? ? ? ?* The `initWith` property allows you to copy configurations from other build types,
? ? ? ? ?* then configure only the settings you want to change. This one copies the debug build
? ? ? ? ?* type, and then changes the manifest placeholder and application ID.
? ? ? ? ?*/
? ? ? ? staging {
? ? ? ? ? ? initWith debug
? ? ? ? ? ? manifestPlaceholders = [hostName:"internal.example.com"]
? ? ? ? ? ? applicationIdSuffix ".debugStaging"
? ? ? ? }
? ? }
}
Product flavors
Product flavors represent different versions of your app that you may release to users, such as free and paid versions of your app. You can customize product flavors to use different code and resources, while sharing and reusing the parts that are common to all versions of your app. Product flavors are optional and you must create them manually.
定義在productFlavors塊中,支持跟defaultConfig相同的屬性-this is because defaultConfig actually belongs to the ProductFlavor class媳瞪。因此你可以配置共有的屬性在defaultConfig ,?每一個(gè)flavor可以根據(jù)需要更改其中部分屬性(如applicationId等)骗炉。
所有的flavors必須隸屬于一個(gè)flavor dimention,?相當(dāng)于對(duì)flavors進(jìn)行分類。
Build variants的形成基于buildType + productFlavor.?命名形式為:<product-flavor><build-type>.?如果有不同的flavor?dimention, build variants?的命名形式規(guī)則為:?較高優(yōu)先級(jí)的flavor dimention的flavors +?低優(yōu)先級(jí)dimention的flavors + build type.
例如:
android{
????? buildType{
???????????? debug {...}
???????????? release {...}
??????? }
flavorDimensions "api", "mode"
productFlavors {
? ? demo {
? ? ? dimension "mode"
???? }
? ? full {
? ? ? dimension "mode"
? ? }
minApi24 {
? ? ? dimension "api"
? ? ? minSdkVersion 24
? ? ? versionCode 30000 + android.defaultConfig.versionCode
? ? ? versionNameSuffix "-minApi24"
? ? }
? ? minApi23 {
? ? ? dimension "api"
? ? ? minSdkVersion 23
? ? ? versionCode 20000 ?+ android.defaultConfig.versionCode
? ? ? versionNameSuffix "-minApi23"
? ? }
? ? minApi21 {
? ? ? dimension "api"
? ? ? minSdkVersion 21
? ? ? versionCode 10000 ?+ android.defaultConfig.versionCode
? ? ? versionNameSuffix "-minApi21"
? ? }
}
Gradle creates a total of 12 build variants with the following naming scheme:
Build variant: [minApi24, minApi23, minApi21][Demo, Full][Debug, Release]
Corresponding APK: app-[minApi24, minApi23, minApi21]-[demo, full]-[debug, release].apk
Filter variants
Build variants?由buildtype和productflavors?組合成多種蛇受。有時(shí)候有一些組合用不著或沒有意義句葵,那么就可以通過filter將其過濾掉。如上例中你不需要api23以下的demo版兢仰,那么可以添加filter如下:
variantFilter { variant ->
? ? ? def names = variant.flavors*.name
? ? ? // To check for a certain build type, use variant.buildType.name == "<buildType>"
? ? ? if (names.contains("minApi21") && names.contains("demo")) {
? ? ? ? ? // Gradle ignores any variants that satisfy the conditions above.
? ? ? ? ? setIgnore(true)
? ? ? }
? }
Manifest entries
可以在build?variant?configuration中通過設(shè)置manifest的一些屬性來配置不同的apk乍丈,這些屬性有:application name, minimum SDK version, or target SDK version状囱。manifest文件的合并順序?yàn)椋?/p>
Dependencies
利用 Android Studio 中的 Gradle 構(gòu)建系統(tǒng)叛氨,您可以輕松地將外部二進(jìn)制文件或其他庫(kù)模塊作為依賴項(xiàng)添加到您的構(gòu)建中辐赞。這些依賴項(xiàng)可位于您的計(jì)算機(jī)上或遠(yuǎn)程代碼庫(kù)中柬唯,并且它們聲明的所有傳遞依賴項(xiàng)也會(huì)自動(dòng)包含在內(nèi)。
三種類型依賴項(xiàng):
apply plugin: 'com.android.application'
? ? android { ... }
? ? dependencies {
? ? ? ? // Dependency on a local library module
? ? ? ? implementation project(":mylibrary")
? ? ? ? // Dependency on local binaries
? ? ? ? implementation fileTree(dir: 'libs', include: ['*.jar'])
? ? ? ? // Dependency on a remote binary
? ? ? ? implementation 'com.example.android:app-magic:12.3'
? ? }
參考:https://developer.android.google.cn/studio/build/index.html#settings-file
PS: minifyEnabled:true?運(yùn)行混淆文件酿雪;false?不運(yùn)行混淆文件暑椰。