Gradle3.0 修改APK生成路径和文件名

时间:2024-04-13 22:04:03

Gradle3.0在未升级更新之前我们输出apk路径:

applicationVariants.all { variant ->
    variant.outputs.each { output ->
        def outputFile = output.outputFile
        if (outputFile != null && outputFile.name.endsWith('.apk')) {
            if(!outputFile.name.contains("debug")){
                def fileName = outputFile.name.replace(".apk", "-${defaultConfig.versionName}.apk")
                output.outputFile = new File("C:\\Users\\user\\Desktop\\apk\\${defaultConfig.versionName}", fileName)
            }
        }
    }
}

Gradle3.0升级之后我们需要更改:

Gradle3.0 修改APK生成路径和文件名

代码如下:

applicationVariants.all { variant ->
    variant.outputs.all { output ->
        outputFileName = outputFile.name.replace(".apk", "-${defaultConfig.versionName}.apk")
        if (outputFileName != null && outputFileName.endsWith('.apk')) {
            if (!outputFile.name.contains("debug")) {
                variant.getPackageApplication().outputDirectory = new File("C:\\Users\\user\\Desktop\\apk")
                variant.getPackageApplication().outputScope.apkDatas.forEach {
                    apkData -> apkData.outputFileName = outputFileName
                }
            }
        }
    }
}