【问题标题】:Confused with signing android APK?对签署 android APK 感到困惑?
【发布时间】:2016-02-12 06:22:46
【问题描述】:

我已按照officials 所说的步骤对我的 android 应用程序进行数字签名。在发布模式下签名他们说要使用.keystore 文件,它的凭据类似于this。我正在使用 android studio 以便我得到的是.jks 文件。那么根据docs,我需要在哪里保存.jks 文件以构建我的签名APK?请对此进行简单的阐述。并说是否做错了什么?

谢谢。

【问题讨论】:

    标签: android android-studio gradle apk jks


    【解决方案1】:

    您可以将.jks 文件放在任何位置。
    这意味着您可以将文件放在项目中,也可以将文件放在外部文件夹中(只需查看下面的路径)。 这取决于您的政策。

    只需使用 gradle 配置您的 apk 的签名。

    android {
    
        signingConfigs {
            release
        }
    
        buildTypes {
                release {
                    signingConfig signingConfigs.release
                }
        }
    }
    

    简单方法:只需在 build.gradle 文件中定义凭据即可。

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

    使用 .properties 文件将凭据存储在脚本之外(例如,如果您不想将凭据推送到 git 存储库中)。

    示例:signing.properties

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

    然后在你的 build.gradle 文件中获取这些值:

    signingConfigs {
            release
     }
    

    然后定义:

    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
    }
    

    【讨论】:

    • 这意味着我们不想将 .jks 文件保留在 apk 根目录中,即应用根目录?为什么我们在 gradle 中提供这些东西 storeFile file("myreleasekey.keystore") storePassword "password" keyAlias "MyReleaseKey" keyPassword "password" ?请详细说明我的疑问的答案。
    • @Christ 更新了答案。 .jks 的位置并不重要。使用 gradle 脚本,您可以定义路径。要使用 build.gradle 构建,您必须告诉 Gradle 凭据。您可以在脚本中或外部定义它们。
    • storePassword 和其他凭据必须与我们生成 APK 时给定的凭据匹配正确 developer.android.com/tools/publishing/app-signing.html#studio ?
    • 所以第 1 步:developer.android.com/tools/publishing/app-signing.html#studio,我们应该使用第 1 步到第 2 步中给出的凭据:developer.android.com/tools/publishing/… 对吗?
    • Oki..非常感谢您的宝贵时间和精彩的解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 2016-01-03
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    相关资源
    最近更新 更多