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.
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
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
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.
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.
Using: AS 1.2.1.1 & 'com.android.tools.build:gradle:1.2.3'
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")
}
}
}
...
}
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With