升级Gradle 3.0重命名Apk并输出指定目录

背景是工程之前用的2.2.2版本,gradle-wrapper是gradle-2.14.1-all,这个版本的重命名apk和输出指定目录如下代码:

android.applicationVariants.all { variant ->

        variant.assemble.doLast {
            def targetDir = "$rootDir/bin"
            def fileNamePrefix = "${project.name}-${variant.baseName}"
            def targetFileNamePrefix = "${project.name}-${variant.baseName}-${project.android.defaultConfig.versionName}"
            targetFileNamePrefix = "soho_${project.android.defaultConfig.versionName}.${project.android.defaultConfig.versionCode}_android_${variant.baseName}"

            // gradle 2.2.2 适用
            project.copy {
                from variant.outputs.outputFile
                into "${targetDir}"
                rename("${fileNamePrefix}.apk", "${targetFileNamePrefix}.apk")
                project.logger.error("Copy ${fileNamePrefix}.apk to ${targetDir}/${targetFileNamePrefix}.apk")
            }


            project.copy {
                // copy mapping.txt
                from "${project.buildDir}/outputs/mapping/${variant.dirName}/mapping.txt"
                into "${targetDir}"
                rename("mapping.txt", "${targetFileNamePrefix}-mapping.txt")

                project.logger.error("Copy mapping.txt to ${targetDir}/${targetFileNamePrefix}-mapping.txt")
            }
}

            project.copy {
                from "${project.buildDir}/intermediates/symbols/${variant.dirName}/R.txt"
                into "${targetDir}"
                rename("R.txt", "${targetFileNamePrefix}-R.txt")

                project.logger.error("Copy R.txt to ${targetDir}/${targetFileNamePrefix}-R.txt")
            }
        }*/
    }

不升级一切都安好,升级gradle3.0之后就报错了:

问题很明确,就是不兼容了呗。

纠结了一会,gradle版本肯定要升级的,就做下适配:

android.applicationVariants.all { variant ->

        variant.outputs.all { output ->
            def outputFile = output.outputFile

            def targetDir = "$rootDir/bin"

            def targetFileNamePrefix

            def date = new Date()

            def formattedData = date.format('yyyyMMddHHmm')

            def fileNamePrefix = "${project.name}-${variant.baseName}"
            project.logger.error("fileNamePrefix: ${fileNamePrefix}")


            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                project.logger.error("outputFile: ${outputFile}")

                if (variant.buildType.name.equals("release")) {
                    targetFileNamePrefix = "soho_${project.android.defaultConfig.versionName}.${project.android.defaultConfig.versionCode}_android_release_${formattedData}"
                } else if (variant.buildType.name.equals("debug")) {
                    targetFileNamePrefix = "soho_${project.android.defaultConfig.versionName}.${project.android.defaultConfig.versionCode}_android_debug_${formattedData}"
                }
                project.logger.error("outputFileName: ${targetFileNamePrefix}")

                project.copy {
                    from output.outputFile
                    into "${targetDir}"
                    rename("${fileNamePrefix}.apk", "${targetFileNamePrefix}.apk")
                    project.logger.error("Copy ${fileNamePrefix}.apk to ${targetDir}/${targetFileNamePrefix}.apk")
                }

                project.copy {
                    // copy mapping.txt
                    from "${project.buildDir}/outputs/mapping/${variant.dirName}/mapping.txt"
                    into "${targetDir}"
                    rename("mapping.txt", "${targetFileNamePrefix}-mapping.txt")

                    project.logger.error("Copy mapping.txt to ${targetDir}/${targetFileNamePrefix}-mapping.txt")
                }

                project.copy {
                    from "${project.buildDir}/intermediates/symbols/${variant.dirName}/R.txt"
                    into "${targetDir}"
                    rename("R.txt", "${targetFileNamePrefix}-R.txt")

                    project.logger.error("Copy R.txt to ${targetDir}/${targetFileNamePrefix}-R.txt")
                }
            }
        }
    }

ok,适配完毕,完美。

原文发布于微信公众号 - 巫山老妖(wwjblog)

原文发表时间:2019-02-12

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券