android - 构建时重命名的APK不运行

标签 android android-studio android-gradle-plugin apk build.gradle

我在模块的 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"))
            }
        }

当我按下“运行”时,我在此处收到 APK build/outputs/apk/app-debug-2016_01_11_13_23.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 似乎使用了应用名称的旧值。

我使用这个运行配置: enter image description here

请随时提出任何建议。

最佳答案

看起来有一个 bug在 android studio 的当前版本 1.5.1 中。 我遇到了和你一样的问题,但我只想重命名发布的 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 名称有困难并不重要

关于android - 构建时重命名的APK不运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34722304/

相关文章:

android - 为什么 preferences.getString ("key", "DEFAULT") 总是返回 "DEFAULT"?

android - 无法在 android studio 中加载自定义 Gradle 插件的类

android - APK LICENSE 中复制的重复文件(同一文件)

android -/tools/external/gradle/gradle-4.5-bin.zip(无此类文件或目录)

java - GridView 项目高度与其他项目高度不匹配

android - 如何对包裹在 `runOnUiThread` 中的代码进行单元测试?

java - 如何拉伸(stretch)ImageView中的图像?

java - 涉及标识符 R 的错误(将一个项目嵌入另一个项目时)

android - Android ADT 中的 Gradle 构建导出错误

android - 谷歌 openID 登录集成