Android Studio / gradle dynamic apk name out of sync

I am looking dynamically for the APK name for each assembly with a date / time (yyMMddHHmm). I completed this and gradle builds and correctly calls the APK, however Android Studio will not record the correct name to try to click on the device.

There is a question on this topic with good information confirming the problem and that manual synchronization is required before each build. Android Studio downloads a sterile APK to the device if gradle has custom logic changing the name of the APK

For a complete 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, for example:

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

but Android Studio will always try to download the first version to the phone, because it is not synchronized and does not know the name change after each assembly.

Is there any suggestion on how to force synchronize every build / launch of Android studio? Manually synchronizing before building is a show stopper, and I only need to go back to the default APK naming scheme.

Usage: AS 1.2.1.1 and 'com.android.tools.build: gradle: 1.2.3'

+7
android android-studio gradle
source share
1 answer

Another option would be to ensure that your external builds are provided by the CI server (as 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") } } } ... } 

Then you can set the global environment variable CI_BUILD on the CI server, which will cause the renaming of the APK, but Android Studio will use the APK, as usual.

If you want to run the local assembly and rename the APK, then run it through the command line and add -PCI_BUILD=true

./gradlew clean assemble -PCI_BUILD=true

+3
source share

All Articles