android-gradle Define the signing configuration in an external file


Example

You can define the signing configuration in an external file as a signing.properties in the root directory of your project.

For example you can define these keys (you can use your favorite names):

STORE_FILE=myStoreFileLocation    
STORE_PASSWORD=myStorePassword
KEY_ALIAS=myKeyAlias
KEY_PASSWORD=mykeyPassword

Then in your build.gradle file:

android {

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

Then you can introduce some checks to avoid gradle issues in the build process.

//------------------------------------------------------------------------------
// Signing
//------------------------------------------------------------------------------
def Properties props = new Properties()
def propFile = file('../signing.properties')
if (propFile.canRead()) {

    if (props != null && props.containsKey('STORE_FILE') && props.containsKey('STORE_PASSWORD') &&
            props.containsKey('KEY_ALIAS') && props.containsKey('KEY_PASSWORD')) {

        android.signingConfigs.release.storeFile = file(props['STORE_FILE'])
        android.signingConfigs.release.storePassword = props['STORE_PASSWORD']
        android.signingConfigs.release.keyAlias = props['KEY_ALIAS']
        android.signingConfigs.release.keyPassword = props['KEY_PASSWORD']
    } else {
        android.buildTypes.release.signingConfig = null
    }
} else {
    android.buildTypes.release.signingConfig = null
}