如何通过命令行签署 apk [英] How to sign an apk through command line

查看:40
本文介绍了如何通过命令行签署 apk的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

被告知我们已经在 Android SDK 的帮助下通过命令行创建了一个 apk 文件.现在因为将它上传到 google play 商店需要对 apk 进行签名.我们该怎么做.

Be informed that we have created an apk file through command line with the help of Android SDK. Now since uploading it to google play store needs the apk to be signed. How shall we do this.

推荐答案

步骤 1

首先你需要生成一个私有签名密钥

Step 1

First you need to generate a private signing key

keytool -genkey -v -keystore my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000

此命令将提示您输入密钥库和密钥的密码(还有一些其他字段).请记住随时保持您的密钥库文件私密.

This command will prompt you for a password for your keystore and key (also for some additional fields). Please remember to keep your keystore file private at anytime.

接下来你需要设置gradle

Next you need to setup gradle

  1. 将您在第 1 步中生成的 my-release-key.keystore 放在 android/app
  2. android/app 下更新您的 ~/.gradle/gradle.properties 并添加以下内容

  1. Place my-release-key.keystore which you generated in Step 1 under android/app
  2. Update your ~/.gradle/gradle.properties under android/app and add the following

MYAPP_RELEASE_STORE_FILE=my-release-key.keystore
MYAPP_RELEASE_KEY_ALIAS=my-key-alias
MYAPP_RELEASE_STORE_PASSWORD=<The password you choose earlier with the keytool>
MYAPP_RELEASE_KEY_PASSWORD=<The password you choose earlier with the keytool>

步骤 3

最后你需要更新你的android/app/build.gradle.

android {
    ...
    defaultConfig { ... }
    signingConfigs {
        release {
            if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
                storeFile file(MYAPP_RELEASE_STORE_FILE)
                storePassword MYAPP_RELEASE_STORE_PASSWORD
                keyAlias MYAPP_RELEASE_KEY_ALIAS
                keyPassword MYAPP_RELEASE_KEY_PASSWORD
            }
        }
    }
    buildTypes {
        release {
            ...
            signingConfig signingConfigs.release
        }
    }
}

现在,您可以通过在 android 目录中运行以下命令,通过命令行简单地生成签名版本

Now you can simply generate a signed release via the command line by running the following command in your android directory

./gradlew assembleRelease

生成的 apk 可以在您的 build/outputs/apk/release 目录下找到.

The generated apk can then be found under your build/outputs/apk/release directory.

这篇关于如何通过命令行签署 apk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆