Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Release signing in gradle.properties for Android

Tags:

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?

like image 877
Gnathonic Avatar asked Apr 01 '13 21:04

Gnathonic


People also ask

What is signing config in gradle?

In Android Studio, you can configure your project to sign the release version of your app automatically during the build process by creating a signing configuration and assigning it to your release build type. A signing configuration consists of a keystore location, keystore password, key alias, and key password.


2 Answers

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.

like image 159
Xavier Ducrohet Avatar answered Oct 20 '22 19:10

Xavier Ducrohet


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

like image 45
GrkEngineer Avatar answered Oct 20 '22 20:10

GrkEngineer