How to use properties file to do android sign action in Gradle

wang.chauyan
2 min readOct 4, 2016

--

In Android Studio, we usually use gradle to build/sign official release apk to Google Play Store. But it sometimes is annoying that we need to input keystore password and choose alias sign key everytime. There is an easy way to do this. Check here

We might have a build.gradle file which looks like …

android {
signingConfigs {
config_release {
storeFile file('your/key/store/path')
keyAlias 'your key store alias'
storePassword 'your key store password'
keyPassword 'your key password'
}
}
}

If we do this in this way, it’s easier to release our official application without any password input needed.

But the problem is, we store everything in a plain-text way in build.gradle. It sometimes causes some problems, e.g security issue, or key management issue. So, for me, I would rather to create a properties file to control related information.

Here I will give an example as followed.

android {
signingConfigs {
config_release {
def Properties localProps = new Properties()
localProps.load(new FileInputStream(file('../local.properties')))
def Properties keyProps = new Properties()
assert localProps['keystore.props.file'];

keyProps.load(new FileInputStream(file(localProps['keystore.props.file'])))

storeFile file(keyProps["RELEASE_STORE_FILE"])
keyAlias keyProps["RELEASE_KEY_ALIAS"]
storePassword keyProps["RELEASE_STORE_PASSWORD"]
keyPassword keyProps["RELEASE_KEY_PASSWORD"]
}
}
}

Here I create another file called keystore.properties. In this file, I put the four values inside.

RELEASE_STORE_FILE=your/key/store/path
RELEASE_KEY_ALIAS=your_key_alias
RELEASE_STORE_PASSWORD=store_password
RELEASE_KEY_PASSWORD=key_password

So, put these information into keystore.properties and then put its path value into local.properties.

keystore.props.file=your/keystore/properties/path

After doing this, we now can easily change different keystore.properties file for different builds if needed, and this keystore.properties is actually put at local side. So, we would not use the key value in a plain-text way in build.gradle.

Wait a minute, do we have more secure way to protect our password ? Yes, we can ask developer to manually input key value when building.

android {
signingConfigs {
config_release {
def Properties localProps = new Properties()
localProps.load(new FileInputStream(file('../local.properties')))
def Properties keyProps = new Properties()
assert localProps['keystore.props.file'];

keyProps.load(new FileInputStream(file(localProps['keystore.props.file'])))

storeFile file(keyProps["RELEASE_STORE_FILE"])
keyAlias keyProps["RELEASE_KEY_ALIAS"]
def sPassword = null
def kPassword = null
if (System.console() != null)
sPassword = System.console().readLine("\\\\nEnter store password ")
if (System.console() != null)
kPassword = System.console().readLine("\\\\nEnter key password ")
keyPassword kPassword
storePassword sPassword
}
}
}

Just do a little bit change, we ask developer to input related key value every time.

See, that’s simple, and enjoy gradle!

--

--