Release signing in gradle.properties for Android

落爺英雄遲暮 提交于 2019-12-03 03:15:41

问题


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?


回答1:


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.




回答2:


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




回答3:


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
}



回答4:


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
}



回答5:


There's a good guide on this - https://github.com/almalkawi/Android-Guide/wiki/Generating-signed-release-APK-using-Gradle




回答6:


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'))
}


来源:https://stackoverflow.com/questions/15752646/release-signing-in-gradle-properties-for-android

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