• 利用gradle加入构建版本


    在java的程序中,貌似都没有这个构建版本的概念。用的诸如eclipse。 idea和android studio的IDE也没有直接提供构建版本的选项。只是我却想在android程序的版本其中加入一个构建版本,当然,这个仅仅是个人的蛋疼需求。尽管在stackoverflow上看到其它人也有这样的需求,但相信人不多。

    这里分享一下我的解决方法。

    首先在app的build.gradle中的android方法里面,把defaultConfig的代码替换成下面代码:

        def verName = "1.0.0"
        def suffix = "(RC)"
    
        defaultConfig {
            applicationId "cn.irains.access.v2"
            minSdkVersion 14
            targetSdkVersion 20
            versionCode 5
            versionName verName + suffix
        }
    
        def versionPropsFile = file('version.properties')
    
        if (versionPropsFile.canRead()) {
            def Properties versionProps = new Properties()
    
            versionProps.load(new FileInputStream(versionPropsFile))
    
            def name = versionProps['VERSION_NAME'].toInteger()
    
            def runTasks = gradle.startParameter.taskNames
            if ('b' in runTasks || 'build' in runTasks ) {
                name++
            }
    
            versionProps['VERSION_NAME']=name.toString()
            versionProps.store(versionPropsFile.newWriter(), null)
            defaultConfig {
                versionName verName + "." + name + suffix
            }
        }
    
        android.applicationVariants.all { variant ->
            def file = variant.outputFile
            variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
        }

    上面的android.applicationVariant.all就是指定了新的apk的文件名称。前面的一大堆代码都是构建版本的逻辑。

    由于是构建版本,所以先读取任务,仅在任务为b(build的缩写)或build的情况下,版本才加一。

    然后我们还须要在build.gradle的同级文件夹下建立一个叫version.properties的文件。用于保存构建版本。

    完毕之后,构建一下试试吧。


  • 相关阅读:
    统计数据持久化
    缓存层的实现
    C++语法疑点
    为什么需要定义虚的析构函数?
    C++ shared_ptr deleter的实现
    条件变量
    ubuntu  输入时弹出剪切板候选项
    leetcode Bitwise AND of Numbers Range
    C/C++ 字符串 null terminal
    C++ inline weak symbol and so on
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7039308.html
Copyright © 2020-2023  润新知