Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Studio 配置 Build Variant:Build Types、Product Flavors 與 Source Sets

編輯:關於 Android 程式設計

每個 build variant 對應一個可以構建的版本。你通常不會直接配置 variant,而是透過配置 build type 與 product flavor 間接得到它。

舉例來說,一個 demo 的 product flavor 可以聲明不同的功能特性、設備需求、自定義源碼、資源與最低 API 級別;而 debug 這個 build type 則對應不同的構建與打包設置,比如調試選項與簽名檔。兩者交叉組合,就得到了全部 variant。

配置 Build Types

在模組的 build.gradle 的 android {} 區塊中建立與配置。新建模組時,Android Studio 會自動生成 debug 與 release 兩種類型。debug 不出現在配置檔裡,Studio 通過 debuggable true 隱式配置它,讓你能在裝置上調試,並用通用的 debug keystore 簽名。

如果需要覆寫或新增設定,可以顯式聲明 debug,並用 initWith 從既有類型繼承配置:

android {
    ...
    defaultConfig { ... }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        debug {
            applicationIdSuffix ".debug"
        }

        /**
         * initWith 允許從其他 build type 複製配置,
         * 不必從零開始寫,只需覆寫要改的項目。
         * 下面這行以 debug 為基礎初始化 jnidebug,
         * 只改 applicationIdSuffix 與開啟 jniDebuggable。
         */
        jnidebug {
            initWith debug
            applicationIdSuffix ".jnidebug"
            jniDebuggable true
        }
    }
}

配置 Product Flavors

做法與 build type 相同:加到 productFlavors {} 區塊裡,配置所需設定。defaultConfig 之所以能對所有 flavor 生效,是因為它本身屬於 ProductFlavor 類,所以你可以在 defaultConfig {} 中提供基礎配置,再由每個 flavor 各自覆寫(比如 applicationId)。

注意:manifest 檔中的 package 屬性仍需保留。

android {
    ...
    defaultConfig { ... }
    buildTypes { ... }

    productFlavors {
        demo {
            applicationId "com.example.myapp.demo"
            versionName "1.0-demo"
        }
        full {
            applicationId "com.example.myapp.full"
            versionName "1.0-full"
        }
    }
}

配置完成後點擊提示條上的 Sync Now。同步結束,Gradle 會依 build type 與 product flavor 自動生成 variant。以本例來說,保留 debug 與 release 為預設,就會得到四個 variant:

  1. demoDebug
  2. demoRelease
  3. fullDebug
  4. fullRelease

命名規則是「flavor 名 + build type 名」,記住這個規律就能推導任意組合。

為 Build Variants 建立 Source Sets

預設情況下,Android Studio 只建立 main/ 一組 source set 供所有 variant 共用。但你完全可以新增 source set,用來控制 Gradle 在編譯特定 build type、product flavor 或 build variant 時使用哪些代碼與資源。

典型玩法是:在 main/ 中定義基礎功能,再用各 flavor 的 source set 覆蓋部分內容,例如不同的圖標、不同的 API 位址、不同的日誌開關,甚至不同的權限宣告。

Gradle 對 source set 的檔案與目錄組織有明確約定,並提供了一個任務來查詢:

------------------------------------------------------------
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. 打開右側的 Gradle 面板。
  2. 找到 MyApplication > Tasks > android,雙擊 sourceSets。
  3. 在 IDE 底部的 Gradle Console 中查看輸出。

新建 build variant 時,Android Studio 不會自動幫你建 source set 目錄,但提供了幾個便捷入口。比如為 debug 建立 java/ 目錄:

  1. 在 Project 面板中切換到 Project 視圖。
  2. 找到 MyProject/app/src。
  3. 右鍵 src 目錄,選擇 New > Folder > Java Folder。
  4. 在 Target Source Set 下拉中選擇 debug。
  5. 點擊 Finish。

或者直接新建檔案,讓 Studio 順帶把目錄建好。比如為 debug 建立一個 values XML:

  1. 在 Project 面板中右鍵 src 目錄,選擇 New > XML > Values XML File。
  2. 輸入檔名(或保留預設)。
  3. 在 Target Source Set 下拉中選擇 debug。
  4. 點擊 Finish。

因為 debug 是針對特定 source set 的,Android Studio 會自動建立所需的目錄結構:

最後提醒一個實務上的注意點:source set 的合併有優先級,大致是 build variant > build type / flavor > main。同名資源會被高優先級的覆蓋,而清單檔則是逐層合併。這機制很方便,但也容易踩坑——當你發現某個字串或圖標「莫名其妙變了」,先想想是不是被某個 flavor 的 source set 覆蓋掉了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved