我在模块的build.gradle文件中使用此代码来重命名输出APK

android.applicationVariants.all { variant ->
            variant.outputs.each { output ->
                def file = output.outputFile
                def formattedDate = new Date().format('yyyy_MM_dd_HH_mm')
                output.outputFile = new File(file.parent, file.name.replace(".apk",
                        "-" + formattedDate + ".apk"))
            }
        }


当我按“运行”时,我在build/outputs/apk/app-debug-2016_01_11_13_23.apk处收到APK,并在“运行”控制台中收到错误消息:

The APK file .../build/outputs/apk/app-debug-2016_01_11_13_21.apk does not exist on disk.


每次按“运行”时,我都会在/build/outputs/apk/文件夹中收到新的APK,但错误是相同的。看起来Android Studio使用的是应用名称的旧值。

我使用以下运行配置:
android - 重新命名为APK的版本无法运行-LMLPHP

随时提出任何建议。

最佳答案

看起来在当前版本的Android Studio 1.5.1中有一个bug
我和您有同样的问题,但我只想重命名发行版APK。

因此,我最终得到了一个快速的解决方法,仅当所选的signinConfig是发行版之一时才重命名apk:

android.applicationVariants.all { variant ->
    if (variant.buildType.signingConfig.getName() == android.signingConfigs.release.getName()) {
        variant.outputs.each { output ->
            def file = output.outputFile
            def formattedDate = new Date().format('yyyy_MM_dd_HH_mm')
            output.outputFile = new File(file.parent, file.name.replace(".apk",
                    "-" + formattedDate + ".apk"))
        }
    }
}


所有的调试版本都将使用相同的名称,因此,如果Android Studio在将apk名称上传到设备之前是否有刷新困难,这无关紧要

10-02 05:03
查看更多