如何使用Gradle脚本在Android Studio中自动增加和释放已签名的apk

Ale*_*vis 8 android gradle apk android-studio

我正在尝试自动更新Android Manifest中的versionName和VersionCode参数,并在输出文件名中使用它们而不是"app-release.apk".

这个站点我在build.gradle文件中添加了这段代码:

import java.util.regex.Pattern
import com.android.builder.core.DefaultManifestParser

def mVersionCode
def mNextVersionName
def newName

task ('increaseVersionCode') << {
    def manifestFile = file("src/main/AndroidManifest.xml")
    def pattern = Pattern.compile("versionCode=\"(\\d+)\"")
    def manifestText = manifestFile.getText()
    def matcher = pattern.matcher(manifestText)
    matcher.find()
    mVersionCode = Integer.parseInt(matcher.group(1))
    def manifestContent = matcher.replaceAll("versionCode=\"" + ++mVersionCode + "\"")
    manifestFile.write(manifestContent)

}

task ('incrementVersionName') << {
    def manifestFile = file("src/main/AndroidManifest.xml")
    def patternVersionNumber = Pattern.compile("versionName=\"(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)\"")
    def manifestText = manifestFile.getText()
    def matcherVersionNumber = patternVersionNumber.matcher(manifestText)
    matcherVersionNumber.find()
    def majorVersion = Integer.parseInt(matcherVersionNumber.group(1))
    def minorVersion = Integer.parseInt(matcherVersionNumber.group(2))
    def pointVersion = Integer.parseInt(matcherVersionNumber.group(3))
    def buildVersion = Integer.parseInt(matcherVersionNumber.group(4))
    mNextVersionName = majorVersion + "." + minorVersion + "." + pointVersion + "." + (buildVersion + 1)
    def manifestContent = matcherVersionNumber.replaceAll("versionName=\"" + mNextVersionName + "\"")
    manifestFile.write(manifestContent)

}

tasks.whenTaskAdded { task ->
    if (task.name == 'generateReleaseBuildConfig') {
        task.dependsOn 'increaseVersionCode'
        task.dependsOn 'incrementVersionName'
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码完美运行,2个任务运行并正确更新清单文件.现在我想使用2个变量mVersionCodemNextVersionNamerelease块内部buildTypes像这样:

newName = defaultConfig.applicationId + "-" + mNextVersionName + " (" + mVersionCode + ").apk"
applicationVariants.all { variant ->
                variant.outputs.each {output ->
                    def file = output.outputFile
                    output.outputFile = new File(file.parent, file.name.replace("app-release.apk", newName))
                }
            }
Run Code Online (Sandbox Code Playgroud)

但是2的返回值为null.

我还尝试设置属性和额外属性:

task.setProperty("vName", mNextVersionName) 
ext.vName = mNextVersionName 
extensions.extraProperties.set("vName", mNextVersionName)
Run Code Online (Sandbox Code Playgroud)

在2个任务中,让他们在release没有运气的情况下.

有人有关于如何实现这一目标的想法吗?

Vin*_*ing 1

好吧,这是我的build.gradle应用程序模块的代码:

apply plugin: 'com.android.application'

apply from: 'versionalization.gradle'

def genVersionName = VersionInfo.versionName
def genVersionCode = VersionInfo.versionCode

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.vincestyling.exerciseapk"
        minSdkVersion 10
        targetSdkVersion 22
        versionName genVersionName
        versionCode genVersionCode
    }
}

android.applicationVariants.all { variant ->
    def taskSuffix = variant.name.capitalize()
    def assembleTaskName = "assemble${taskSuffix}"

    if (tasks.findByName(assembleTaskName)) {
        def processAPKTask = tasks.create(name: "process${taskSuffix}Apk", type: Copy) {
            variant.outputs.each { output ->
                from output.outputFile
                into output.outputFile.parent

                def newApkName = android.defaultConfig.applicationId + "-" + variant.buildType.name + "-" + genVersionName + " (" + genVersionCode + ").apk"
                rename ~/(.+)/, newApkName
            }
        }
        tasks[assembleTaskName].finalizedBy processAPKTask
    }
}
Run Code Online (Sandbox Code Playgroud)

应用于 head 的内容versionalization.gradle是我用来增加VersionInfo 的内容,然后返回两个要使用的值。

task VersionInfo {
    String FACTOR_KEY = "BUILD_NUMBER_FACTOR"

    File buildDir = file("build")
    buildDir.mkdir()

    File factorPropFile = new File(buildDir, "kbuildfactor.prop")

    Properties props = new Properties()
    if (factorPropFile.exists()) {
        props.load(new FileInputStream(factorPropFile))
    }

    int buildNumberFactor = props.get(FACTOR_KEY) as Integer ?: 0
    buildNumberFactor += 1

    props.put(FACTOR_KEY, buildNumberFactor as String)
    props.store(new FileOutputStream(factorPropFile), null)



    String BASE_VERSION_NAME = "BASE_VERSION_NAME"
    String BASE_VERSION_CODE = "BASE_VERSION_CODE"


    File versionPropFile = file("versioning.properties")
    props.load(new FileInputStream(versionPropFile))


    String baseVersionName = props.get(BASE_VERSION_NAME) as String
    Integer baseVersionCode = props.get(BASE_VERSION_CODE) as Integer

    ext.versionName = baseVersionName + "." + buildNumberFactor
    ext.versionCode = baseVersionCode * 1000 + buildNumberFactor
}
Run Code Online (Sandbox Code Playgroud)

这非常简单,读取两个文件以获取构建版本信息所需的字段。

最后我们复制/重命名最终的APK。

此外,我首先实现了复制/重命名部分,如下所示,但是当您有任何产品口味时它不会工作,我粘贴在这里作为另一个选择。

android.applicationVariants.all { variant ->
    variant.outputs.each {output ->
        def newApkName = android.defaultConfig.applicationId + "-" + variant.buildType.name + "-" + genVersionName + " (" + genVersionCode + ").apk"

        def oldApkName = "app-${variant.buildType.name}.apk"

        def file = output.outputFile

        output.outputFile = new File(file.parent, file.name.replace(oldApkName, newApkName))
    }
}
Run Code Online (Sandbox Code Playgroud)