Release signing in gradle.properties for Android

Gnathonic picture Gnathonic · Apr 1, 2013 · Viewed 21.2k times · Source

So I'm trying to convert all of my ant build scripts to gradle, and I've been able to find ample resources and documentation on all of it except how to configure signing in the gradle.properties file.

ant.properties does it like so:

key.alias=alias
key.store.password=password
key.store=keystore.file
key.alias.password=password

But how do I do the same in gradle?

Answer

Xavier Ducrohet picture Xavier Ducrohet · Apr 2, 2013

In your gradle.properties file store the same values as in the ant.properties file, I think you'll have to do simpler names, like keyAlias for instance. Just remove the dots to be sure.

then in your build.gradle file do something like this:

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

if (project.hasProperty('keyAlias')) {
    android.signingConfigs.release.keyAlias = keyAlias
}
// do the same for the three other properties
// ...

Doing it this way gives you flexibility to build on a computer that has the gradle.properties file or not. The "keyalias" property is only read if it exists so the code with not fail if it's not there.

If all the properties are there, signingConfigs.release will be fully configured and will be used to sign the apk during the build. If it's not there, the APK will be built but not signed.