Sign APK Without Putting Keystore Info in Build.Gradle

Sign APK without putting keystore info in build.gradle

The nice thing about Groovy is that you can freely mix Java code, and it's pretty easy to read in a key/value file using java.util.Properties. Perhaps there's an even easier way using idiomatic Groovy, but Java is still pretty simple.

Create a keystore.properties file (in this example, in the root directory of your project next to settings.gradle, though you can put it wherever you like:

storePassword=...
keyPassword=...
keyAlias=...
storeFile=...

Add this to your build.gradle:

allprojects {
afterEvaluate { project ->
def propsFile = rootProject.file('keystore.properties')
def configName = 'release'

if (propsFile.exists() && android.signingConfigs.hasProperty(configName)) {
def props = new Properties()
props.load(new FileInputStream(propsFile))
android.signingConfigs[configName].storeFile = file(props['storeFile'])
android.signingConfigs[configName].storePassword = props['storePassword']
android.signingConfigs[configName].keyAlias = props['keyAlias']
android.signingConfigs[configName].keyPassword = props['keyPassword']
}
}
}

How to create a release signed apk file using Gradle?

Easier way than previous answers:

Put this into ~/.gradle/gradle.properties

RELEASE_STORE_FILE={path to your keystore}
RELEASE_STORE_PASSWORD=*****
RELEASE_KEY_ALIAS=*****
RELEASE_KEY_PASSWORD=*****

Modify your app/build.gradle, and add this inside the android { code block:

...    
signingConfigs {

release {
storeFile file(RELEASE_STORE_FILE)
storePassword RELEASE_STORE_PASSWORD
keyAlias RELEASE_KEY_ALIAS
keyPassword RELEASE_KEY_PASSWORD

// Optional, specify signing versions used
v1SigningEnabled true
v2SigningEnabled true
}
}

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

Then you can run gradle assembleRelease


Also see the reference for the signingConfigs Gradle DSL

Sign APK without putting keystore info in build.gradle

The nice thing about Groovy is that you can freely mix Java code, and it's pretty easy to read in a key/value file using java.util.Properties. Perhaps there's an even easier way using idiomatic Groovy, but Java is still pretty simple.

Create a keystore.properties file (in this example, in the root directory of your project next to settings.gradle, though you can put it wherever you like:

storePassword=...
keyPassword=...
keyAlias=...
storeFile=...

Add this to your build.gradle:

allprojects {
afterEvaluate { project ->
def propsFile = rootProject.file('keystore.properties')
def configName = 'release'

if (propsFile.exists() && android.signingConfigs.hasProperty(configName)) {
def props = new Properties()
props.load(new FileInputStream(propsFile))
android.signingConfigs[configName].storeFile = file(props['storeFile'])
android.signingConfigs[configName].storePassword = props['storePassword']
android.signingConfigs[configName].keyAlias = props['keyAlias']
android.signingConfigs[configName].keyPassword = props['keyPassword']
}
}
}

Android Studio don't recognise signing configuration declared in a properties file

Make sure you have:

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

in module.

Is it safe to write password in signing configuration?

I have been thinking about that recently and I've figured out better solution. You can put in gradle configuration information from where it should look for signing data. So If You are working with git You can ignore that file. Any new developer after taking the project and willing to sign apk will have to obtain mentioned file before building release.

Here is the example. In gradle file in signingConfigs tag You use Properties which is reading keystore.config containing signing information:

signingConfigs {
release {
Properties keystoreProps = new Properties()
keystoreProps.load(new FileInputStream(file('keystore.config')))

keyAlias keystoreProps['keyAlias']
keyPassword keystoreProps['keyPassword']
storePassword keystoreProps['storePassword']
storeFile file('keystore.jks')
}
}

Here is the keystore.config file:

keyAlias=MyAlias
keyPassword=MyPassword
storePassword=StorePassword

The above solution assumes that the keystore.jks and keystore.config are located in the app folder but You are free to change the path.

Android Error Building Signed APK: keystore.jks not found for signing config 'externalOverride'

I found the solution. I misplaced the path to the keystore.jks file.
Searched for the file on my computer used that path and everything worked great.



Related Topics



Leave a reply



Submit