build automation – Release signing in gradle.properties for Android-ThrowExceptions

Exception or error:

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?

How to solve:

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.

###

I was able to do it with the following. I tried @Xav’s solution, but it would complain during the release validation step, if I didn’t have the properties set. I’m sure this is a recent change due to the framework changing a lot. I just wanted to help by pointing out that with the else at the very end, I was able to force the release signingConfig to null. Now both signed and unsigned releases happen depending on the presence of the gradle.properties.

signingConfigs {
    release {
        keyAlias = "blue_sleep"
    }
}

buildTypes {
    release {
        signingConfig signingConfigs.release
    }
}

if (project.hasProperty('storeFile') &&
        project.hasProperty('storePassword') &&
        project.hasProperty('keyPassword')) {
    android.signingConfigs.release.storeFile = file(storeFile)
    android.signingConfigs.release.storePassword = storePassword
    android.signingConfigs.release.keyPassword = keyPassword
} else {
    android.buildTypes.release.signingConfig = null
}

Some other useful notes, you can put the gradle.properties in ~/.gradle/ if you don’t want it sitting in the project folder. Also you can set the storeFile property with an absolute path like this: storePath=file:///Users/nick/Dropbox/mycompany.keystore

###

Inspired by Eugens solution i came up with a little shorter variance. The code has to be in the android {} task configuration.

File signFile = rootProject.file('sign.properties')
if (signFile.exists()) {
    Properties p = new Properties()
    p.load(new FileInputStream(signFile))
    signingConfigs {
        releaseConfig {
            storeFile file(p.storeFile)
            storePassword p.storePassword
            keyAlias p.keyAlias
            keyPassword p.keyPassword
        }
    }
    buildTypes.release.signingConfig signingConfigs.releaseConfig
}

###

Gradle 1.9 doesn’t allow you to define properties. You can add them only to ext now. Small addition to previous answers:

signingConfigs {
    debug {
        project.ext.loadSign = false
    }
    release {
        project.ext.loadSign = true
    }
}

buildTypes {
    debug {
        signingConfig signingConfigs.debug
    }
    release {
        signingConfig signingConfigs.release
    }
}

if ( project.ext.loadSign ) {
    Properties p = new Properties ()
    p.load ( new FileInputStream ( rootProject.file ( 'keys/sign.properties' ) ) )

    android.signingConfigs.release.storeFile file ( p.file )
    android.signingConfigs.release.storePassword p.password
    android.signingConfigs.release.keyAlias p.alias
    android.signingConfigs.release.keyPassword p.keyPassword
}

###

There’s a good guide on this – https://github.com/almalkawi/Android-Guide/wiki/Generating-signed-release-APK-using-Gradle

###

My requirements were that anyone without the keystore should be able to build properly. This is the cleanest way I could find:

android {
    signingConfigs {
        release    //Filled in readSigningConfigIfAvailable()
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-xyz.txt'
            readSigningConfigIfAvailable()
        }
    }
}

private void readSigningConfigIfAvailable() {
    if (hasAllSigningProperties()) {
        android.signingConfigs.release.storeFile = file(keystore_path)
        android.signingConfigs.release.storePassword = keystore_password
        android.signingConfigs.release.keyAlias = key_alias
        android.signingConfigs.release.keyPassword = key_password
        android.buildTypes.release.signingConfig = android.signingConfigs.release
    } else {
        android.buildTypes.release.signingConfig = null
    }
}

private boolean hasAllSigningProperties() {
    (hasProperty('keystore_path')
    && hasProperty('keystore_password')
    && hasProperty('key_alias')
    && hasProperty('key_password'))
}

Leave a Reply

Your email address will not be published. Required fields are marked *