我已经完成了根据我的应用程序中的buildType自动生成的内部版本号和文件名的生成。如果buildType为“release”,则appname将类似于“app-v.1.0.0.apk”;否则,如果buildType为“debug”,则appname将类似于“app-v.1.0.0-debug + build.179.apk” ”。它运行良好,但是不依赖于version.properties中的buildType的值始终像“1.0.0-debug + build.179”。我该如何解决?我认为我的问题出在范围内,我无法覆盖variant中的变量,但是我不知道该如何解决。我已经尝试解决我的问题很长时间了。

version.properties:

VERSION_NUMBER=1.0.0-debug+build.179
VERSION_MINOR=0
VERSION_BUILD=179
VERSION_PATCH=0
VERSION_MAJOR=1

build.gradle:
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"

    def versionPropsFile = file('version.properties')

    if (versionPropsFile.canRead()) {
        Properties versionProps = new Properties()
        versionProps.load(new FileInputStream(versionPropsFile))

        def versionMajor = 1
        def versionMinor = 0
        def versionPatch = 0
        def versionBuild = versionProps['VERSION_BUILD'].toInteger() + 1
        def versionNumber = "${versionMajor}.${versionMinor}.${versionPatch}-debug+build.${versionBuild}"

        android.applicationVariants.all { variant ->
            variant.outputs.all {
                if (buildType.name == "release") {
                    versionNumber = versionNumber.split("-", 0)[0]
                }
                outputFileName = "app"
                outputFileName += "-v" + versionNumber + ".apk"
            }
        }

        versionProps['VERSION_MAJOR'] = versionMajor.toString()
        versionProps['VERSION_MINOR'] = versionMinor.toString()
        versionProps['VERSION_PATCH'] = versionPatch.toString()
        versionProps['VERSION_BUILD'] = versionBuild.toString()
        versionProps['VERSION_NUMBER'] = versionNumber.toString()

        versionProps.store(versionPropsFile.newWriter(), null)

        defaultConfig {
            applicationId "com.my.app"
            minSdkVersion 28
            targetSdkVersion 29
            versionCode versionBuild
            versionName "${versionNumber}"
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
    } else {
        throw new FileNotFoundException("Could not read version.properties!")
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.github.kenglxn.QRGen:android:2.3.0'
    implementation 'androidx.preference:preference:1.1.0'
    implementation 'androidx.biometric:biometric:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

最佳答案

我解决了我的问题。我找到了解决方案here。 BuildType可以定义为

def buildType = gradle.startParameter.taskNames.any{it.toLowerCase().contains("debug")} ?
                        "debug" : "release"```

build.gradle:
 apply plugin: 'com.android.application'

    apply plugin: 'kotlin-android'

    apply plugin: 'kotlin-android-extensions'

    android {
        compileSdkVersion 29
        buildToolsVersion "29.0.2"

        def versionPropsFile = file('version.properties')

        if (versionPropsFile.canRead()) {
            Properties versionProps = new Properties()
            versionProps.load(new FileInputStream(versionPropsFile))

            def versionMajor = 1
            def versionMinor = 0
            def versionPatch = 0
            def versionBuild = versionProps['VERSION_BUILD'].toInteger() + 1
            def versionNumber = "${versionMajor}.${versionMinor}.${versionPatch}-" +
                                "debug+build.${versionBuild}"

            android.applicationVariants.all { variant ->
                variant.outputs.all {
                    if (buildType.name == "release") {
                        versionNumber = versionNumber.split("-", 0)[0]
                    }
                    outputFileName = "app"
                    outputFileName += "-v" + versionNumber + ".apk"
                }
            }

            def buildType = gradle.startParameter.taskNames.any{it.toLowerCase().contains("debug")} ?
                            "debug" : "release"

            versionProps['VERSION_MAJOR'] = versionMajor.toString()
            versionProps['VERSION_MINOR'] = versionMinor.toString()
            versionProps['VERSION_PATCH'] = versionPatch.toString()
            versionProps['VERSION_BUILD'] = versionBuild.toString()

            if (buildType == "release"){
                versionProps['VERSION_NUMBER'] = versionNumber.split("-", 0)[0]
            } else if (buildType == "debug"){
                versionProps['VERSION_NUMBER'] = versionNumber.toString()
            }

            versionProps.store(versionPropsFile.newWriter(), null)

            defaultConfig {
                applicationId "com.my.app"
                minSdkVersion 28
                targetSdkVersion 29
                versionCode versionBuild
                versionName versionProps['VERSION_NUMBER'].toString()
                testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            }
        } else {
            throw new FileNotFoundException("Could not read version.properties!")
        }

        buildTypes {
            release {
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
            debug {
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        implementation 'androidx.appcompat:appcompat:1.1.0'
        implementation 'androidx.core:core-ktx:1.1.0'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        implementation 'com.github.kenglxn.QRGen:android:2.3.0'
        implementation 'androidx.preference:preference:1.1.0'
        implementation 'androidx.biometric:biometric:1.0.0'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test.ext:junit:1.1.1'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    }

关于android - 应用版本取决于buildType-如何?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59109413/

10-11 08:11