配置构建变体

2020-09-11  本文已影响0人  资本家大恶人

配置构建类型

您可以在模块级 build.gradle 文件中的 android 代码块内创建和配置构建类型。当您创建新模块时,Android Studio 会自动为您创建“debug”构建类型和“release”构建类型。虽然“debug”构建类型没有出现在构建配置文件中,但 Android Studio 会使用 debuggable true 配置它。这样,您就可以在安全的 Android 设备上调试应用,并使用常规调试密钥库配置 APK 签名。

如果您要添加或更改某些设置,可以将“debug”构建类型添加到配置中。以下示例为“debug”构建类型指定了 applicationIdSuffix,并配置了一个使用“debug”构建类型的设置进行初始化的“staging”构建类型。

android {
    defaultConfig {
        manifestPlaceholders = [hostName:"www.example.com"]
        ...
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        debug {
            applicationIdSuffix ".debug"
            debuggable true
        }
staging {
            initWith debug
            manifestPlaceholders = [hostName:"internal.example.com"]
            applicationIdSuffix ".debugStaging"
        }
    }
}

配置产品变种

以下代码示例使用 flavorDimensions 属性创建“mode”变种维度和“api”变种维度,前者用于对“full”和“demo”产品变种进行分组,后者用于根据 API 级别对产品变种配置进行分组:

将多个产品变种与变种维度组合在一起

在某些情况下,您可能要将多个产品变种的配置组合在一起。例如,您可能要为基于 API 级别的“full”和“demo”产品变种创建不同的配置。为此,您可以使用 Android Plugin for Gradle 创建多组产品变种作为变种维度。在构建应用时,Gradle 会将您定义的每个变种维度中的产品变种配置以及构建类型配置组合在一起,以创建最终的构建变体。Gradle 不会将属于同一变种维度的产品变种组合在一起。

以下代码示例使用 flavorDimensions 属性创建“mode”变种维度和“api”变种维度,前者用于对“full”和“demo”产品变种进行分组,后者用于根据 API 级别对产品变种配置进行分组:

android {
  ...
  buildTypes {
    debug {...}
    release {...}
  }

  // Specifies the flavor dimensions you want to use. The order in which you
  // list each dimension determines its priority, from highest to lowest,
  // when Gradle merges variant sources and configurations. You must assign
  // each product flavor you configure to one of the flavor dimensions.
  flavorDimensions "api", "mode"

  productFlavors {
    demo {
      // Assigns this product flavor to the "mode" flavor dimension.
      dimension "mode"
      ...
    }

    full {
      dimension "mode"
      ...
    }

    // Configurations in the "api" product flavors override those in "mode"
    // flavors and the defaultConfig block. Gradle determines the priority
    // between flavor dimensions based on the order in which they appear next
    // to the flavorDimensions property above--the first dimension has a higher
    // priority than the second, and so on.
    minApi24 {
      dimension "api"
      minSdkVersion 24
      // To ensure the target device receives the version of the app with
      // the highest compatible API level, assign version codes in increasing
      // value with API level. To learn more about assigning version codes to
      // support app updates and uploading to Google Play, read
     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 创建的构建变体数量等于每个变种维度中的变种数量与您配置的构建类型数量的乘积。当 Gradle 为每个构建变体或对应的 APK 命名时,先显示属于较高优先级变种维度的产品变种,接着是较低优先级维度中的产品变种,再接着是构建类型。以上面的构建配置为例,Gradle 使用以下命名方案创建了总共 12 个构建变体:

构建变体:[minApi24, minApi23, minApi21][Demo, Full][Debug, Release]
对应的 APK:app-[minApi24, minApi23, minApi21]-[demo, full]-[debug, release].apk
例如,
构建变体:minApi24DemoDebug
对应的 APK:app-minApi24-demo-debug.apk

过滤变体

Gradle 会为您配置的产品变种和构建类型的每种可能组合创建一个构建变体。不过,某些构建变体可能并不是您需要的,或者对于您的项目来说没有意义。您可以在模块级 build.gradle 文件中创建变体过滤器,以移除某些构建变体配置。

以上一部分中的构建配置为例,假设您打算让“demo”版应用仅支持 API 级别 23 及更高级别。您可以使用 variantFilter 代码块过滤掉所有将“minApi21”和“demo”产品变种组合在一起的构建变体配置:

android {
  ...
  buildTypes {...}

  flavorDimensions "api", "mode"
  productFlavors {
    demo {...}
    full {...}
    minApi24 {...}
    minApi23 {...}
    minApi21 {...}
  }

  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)
      }
  }
}
...
上一篇下一篇

猜你喜欢

热点阅读