You*_*jae 14 android build.gradle android-gradle-plugin
我想应用不同的VersionCode来制作apk文件.对于调试,只修复它1,并释放defaultConfig中指定的任何数字.
下面的代码将mypackage-release-1.apk文件作为assembleRelease工件提供,这是不期望的.我期待mypackage-release-10111.apk着这一点.
为什么这行debug { defaultConfig.versionCode=1 }会影响assembleRelease工件?
defaultConfig {
versionCode 10111
versionName '2.5.4'
minSdkVersion 10
targetSdkVersion 21
}
signingConfigs {
debug {
project.ext.loadSign = false
defaultConfig.versionCode = 1 // Why this value applied to assembleRelease?
}
release {
project.ext.loadSign = true
applicationVariants.all { variant ->
variant.outputs.each { output ->
def file = output.outputFile
output.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionCode + ".apk"))
}
}
}
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
signingConfig signingConfigs.release
}
}
Run Code Online (Sandbox Code Playgroud)
小智 11
最简单的解决方案是将 versionCode 和 versionName 变量从 defaultConfig 分别移至调试和发布。
android {
...
defaultConfig {
// without versionCode and versionName
...
}
buildTypes {
debug {
defaultConfig.versionCode X
defaultConfig.versionName 'X.Y.Z'
}
release {
defaultConfig.versionCode A
defaultConfig.versionName 'A.B.C'
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
晚会......
在任何任务执行之前评估整个gradle文件,因此您versionCode在声明debug配置时基本上更改了默认值.有重置没有直接的方法versionCode从buildType,但对方的回答链接通过声明对构建变种任务做的伎俩.
android {
...
defaultConfig {
...
}
buildTypes {
...
}
applicationVariants.all { variant ->
def flavor = variant.mergedFlavor
def versionCode = flavor.versionCode
if (variant.buildType.isDebuggable()) {
versionCode += 1
}
flavor.versionCode = versionCode
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个更新的版本:
android {
defaultConfig { ... }
applicationVariants.all { variant ->
if (variant.name == 'debug') {
variant.outputs.each { output ->
output.versionCodeOverride = 1
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
与风味一起使用:
applicationVariants.all { variant ->
def flavor = variant.mergedFlavor
def name = flavor.getVersionName()
def code = flavor.getVersionCode()
if (variant.buildType.isDebuggable()) {
name += '-d'
code = 1
}
variant.outputs.each { output ->
output.versionNameOverride = name
output.versionCodeOverride = code
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5952 次 |
| 最近记录: |