I am having an issue with gradle and Android Studio, which only appears when building in Android Studio (BuildServer and Commandline work just fine)
applicationVariants.all { variant ->
def file = variant.outputFile
variant.outputFile = new File(file.parent, file.name.replace("app-", getDate() + "_myapp_" + getGitCommit() +"_"));
}
def getDate() {
def dateNow = new Date()
def formattedDate = dateNow.format('yyyy-MM-dd_HH-mm-ss')
return formattedDate
}
The build works, but when AS wants to copy the apk to the device it throws a Local path doesn't exist.
error.
The problem is that the files that are generated look like:
2014-03-17_16-17-41_myapp__debug-unaligned.apk
but AS looks for:
2014-03-17_16-17-18_myapp__debug-unaligned.apk
which is somehow generated by an AS specific build step resulting in a recalculation of the date. I tried to fix it with an external property for the build date which should remain the same throughout the build process, but probably due to my lack of gradle skills, this did not help.
Maybe someone has a workaround for me to make my build work in Android Studio.
This is normal, though unfortunate.
When Studio opens the project, it queries Gradle for the model of the project. This includes the list of modules and for each module, their source folders, their dependencies, and the output of their build. In this case, the APK.
So when Studio queries Gradle for the model, our plugin will build the model, which includes running your custom code that renames the APK filename. This is then sent to Studio which records it.
However whenever you build, Studio will tell Gradle to build but not pass it any other information. Gradle will actually re-create the model again, and run your code again.
This means that at every build the APK file name is different (Since your APK filename contains the date to the second), but none of them match the filename created during the project import. This makes the deployment fail.
There is no way right now to have Gradle send Studio the filename of the generated APK.
Edit: A better way to do this would be to keep the current output but copy it in a new file for safekeeping.
I would do something like this:
android.applicationVariants.all { variant ->
def file = variant.output
// create the new task
def copyTask = project.tasks.create("copy${variant.name}Apk", Copy)
copyTask.from = file
copyTask.into = file.parent
copyTask.rename("app-", getDate() + "_myapp_" + getGitCommit() +"_")
// set up task dependencies
// first make the assemble task depend on copyTask to make sure it gets called.
variant.assemble.dependsOn copyTask
// then make copyTask depend on the actual packaging task.
copyTask.dependsOn variant.packageApplication
}
Note that the copy task expect a folder as destination so we have to use a rename rule.
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