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
To complement the other answers, you can also place your gradle.properties file in your own module folder, together with build.gradle, just in case your keystore is specific to one project.
android {
compileSdkVersion 17
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 9
targetSdkVersion 18
}
File signFile = rootProject.file('sign/keystore.properties')
if (signFile.exists()) {
Properties properties = new Properties()
properties.load(new FileInputStream(signFile))
signingConfigs {
release {
storeFile rootProject.file(properties['keystore'])
storePassword properties['storePassword']
keyAlias properties['keyAlias']
keyPassword properties['keyPassword']
}
}
}
buildTypes {
release {
runProguard true
zipAlign true
proguardFile rootProject.file('proguard-rules.cfg')
signingConfig signingConfigs.release
}
debug {
runProguard false
zipAlign true
}
}
}
Yet another approach to the same problem. As it is not recommended to store any kind of credential within the source code, we decided to set the passwords for the key store and key alias in a separate properties file as follows:
key.store.password=[STORE PASSWORD]
key.alias.password=[KEY PASSWORD]
If you use git, you can create a text file called, for example, secure.properties. You should make sure to exclude it from your repository (if using git, adding it to the .gitignore file). Then, you would need to create a signing configuration, like some of the other answers indicate. The only difference is in how you would load the credentials:
android {
...
signingConfigs {
...
release {
storeFile file('[PATH TO]/your_keystore_file.jks')
keyAlias "your_key_alias"
File propsFile = file("[PATH TO]/secure.properties");
if (propsFile.exists()) {
Properties props = new Properties();
props.load(new FileInputStream(propsFile))
storePassword props.getProperty('key.store.password')
keyPassword props.getProperty('key.alias.password')
}
}
...
}
buildTypes {
...
release {
signingConfig signingConfigs.release
runProguard true
proguardFile file('proguard-rules.txt')
}
...
}
}
Never forget to assign the signingConfig to the release build type manually (for some reason I sometimes assume it will be used automatically). Also, it is not mandatory to enable proguard, but it is recommendable.
We like this approach better than using environment variables or requesting user input because it can be done from the IDE, by switching to the realease build type and running the app, rather than having to use the command line.
Android Studio Go to File -> Project Structure or press Ctrl+Alt+Shift+S
See The Image
Click Ok
Then the signingConfigs will generate on your build.gradle file.
(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.
android {
signingConfigs {
flavor1 {
storeFile file("..")
storePassword ".."
keyAlias ".."
keyPassword ".."
}
flavor2 {
storeFile file("..")
storePassword ".."
keyAlias ".."
keyPassword ".."
}
}
}
Then in your build.gradle file include this line right underneath "apply plugin: 'android'"
apply from: 'signing.gradle'
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.
...
productFlavors {
flavor1 {
...
signingConfig signingConfigs.flavor1
}
flavor2 {
...
signingConfig signingConfigs.flavor2
}
}
...
Almost all platforms now offer some sort of keyring, so there is no reason to leave clear text passwords around.
I propose a simple solution that uses the Python Keyring module (mainly the companion console script keyring
) and a minimal wrapper around Groovy ['do', 'something'].execute()
feature:
def execOutput= { args ->
def proc = args.execute()
proc.waitFor()
def stdout = proc.in.text
return stdout.trim()
}
Using this function, the signingConfigs
section becomes:
signingConfigs {
release {
storeFile file("android.keystore")
storePassword execOutput(["keyring", "get", "google-play", storeFile.name])
keyAlias "com.example.app"
keyPassword execOutput(["keyring", "get", "google-play", keyAlias])
}
}
Before running gradle assembleRelease
you have to set the passwords in your keyring, only once:
$ keyring set google-play android.keystore # will be prompted for the passwords
$ keyring set google-play com.example.app
Happy releases!