How to sign release apk with gradle

Whenever I prepare my APK for the release - I used to do the following steps:

  1. Copy keystore to the project directory
  2. Copy into the project directory with keystore name, alias and password defined
  3. Copy some code from some blog that would load the properties and set up the signing
  4. Finally, do gradle build

But why not to move the boilderplate out of build.gradle?

Helper signing script

I wrote a tiny helper script that can be included into the projects:

def signingProperties = ""
def signingKeys = [
	storeFile     : {x -> file(x)},
	storePassword : {x -> x},
	keyAlias      : {x -> x},
	keyPassword   : {x -> x},

// Find in the project root, or in $HOME/.gradle
def f = ["${rootDir}/${signingProperties}", "${gradle.gradleUserHomeDir}/${signingProperties}"].find { file(it).exists() }

if (f) { "Loading signing properties from ${f}"
	def props = new Properties()
	props.load(new FileInputStream(f))

	// For each property apply it to the release signing config
	signingKeys.any { k, fn ->
		if (!props.containsKey(k)) {
			logger.error "Missing property ${k}"
			android.buildTypes.release.signingConfig = null
			return true
		android.signingConfigs.release[k] = fn(props[k]);
} else { "Missing ${signingProperties} file"
	android.buildTypes.release.signingConfig = null

It scans for the signing.propeties file in the project directory and in the gradle home directory, then it parses the properties and configures the build.


Now I do the following.

Create in my $HOME/.gradle (once for all projects):


Put my.keystore into $HOME/.gradle (once for all projects).

Edit build.gradle to use release singing config (most likely it is already done did it the app has been signed in the past):

android {
	signingConfigs {
	buildTypes {
		release {
			signingConfig signingConfigs.release

Finally, I include the script above. I simply use the github link:

	apply from: ''

Done. One ‘apply from’ line and signing just works.

You may try it, too. Sources are available on github, so please file an issue or a pull request if something it not working.

Posted on 2015-08-09

like   tweet   +1
rss   @me   +me   </>me