简体   繁体   English

Android Gradle detect是发布版本还是不在运行时?

[英]Android Gradle detect is a release build or not in runtime?

I need to create auto-increment version mechanism via Gradle. 我需要通过Gradle创建自动增量版本机制。 For this I use this manual and create two files 为此,我使用手册并创建两个文件

version.properties 版本属性

#Fri May 20 11:04:03 EEST 2016
VERSION_BUILD=3

build.gradle at app level: 在应用程序级别的build.gradle:

buildTypes {

        debug{
            versionNameSuffix 'debug';
        }
        release {
            def versionPropsFile = file('version.properties')

            if (versionPropsFile.canRead()) {
                def Properties versionProps = new Properties()
                versionProps.load(new FileInputStream(versionPropsFile))
                def versionBuild = versionProps['VERSION_BUILD'].toInteger() + 1
                versionProps['VERSION_BUILD'] = versionBuild.toString()
                versionProps.store(versionPropsFile.newWriter(), null)

                versionNameSuffix = versionBuild;

            } else {
                throw new GradleException("Could not read version.properties!")
            }
            debuggable false
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
}

The my problem is : VERSION_BUILD increments each time when I build project. 我的问题是:每次构建项目时, VERSION_BUILD都会增加。 Not a Run , just a build. 不是Run ,而是构建。 And don't matter I Run project or create a signed APK. 而且不管我运行项目还是创建签名的APK。

The my goal - increment version only when I generate signed APK. 我的目标-仅在生成签名的APK时增加版本。 How I can do that? 我该怎么做?

Finally, I found solution. 最后,我找到了解决方案。

List<String> runTasks = gradle.startParameter.getTaskNames();

for (String item : runTasks) {
    if (item.contains("assemble") && item.contains("Release")) {
           // this is a release task
    }
}

From your gradle file,everytime you run a rel build you sign the app. 从gradle文件中,每次运行rel build时都要对应用进行签名。 So Why not use versionCode instead of VERSION_BUILD, that way it will only happen when you build. 因此,为什么不使用versionCode而不是VERSION_BUILD,那样它只会在构建时发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM