Release signing in gradle.properties for Android

后端 未结 6 1017
故里飘歌
故里飘歌 2021-01-30 23:20

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 th

相关标签:
6条回答
  • 2021-01-31 00:03

    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

    0 讨论(0)
  • 2021-01-31 00:07

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

    0 讨论(0)
  • 2021-01-31 00:08

    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.

    0 讨论(0)
  • 2021-01-31 00:09

    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
    }
    
    0 讨论(0)
  • 2021-01-31 00:12

    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'))
    }
    
    0 讨论(0)
  • 2021-01-31 00:23

    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
    }
    
    0 讨论(0)
提交回复
热议问题