【问题标题】:Renamed APK with the time of building does not run使用构建时间重命名的 APK 不运​​行
【发布时间】:2016-04-15 19:22:38
【问题描述】:

我在模块的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 使用了应用名称的旧值。

我使用这个运行配置:

随时提出任何建议。

【问题讨论】:

  • "请随时提出任何建议。" ——这真的会让你对各种笑话敞开心扉。喜欢这个 ;)
  • 我对这种异步问题感到很困惑。我真的愿意寻求任何帮助。请用你的笑话来回答:P
  • 面临同样的问题 :(

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


【解决方案1】:

看起来在 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 名称是否有困难并不重要

【讨论】:

    猜你喜欢
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 2016-06-04
    • 1970-01-01
    • 2011-12-24
    • 2015-01-06
    相关资源
    最近更新 更多