Android Studio/gradle 动态 apk 名称不同步 [英] Android Studio/gradle dynamic apk name out of sync

查看:24
本文介绍了Android Studio/gradle 动态 apk 名称不同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用日期/时间 (yyMMddHHmm) 在每个构建中动态命名 APK.我已经完成了这个并且 gradle 构建并正确命名了 APK,但是 Android Studio 不会选择正确的名称来尝试推送到设备.

I'm looking to dynamically name the APK on every build with a date/time (yyMMddHHmm). I have completed this and gradle builds and names the APK correctly, however Android Studio will not pickup the right name to attempt to push to device.

有一个关于这个主题的问题,提供了一些很好的信息,确认了这个问题,并且在每次构建之前都需要手动同步.Android Studio 将无菌 APK 上传到Gradle 具有自定义逻辑更改 APK 名称时的设备

There is a question on this topic with some good info, confirming the issue and that a manual sync is required before each build. Android Studio uploads sterile APK to device when Gradle has custom logic changing APK names

这里的完整图片是我的 build.gradle

For the full picture here is my build.gradle

android {
   compileSdkVersion 22
   buildToolsVersion "22.0.1"

   // grab the date/time to use for versionCode and file naming
   def date = new Date();
   def clientBuildDate = date.format('yyMMddHHmm')

   // for all client files, set the APK name
   applicationVariants.all { variant ->
    variant.outputs.each { output ->
        output.outputFile = new File(output.outputFile.parent, "myapp-" + versionName.replace(".","_") + "-" + clientBuildDate + ".apk")
    }
   }
   ....
   defaultConfig{...}
   buildTypes{...}
   compileOptions{...}
   signingConfigs{...}
  }

以上将生成如下输出:

myapp-1_0_0-1507021343.apk
myapp-1_0_0-1507021501.apk
myapp-1_0_0-1507021722.apk

但 Android Studio 将始终尝试将第一个版本加载到手机上,因为它不同步并且不知道每次构建后的名称更改.

but Android studio will always try to load the 1st version to a phone because it is out of sync and not aware of the name change after each build.

关于如何在 Android Studio 的每次构建/运行上强制同步有什么建议吗?在构建之前手动进行同步是一个障碍,需要我回到默认的 APK 命名方案.

Are there any suggestion on how to force a sync on each build/run of Android studio? Manually doing a sync before a build is a show stopper, and would require me to just go back to the default APK naming scheme.

使用:AS 1.2.1.1 &'com.android.tools.build:gradle:1.2.3'

Using: AS 1.2.1.1 & 'com.android.tools.build:gradle:1.2.3'

推荐答案

另一种选择是让您的外部发布版本由 CI 服务器提供(无论如何它们都应该提供),然后将以下内容添加到您的 build.gradle

Another option would be to have your externally released builds be provided by a CI server (which they should be anyway) and then add the following to your build.gradle

apply plugin: 'com.android.application'
...
def isCIBuild() {
    return System.getenv('CI_BUILD') != null || hasProperty('CI_BUILD');
}
...
android {
...

    if(isCIBuild()){
        def clientBuildDate = new Date().format('yyMMddHHmm')
        applicationVariants.all { variant ->
            variant.outputs.each { output ->
                output.outputFile = new File(output.outputFile.parent, "myapp-" + versionName.replace(".","_") + "-" + clientBuildDate + ".apk")
            }
        }
    }
...
}

然后,您可以在 CI 服务器上设置全局环境变量 CI_BUILD,这将触发 APK 重命名,但 Android Studio 将使用正常命名的 APK.

You then can setup the global environment variable CI_BUILD on the CI Server which will trigger the APK rename, yet Android Studio would use an APK named as it normally would be.

如果您想运行本地构建并重命名 APK,则通过命令行构建并添加 -PCI_BUILD=true

If you want to run a local build and have the APK renamed then build via command line and add -PCI_BUILD=true

./gradlew clean assemble -PCI_BUILD=true

这篇关于Android Studio/gradle 动态 apk 名称不同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆