configure your build(android app)

Android build system?編譯app資源和源碼文件并打包成apk。

Android Studio用Gradle?自動(dòng)化、定制化并管理android?app的build?process。通過(guò)配置管理實(shí)現(xiàn)app?resources?和 code的引用控制管理,做到代碼和資源的復(fù)用。

Gradle及其Android plugin?獨(dú)立于Android Studio。你可以通過(guò)android?studio編譯打包apk,也可以通過(guò)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

編譯過(guò)程通過(guò)一系列的工具和過(guò)程將你的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í)候有一些組合用不著或沒(méi)有意義,那么就可以通過(guò)filter將其過(guò)濾掉。如上例中你不需要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中通過(guò)設(shè)置manifest的一些屬性來(lái)配置不同的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)行混淆文件。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容