zoukankan      html  css  js  c++  java
  • Android Gradle实用技巧——APK文件名中加上SVN版本号,日期等

    有时候,我们会希望能把APK文件名上带上打包日期,打包时svn的版本号,应用版本号等。当然这些也可以手动添加,但是手动的话也未免太不优雅了,而且可能会出错。

    利用Gradle,我们可以让打包出来的apk自动的带上一些列信息。

    默认读者已经对gradle有一定的了解,有buildtypes,productFlavors的概念。不了解的可以看看上一篇或者去网上搜索来补充一下。

    Gradle是基于groovy的自动化构建工具,在build.gradle中我们可以用一些脚本,函数来控制编译的过程。本文所实现的功能,就是用gradle来控制编译完成后输出文件的文件名来实现的。

    首先来个简单的例子,文件名上加上日期。

    android {
    
        compileSdkVersion 22
        buildToolsVersion '23.0.1'
    
    
        defaultConfig {
    
            minSdkVersion 11
            targetSdkVersion 22
            versionCode 14
            versionName "1.7"
            // dex突破65535的限制
            multiDexEnabled true
            // 默认是umeng的渠道
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "test"]
        }
    buildTypes {
            release {
               
                minifyEnabled true
    
                zipAlignEnabled true
                // 移除无用的resource文件
                shrinkResources true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                applicationVariants.all { variant ->
                    variant.outputs.each { output ->
                        def outputFile = output.outputFile
                        def fileName = "myapp_v${defaultConfig.versionName}_${releaseTime()}.apk"
                        output.outputFile = new File(outputFile.parent, fileName)
                      
                    }
                }
            }
    }
    }
    def releaseTime() {
    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
    }


     

    重点在buildTypes->release->applicationVariants里面

    def fileName = "myapp_v${defaultConfig.versionName}_${releaseTime()}.apk"
    

     这句话,defaultConfig是在上面设置的一些参数,releaseTime()函数是在最下面定义的获取当前时间的函数。按照这个格式,输出的文件名应该是:myapp_v1.7_2015-22-22.apk

    写完这个后,执行:

    ./gradlew assemble_Release
    

     就可以输出指定文件名格式的APK了。

    通过以上步骤,我们可以体会到gradle的灵活性。

    下面就是这篇文章的重点了,在你的apk名字中加上svn版本号。这样做的好处的测试的时候可以更好的定位bug等,还算是蛮有用的。只是不知道为什么百度根本检索不到类似的文章,去google才找到一些资料。也不知道是因为国内的人不爱分享呢,还是百度太菜呢,哈哈。

    加SVN版本号和上面的加入时间原理基本相同,就是要引入一个第三方的库,这个库可以获取svn的信息。

    首先在projece 的build.gralde中的dependencies中添加svnkit这个依赖:

    dependencies {
            classpath 'com.android.tools.build:gradle:1.2.3'
            classpath group: 'org.tmatesoft.svnkit', name: 'svnkit', version: '1.8.11'
        }

    我们就是利用这个库来在编译的时候获取svn的信息的。

    然后在module的build.gradle最上方添加

    import org.tmatesoft.svn.core.wc.*

    这样就把svnkit这个库引入过来了。

    再添加一个获取svn版本号的方法,类似一获取时间的方法。

    def getSvnRevision() {
        ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
        SVNClientManager clientManager = SVNClientManager.newInstance(options);
        SVNStatusClient statusClient = clientManager.getStatusClient();
        SVNStatus status = statusClient.doStatus(projectDir, false);
        SVNRevision revision = status.getCommittedRevision();
        return revision.getNumber();
    }


    这里面用到的都是svnkit的一些方法了,有兴趣的可以自己多了解一下。

    整体build文件如下:

    // project build.gradle
    
    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:1.2.3'
            classpath group: 'org.tmatesoft.svnkit', name: 'svnkit', version: '1.8.11'
        }
    }
    
    allprojects {
        repositories {
            jcenter()
        }
    }
    //module build.gradle
    import org.tmatesoft.svn.core.wc.*
    
    apply plugin: 'com.android.application'
    
    def releaseTime() {
        return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
    }
    
    def getSvnRevision() {
        ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
        SVNClientManager clientManager = SVNClientManager.newInstance(options);
        SVNStatusClient statusClient = clientManager.getStatusClient();
        SVNStatus status = statusClient.doStatus(projectDir, false);
        SVNRevision revision = status.getCommittedRevision();
        return revision.getNumber();
    }
    
    android {
    
        compileSdkVersion 22
        buildToolsVersion '23.0.1'
    
    
        defaultConfig {
            minSdkVersion 11
            targetSdkVersion 22
            //登录注册评论点赞
            versionCode 14
            versionName "1.7"
            // dex突破65535的限制
            multiDexEnabled true
            // 默认是umeng的渠道
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "test"]
        }
    
    
    
        buildTypes {
            release {
    
                minifyEnabled true
    
                zipAlignEnabled true
                // 移除无用的resource文件
                shrinkResources true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    
    
                applicationVariants.all { variant ->
                    variant.outputs.each { output ->
                        def outputFile = output.outputFile
                       //这里修改文件名
                        def fileName = "myapp_v${defaultConfig.versionName}_${releaseTime()}_${getSvnRevision()}.apk"
                        output.outputFile = new File(outputFile.parent, fileName)
                       
                    }
                }
            }
           
    
        productFlavors {
            xiaomi {
                manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
            }
            yingyongbao {
                manifestPlaceholders = [UMENG_CHANNEL_VALUE: "yingyongbao"]
            }
           
        }
    
    }
    
    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        compile 'com.umeng.analytics:analytics:latest.integration'
        compile 'com.android.support:appcompat-v7:22.2.0'
    
    }

     最后执行:

    ./gradlew assembleRelease
    

     这样,就可以打包出名字格式为:myapp_v1.7_20xx-xx-xx_1234.apk的APK文件了

  • 相关阅读:
    hutool工具
    lombok
    混入
    postMan
    jsr303常用注解
    网页兼容性
    C/C++ 一点笔记(1)
    VS2010 灵活运用快捷操作功能(新手必看)
    HTML中meta作用
    C/C++ 一点笔记(2)
  • 原文地址:https://www.cnblogs.com/csonezp/p/4903434.html
Copyright © 2011-2022 走看看