Release signing in gradle.properties for Android

本秂侑毒 提交于 2019-12-02 16:46:17
Xavier Ducrohet

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.

GrkEngineer

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

Inspired by Eugens solution i came up with a little shorter variance. The code has to be in the android {} task configuration.

File signFile = rootProject.file('sign.properties')
if (signFile.exists()) {
    Properties p = new Properties()
    p.load(new FileInputStream(signFile))
    signingConfigs {
        releaseConfig {
            storeFile file(p.storeFile)
            storePassword p.storePassword
            keyAlias p.keyAlias
            keyPassword p.keyPassword
        }
    }
    buildTypes.release.signingConfig signingConfigs.releaseConfig
}

Gradle 1.9 doesn't allow you to define properties. You can add them only to ext now. Small addition to previous answers:

signingConfigs {
    debug {
        project.ext.loadSign = false
    }
    release {
        project.ext.loadSign = true
    }
}

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

if ( project.ext.loadSign ) {
    Properties p = new Properties ()
    p.load ( new FileInputStream ( rootProject.file ( 'keys/sign.properties' ) ) )

    android.signingConfigs.release.storeFile file ( p.file )
    android.signingConfigs.release.storePassword p.password
    android.signingConfigs.release.keyAlias p.alias
    android.signingConfigs.release.keyPassword p.keyPassword
}

My requirements were that anyone without the keystore should be able to build properly. This is the cleanest way I could find:

android {
    signingConfigs {
        release    //Filled in readSigningConfigIfAvailable()
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-xyz.txt'
            readSigningConfigIfAvailable()
        }
    }
}

private void readSigningConfigIfAvailable() {
    if (hasAllSigningProperties()) {
        android.signingConfigs.release.storeFile = file(keystore_path)
        android.signingConfigs.release.storePassword = keystore_password
        android.signingConfigs.release.keyAlias = key_alias
        android.signingConfigs.release.keyPassword = key_password
        android.buildTypes.release.signingConfig = android.signingConfigs.release
    } else {
        android.buildTypes.release.signingConfig = null
    }
}

private boolean hasAllSigningProperties() {
    (hasProperty('keystore_path')
    && hasProperty('keystore_password')
    && hasProperty('key_alias')
    && hasProperty('key_password'))
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!