gradle 构建应用程序包时脚本不工作
gradle script doesn't work while app bundle building
我在 app-gradle 文件中有一些代码可以自动增加构建版本号。
当我构建单个 apk 文件时它工作正常。
但是当我开始构建 App-Bundle 时它并没有增加任何东西。
我有些误解问题出在哪里。
(在 'app\src' 文件夹中还有 'version.properties' 文件,其中只有一行 - VERSION_BUILD=13)。
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
def versionPropsFile = file('version.properties')
def versionBuild
/*Setting default value for versionBuild which is the last incremented value stored in the file */
if (versionPropsFile.canRead()) {
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
versionBuild = versionProps['VERSION_BUILD'].toInteger()
} else {
throw new FileNotFoundException("Could not read version.properties!")
}
/*Wrapping inside a method avoids auto incrementing on every gradle task run. Now it runs only when we build apk*/
ext.autoIncrementBuildNumber = {
if (versionPropsFile.canRead()) {
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
versionBuild = versionProps['VERSION_BUILD'].toInteger() + 1
versionProps['VERSION_BUILD'] = versionBuild.toString()
versionProps.store(versionPropsFile.newWriter(), null)
} else {
throw new FileNotFoundException("Could not read version.properties!")
}
}
// Hook to check if the release/debug task is among the tasks to be executed.
gradle.taskGraph.whenReady {taskGraph ->
if (taskGraph.hasTask(assembleDebug)) {
//autoIncrementBuildNumber()
} else if (taskGraph.hasTask(assembleRelease)) {
autoIncrementBuildNumber()
}
}
defaultConfig {
applicationId "com.example.one"
minSdkVersion 21
targetSdkVersion 28
versionCode versionBuild
versionName "1.0." + versionBuild
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
要使用 gradle 构建 the app bundle,您可以使用命令 bundle<Variant>
而不是 assemble<Variant>
。
这意味着在您的代码中您还必须检查(或添加)任务 bundleRelease
else if (taskGraph.hasTask(bundleRelease)) {
autoIncrementBuildNumber()
我在 app-gradle 文件中有一些代码可以自动增加构建版本号。
当我构建单个 apk 文件时它工作正常。
但是当我开始构建 App-Bundle 时它并没有增加任何东西。
我有些误解问题出在哪里。
(在 'app\src' 文件夹中还有 'version.properties' 文件,其中只有一行 - VERSION_BUILD=13)。
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
def versionPropsFile = file('version.properties')
def versionBuild
/*Setting default value for versionBuild which is the last incremented value stored in the file */
if (versionPropsFile.canRead()) {
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
versionBuild = versionProps['VERSION_BUILD'].toInteger()
} else {
throw new FileNotFoundException("Could not read version.properties!")
}
/*Wrapping inside a method avoids auto incrementing on every gradle task run. Now it runs only when we build apk*/
ext.autoIncrementBuildNumber = {
if (versionPropsFile.canRead()) {
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
versionBuild = versionProps['VERSION_BUILD'].toInteger() + 1
versionProps['VERSION_BUILD'] = versionBuild.toString()
versionProps.store(versionPropsFile.newWriter(), null)
} else {
throw new FileNotFoundException("Could not read version.properties!")
}
}
// Hook to check if the release/debug task is among the tasks to be executed.
gradle.taskGraph.whenReady {taskGraph ->
if (taskGraph.hasTask(assembleDebug)) {
//autoIncrementBuildNumber()
} else if (taskGraph.hasTask(assembleRelease)) {
autoIncrementBuildNumber()
}
}
defaultConfig {
applicationId "com.example.one"
minSdkVersion 21
targetSdkVersion 28
versionCode versionBuild
versionName "1.0." + versionBuild
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
要使用 gradle 构建 the app bundle,您可以使用命令 bundle<Variant>
而不是 assemble<Variant>
。
这意味着在您的代码中您还必须检查(或添加)任务 bundleRelease
else if (taskGraph.hasTask(bundleRelease)) {
autoIncrementBuildNumber()