zoukankan      html  css  js  c++  java
  • Android: android studio配置生成自定义apk名称

    1.Android Studio 3.0之前:

     在build.gradled 的 android {} 内添加如下代码:

    android.applicationVariants.all { variant ->  //如果是Library,例如aar包则使用 android.libraryVariants.all 且下面后缀判断修改为 .arr
            variant.outputs.each { output ->
     def fileName = "demo_${defaultConfig.versionCode}-${defaultConfig.versionName}-${releaseTime()}.apk" def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk')) { //这里修改apk文件名 output.outputFile = new File(outputFile.parent, fileName)  
           
    }
        }
    }

     在 android {} 之外声明 releaseTime() 方法:

    //获取编译时间
    def releaseTime() {
        return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
    }

    2.Android Studio 3.0之后:

     outputFile变为只读,需把each修改为all,然后通过outputFileName修改生成apk的名称,此外AS 3.0后打包完,除了apk包文件,还会多一个 output.json 参数文件。

        新版修改如下:

        //自定义apk名称含版本号信息
        android.applicationVariants.all { variant ->
            variant.outputs.all { output ->  //旧版为:each
                def fileName = "demo_${defaultConfig.versionCode}-${defaultConfig.versionName}-${releaseTime()}.apk"
                def outputFile = output.outputFile
                if (outputFile != null && outputFile.name.endsWith('.apk')) {
                    //这里修改apk文件名
                    outputFileName = fileName  //旧版为:output.outputFile = new File(outputFile.parent, fileName)
                }
            }
        }

     同样在 android {} 之外声明 releaseTime() 方法:

    //获取编译时间
    def releaseTime() {
        return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
    }

    -end-

  • 相关阅读:
    如何将自己的镜像上传到私库
    基于spring-cloud的微服务(1) 服务注册中心eureka
    关于对象池技术的一些记录
    为Docker容器中运行的gitlab添加ssh的一些问题记录
    使用java实现的socket代理(支持socket4和socket5)
    ConfluenceRemoteUserAuth
    JiraRemoteUserAuth
    Apache 的mod_auth_cas模块的介绍和使用
    基于乌班图的标准镜像添加中文支持
    apache反向代解决绝对路径可能出现的问题
  • 原文地址:https://www.cnblogs.com/blogs-of-lxl/p/9366532.html
Copyright © 2011-2022 走看看