[英]Android: Auto Increment version code only on Release BuildType in Gradle
I have read this topic: Autoincrement VersionCode with gradle extra properties我已阅读此主题: Autoincrement VersionCode with gradle extra properties
But it seems to increase the versionCode
on every build (builds up a lot if you run debug version often).但它似乎增加了每次构建的versionCode
(如果经常运行调试版本,则会增加很多)。
Is there a way to tweak the answer so that VersionCode
only increases when I build a release signed version in Android Studio
?有没有办法调整答案,以便VersionCode
仅在我在Android Studio
构建发布签名版本时增加?
For this solution is necessary a file version.properties.对于此解决方案,需要一个文件 version.properties。
in build.gradle:在 build.gradle 中:
def getVersionCode() {
def propertiesFile = file('version.properties')
if (!propertiesFile.canRead()) {
throw new GradleException("Could not read " + propertiesFile.name)
}
Properties properties = new Properties()
properties.load(new FileInputStream(propertiesFile))
def code = properties['VERSION_CODE_PROD'].toInteger()
gradle.taskGraph.whenReady { tasksGraph ->
List<Task> tasks = tasksGraph.getAllTasks()
for (Task task : tasks) {
if (task.name.contains("Production")) {
code = code + 1
properties['VERSION_CODE_PROD'] = code.toString()
properties.store(propertiesFile.newWriter(), null)
break
}
}
}
return code
}
android {
def code = getVersionCode()
defaultConfig {
...
versionCode code
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.