BuildTypes

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.

Build types 定義某些Gradle 使用的屬性當建構與包裝你的App,

For example, the debug build type enables debug options and signs the APK with the debug key, while the release build type may shrink, obfuscate, and sign your APK with a release key for distribution.

You must define at least one build type in order to build your app—Android Studio creates the debug and release build types by default.

To start customizing packaging settings for your app, learn how to Configure Build Types.

有點類似compiler option ,可以在build的時候決定版本,去決定一些變數的定義,用途在於譬如今天要臨時給客戶一個測試版本,由於測試時間的關係,這期間必須改成某個值,平常可能會定義一個變數

DebugHelper.isTimeExpired = true ;

但這樣的程式碼容易因為失誤而把他commit到version control 上,造成困擾,所以如果在這期間能夠定義一個Build code時候的行為版本,就可以讓release版本永遠是不會有被設定到錯誤的flag狀態,於是可以使用gradle.build 中的 buildTypes

Customizing Your Build With Gradle

http://toastdroid.com/2014/03/28/customizing-your-build-with-gradle/

BuildConfig

https://chaosleong.gitbooks.io/gradle-for-android/content/build\_variants/buildconfig.html

GRADLE自定义你的BUILDCONFIG

http://stormzhang.com/android/2015/01/25/gradle-build-field/

Pro Tip: Android BuildConfig

http://buccaneer.io/2015/10/22/pro-tip-android-buildconfig/

使用 Android Studio 中的 Build variants (productFlavors)

http://mf99coding.logdown.com/posts/308236-using-the-android-build-variants-in-the-studio-productflavors

可以透過變數定義減少輸入錯誤

buildTypes{
    def BOOLEAN="boolean"
    def TRUE="true"
    def FALSE="false"


    release{
        minifyEnabledtrue
        proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
        buildConfigField BOOLEAN,IS_TEST_DOWNLOAD,FALSE
        }
    }

注意

1.每一個變數的定義都必須在每個版本出現,否則在build其他版本的時候就可能因為gradle不會產生該變數而使得無法進行build apk

2.可以透過reflection 來判斷是否有該欄位

results matching ""

    No results matching ""