Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Gradle - load signing config from external file

In Gradle for Android it seems to be commons practice to define your signing config for release build like this:

android {
    signingConfigs {
        debug {
            storeFile file("debug.keystore")
        }

        myConfig {
            storeFile file("other.keystore")
            storePassword "android"
            keyAlias "androiddebugkey"
            keyPassword "android"
        }
    }

    buildTypes {
        foo {
            debuggable true
            jniDebugBuild true
            signingConfig signingConfigs.myConfig
        }
    }
}

Thing is, I want to keep my build.gradle file in version control and don't have a good feeling having the password for my keystore (which is the same I use for other stuff, stupid, I know) on some git server.

Is there a way to load the signingConfig from an external file from somewhere on my hard drive?

like image 896
fweigl Avatar asked Jun 17 '14 12:06

fweigl


1 Answers

I use something like this.

I have a signing.properties in my app root folder.

STORE_FILE=xxxx
STORE_PASSWORD=xxx
KEY_ALIAS=xxx
KEY_PASSWORD=xxx

This file is not on under version control. Of course you can change folder.

Then in your build.gradle you can use something like this:

 android {

        signingConfigs {
            release
        }

        buildTypes {
                release {
                    signingConfig signingConfigs.release
                }     
        }
    }

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

        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
    }

If you change the folder, you have to change this line:

 def propFile = file('../signing.properties')
like image 105
Gabriele Mariotti Avatar answered Sep 30 '22 08:09

Gabriele Mariotti