zoukankan      html  css  js  c++  java
  • gradle执行打包并导出Apk到指定文件夹

    例:build.gradle(:app)配置如下

    ...
    
     buildTypes {
            release {
             ...
            }
          
            debug {
              ...
            }
        }
    
    
        productFlavors {
            product {
                ...
            }
            
            develop {
                ...
            }
        }
        
        
    

    通过自定义复制Task实现打包并导出到指定文件夹实现如下:

    def releaseTime = new Date().format("yyyy-MM-dd_HH-mm", TimeZone.getTimeZone("GMT+8"))
    def versionName = android.defaultConfig.versionName
    
    def defaultDestinationPath = rootDir.getAbsolutePath() +
            "${File.separator}package-output${File.separator}"
    def defaultNewFileName = "App_${versionName}_${releaseTime}_release.apk"
    
    android.applicationVariants.all { variant ->
         //取正式发布环境
        if ("product" == variant.productFlavors[0].name && "release" == variant.buildType.name) {
            def releaseCollect = project.task('CollectApk', type: Copy)
            String outputApkPath =
                    variant.getPackageApplicationProvider().get().outputDirectory.getAsFile().get().getPath() +
                    File.separator +
                    variant.getPackageApplicationProvider().get().outputScope.getMainSplit().outputFileName
                        
            releaseCollect.doFirst {
                if (!file(defaultDestinationPath).exists()) {
                    file(defaultDestinationPath).mkdirs()
                }
            }
            releaseCollect.from(outputApkPath)
            releaseCollect.into(defaultDestinationPath)
            releaseCollect.rename { defaultNewFileName }
            releaseCollect.outputs.file(defaultNewFileName)
            releaseCollect.dependsOn project.tasks.getByName(
                    "assemble"
                            + "${variant.productFlavors[0].name.capitalize()}"
                            + "${variant.buildType.name.capitalize()}")
    
            if (variant.buildType.minifyEnabled) {
                variant.getAssembleProvider().get().doLast {
                    copy {
                        from variant.mappingFile
                        into "${defaultDestinationPath}" +
                                "${File.separator}mappings" +
                                "-${android.defaultConfig.versionCode}" +
                                "-${android.defaultConfig.versionName}" +
                                "-$releaseTime"
                        rename { String fileName ->
                            "mapping-${flavorName}.txt"
                        }
                    }
                }
            }
        }
    
    }
    
    
  • 相关阅读:
    JavaScript判断移动端及pc端访问不同的网站
    详情点击文字展开,再点击隐藏
    让IE6/IE7/IE8浏览器支持CSS3属性
    随机输入两位数,并将其交换位置输出
    100-999的水仙花数
    C++读取文件
    求n项阶乘之和并求出和的后六位
    n的阶乘
    3*n+1问题
    完全平方数的判断
  • 原文地址:https://www.cnblogs.com/MillerKevin/p/12567609.html
Copyright © 2011-2022 走看看