Confused with signing android APK?

2019-02-27 10:45发布

问题:

I have followed the steps as per officials says for digitally signing my android application.for signing in release mode they are saying to use .keystore file and it's credentials like this.I am using android studio so that am getting .jks file instead.So where do i need to keep the .jks file according to the docs for building my signed APK ? Please give a simple elaboration on this.and say if am doing anything wrong ?

Thanks.

回答1:

You can put the .jks file anywhere.
It means that you can put the file inside your project, or you can put the file in an external folder (just take a look the path below). It depends by your policy.

Just use gradle to configure the signing of your apk.

android {

    signingConfigs {
        release
    }

    buildTypes {
            release {
                signingConfig signingConfigs.release
            }
    }
}

Simple way: just define the credential inside your build.gradle file.

signingConfigs {
     release {
            //Pay attention to the path. You can use a relative path or an absolute path
            storeFile file("../your_key_store_file.jks")
            storePassword 'some_password'
            keyAlias 'alias_name'
            keyPassword 'key_password'

     }
  }

Using a .properties file to store the credentials outside the script (for example if you don't want to push the credential in a git repo).

Example: signing.properties

STORE_FILE=/path/to/your.keystore
STORE_PASSWORD=yourkeystorepass
KEY_ALIAS=projectkeyalias
KEY_PASSWORD=keyaliaspassword

Then get these values in your build.gradle file:

signingConfigs {
        release
 }

Then define:

def Properties props = new Properties()
def propFile = new File('signing.properties')
if (propFile.canRead()){
    props.load(new FileInputStream(propFile))

    if (props!=null && props.containsKey('STORE_FILE') && props.containsKey('STORE_PASSWORD') &&
            props.containsKey('KEY_ALIAS') && props.containsKey('KEY_PASSWORD')) {
        android.signingConfigs.release.storeFile = file(props['STORE_FILE'])
        android.signingConfigs.release.storePassword = props['STORE_PASSWORD']
        android.signingConfigs.release.keyAlias = props['KEY_ALIAS']
        android.signingConfigs.release.keyPassword = props['KEY_PASSWORD']
    } else {
        println 'signing.properties found but some entries are missing'
        android.buildTypes.release.signingConfig = null
    }
}else {
    println 'signing.properties not found'
    android.buildTypes.release.signingConfig = null
}