I would like to have my Gradle build to create a release signed apk file using Gradle.
I'm not sure if the code is correct or if I'm missing a parameter when doing gradle build
?
This is some of the code in my gradle file:
android {
...
signingConfigs {
release {
storeFile file("release.keystore")
storePassword "******"
keyAlias "******"
keyPassword "******"
}
}
}
The gradle build finishes SUCCESSFUL, and in my build/apk
folder I only see the ...-release-unsigned.apk
and ...-debug-unaligned.apk
files.
Any suggestions on how to solve this?
You can request passwords from the command line:
The
if-then-else
block prevents requests for passwords when you're building a release. Although theelse
branch is unreachable, it tricks Gradle into creating aninstall...Release
task.Backstory. As noted by https://stackoverflow.com/a/19130098/3664487, "Gradle scripts can prompt for user input using the System.console().readLine method." Unfortunately, Gradle will always request a password, even when you're building a debug release (cf. How to create a release signed apk file using Gradle?). Fortunately, this can be overcome, as I have shown above.
I managed to solve it adding this code, and building with
gradle build
:This generates a signed release apk file.
An alternative is to define a task that runs only on release builds.
Note that @sdqali's script will (at least when using Gradle 1.6) ask for the password anytime you invoke any gradle task. Since you only need it when doing
gradle assembleRelease
(or similar), you could use the following trick:Note that I also had to add the following (under android) to make it work:
(In reply to user672009 above.)
An even easier solution, if you want to keep your passwords out of a git repository; yet, want to include your build.gradle in it, that even works great with product flavors, is to create a separate gradle file. Let's call it 'signing.gradle' (include it in your .gitignore). Just as if it were your build.gradle file minus everything not related to signing in it.
Then in your build.gradle file include this line right underneath "apply plugin: 'android'"
If you don't have or use multiple flavors, rename "flavor1" to "release" above, and you should be finished. If you are using flavors continue.
Finally link your flavors to its correct signingConfig in your build.gradle file and you should be finished.
If you want to avoid hardcoding your keystore & password in build.gradle, you can use a properties file as explained here: HANDLING SIGNING CONFIGS WITH GRADLE
Basically:
1) create a myproject.properties file at /home/[username]/.signing with such contents:
2) create a gradle.properties file (perhaps at the root of your project directory) with the contents:
3) refer to it in your build.gradle like this: