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

问题描述

我期待每一个建立与日期/时间(YYMMDDHHMM)来动态命名APK。我已经完成了这一点,并建立的gradle和名称的APK正确,但Android的工作室不会皮卡正确的名称,试图推到设备。

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工作室上传无菌APK到设备时,摇篮定制逻辑改变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{...}
  }

像上面会产生输出:

The above will generate output like:

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

而Android工作室将始终尝试第1版加载到手机,因为它是不同步的,并没有意识到每个生成后更名。

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的工作室运行同步什么建议吗?人工手动操作的同步前的版本是一个显示塞,并要求我只是回到默认的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_BUILD CI服务器上,这将触发APK重命名,但Android的工作室将使用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干净组装-PCI_BUILD =真

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

10-23 16:05