Android Gradle 1.0计算版本代码在多味设置中
在Android Gradle 1.0系统中,用于计算不同产品风格的版本代码的代码不再有效。我成功地使用了下面的示例代码 productFlavors.get(0).versionCode现在计算为null 格雷德尔代码Android Gradle 1.0计算版本代码在多味设置中,android,android-gradle-plugin,Android,Android Gradle Plugin,在Android Gradle 1.0系统中,用于计算不同产品风格的版本代码的代码不再有效。我成功地使用了下面的示例代码 productFlavors.get(0).versionCode现在计算为null 格雷德尔代码 android { buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradl
android {
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
// This actual the app version code. Our given range is [0, 99999]
defaultConfig.versionCode = 123
// 2 dimensions of flavors. API is more important than ABI.
flavorGroups "api", "abi"
productFlavors {
gingerbread {
flavorGroup "api"
minSdkVersion 10
versionCode = 1
}
icecreamSandwich {
flavorGroup "api"
minSdkVersion 14
// this must be higher than the gingerbread version to ensure update of the
// app when the device gets a system update from GB to ICS
versionCode = 2
}
x86 {
flavorGroup "abi"
ndk.abiFilter "x86"
// this is the flavor part of the version code.
// It must be higher than the arm one for devices supporting
// both, as x86 is preferred.
versionCode = 3
}
arm {
flavorGroup "abi"
ndk.abiFilter "armeabi-v7a"
versionCode = 1
}
mips {
flavorGroup "abi"
// It must be higher than the arm one for devices supporting
// both, as mips is preferred.
ndk.abiFilter "mips"
versionCode = 2
}
fat {
flavorGroup "abi"
// fat binary, lowest version code to be
// the last option
versionCode = 0
}
}
// make per-variant version code
applicationVariants.all { variant ->
// get the version code of each flavor
def apiVersion = variant.productFlavors.get(0).versionCode
def abiVersion = variant.productFlavors.get(1).versionCode
// set the composite code
variant.mergedFlavor.versionCode = apiVersion * 1000000 + abiVersion * 100000 + defaultConfig.versionCode
}
}
从
多味变体
在某些情况下,可能需要创建同一应用程序的多个版本
基于不止一个标准。例如,多apk支持
Google Play支持4种不同的过滤器。创建不同的APK
在每个筛选器上拆分需要能够使用多个筛选器
产品风味的维度
考虑一个游戏的例子,它有一个演示版和一个付费版本
希望在多apk支持中使用ABI筛选器。有3个ABI和
两个版本的应用程序,需要生成6个APK(不是
计算不同构建类型引入的变体)。
但是,所有三个ABI的付费版本代码相同,
因此,仅仅创造6种口味是不可取的。相反,有
口味和变体的两个维度应自动构建所有
可能的组合
此功能是使用Flavor维度实现的。口味是
分配给特定维度{
flavorDimensions "abi", "version"
productFlavors {
freeapp {
flavorDimension "version"
...
}
x86 {
flavorDimension "abi"
...
}
} }
flavorGroups
已被flavorDimensions
替换,因此您需要在build.gradle
// 2 dimensions of flavors. API is more important than ABI.
flavorDimensions "api", "abi"
productFlavors {
gingerbread {
flavorDimension "api"
minSdkVersion 10
versionCode = 1
}
icecreamSandwich {
flavorDimension "api"
minSdkVersion 14
// this must be higher than the gingerbread version to ensure update of the
// app when the device gets a system update from GB to ICS
versionCode = 2
}
x86 {
flavorDimension "abi"
ndk.abiFilter "x86"
// this is the flavor part of the version code.
// It must be higher than the arm one for devices supporting
// both, as x86 is preferred.
versionCode = 3
}
arm {
flavorDimension "abi"
ndk.abiFilter "armeabi-v7a"
versionCode = 1
}
mips {
flavorDimension "abi"
// It must be higher than the arm one for devices supporting
// both, as mips is preferred.
ndk.abiFilter "mips"
versionCode = 2
}
fat {
flavorDimension "abi"
// fat binary, lowest version code to be
// the last option
versionCode = 0
}
}
// make per-variant version code
applicationVariants.all { variant ->
// get the version code of each flavor
def apiVersion = variant.productFlavors.get(0).versionCode
def abiVersion = variant.productFlavors.get(1).versionCode
// set the composite code
variant.mergedFlavor.versionCode = apiVersion * 1000000 + abiVersion * 100000 + defaultConfig.versionCode
}
更新:
添加这些行,以便能够在生成的apk名称处查看versionCode
applicationVariants.all { variant ->
variant.outputs.each { output ->
def apk = output.outputFile;
def newName = "${output.name}-${variant.mergedFlavor.versionCode}"
if (variant.buildType.versionNameSuffix) {
newName += "-${variant.buildType.versionNameSuffix}"
}
if (output.zipAlign) {
output.zipAlign.outputFile = new File((File) apk.parentFile, newName + '-aligned.apk');
}
output.packageApplication.outputFile = new File((File) apk.parentFile, newName + ".apk")
}
}
请参见下面的构建结果:
gingerbreadArmDebug-1100123.apk
gingerbreadArmDebug-1100123-aligned.apk
gingerbreadFatDebug-1000123.apk
gingerbreadFatDebug-1000123-aligned.apk
gingerbreadMipsDebug-1200123.apk
gingerbreadMipsDebug-1200123-aligned.apk
gingerbreadX86Debug-1300123.apk
gingerbreadX86Debug-1300123-aligned.apk
icecreamSandwichArmDebug-2100123.apk
icecreamSandwichArmDebug-2100123-aligned.apk
icecreamSandwichFatDebug-2000123.apk
icecreamSandwichFatDebug-2000123-aligned.apk
icecreamSandwichMipsDebug-2200123.apk
icecreamSandwichMipsDebug-2200123-aligned.apk
icecreamSandwichX86Debug-2300123.apk
icecreamSandwichX86Debug-2300123-aligned.apk
gingerbreadArmRelease-1100123.apk
gingerbreadFatRelease-1000123.apk
gingerbreadMipsRelease-1200123.apk
gingerbreadX86Release-1300123.apk
icecreamSandwichArmRelease-2100123.apk
icecreamSandwichFatRelease-2000123.apk
icecreamSandwichMipsRelease-2200123.apk
icecreamSandwichX86Release-2300123.apk
其中一个的信息,由以下人员提取:
更新2:
在上发布了我的测试项目,你如何知道你使用的是哪个变体?因为我想为每个变体计算不同的版本代码。你的代码会为所有变体计算相同的版本代码,对吗?@最后,它更新了你的代码,看起来它会为每个变体计算不同的
versionCode
g its implementation.def apiVersion=variant.productFlavors.get(0).versionCode仍以null计算,这是我的原始版本problem@TWilly如何检查null
?请参阅我的答案更新,我在这里添加了gradlew clean build
的结果。您在哪里设置版本代码?我只看到您设置了输出文件名
version: 2.0.0-RC3
apkFileName: gingerbreadArmDebug-1100123.apk
isFrameworkApk: false
usesFramework:
ids:
- 1
sdkInfo:
minSdkVersion: '10'
targetSdkVersion: '21'
packageInfo:
forced-package-id: '127'
versionInfo:
versionCode: '1100123'
versionName: '1.0'
compressionType: false