Sign the .apk package using the command

Based on project encryption requirements, after using an external encryption tool, the original signature of the apk will be eliminated, so the encrypted apk needs to be re-signed.


The signature steps are as follows:

1. Use cmd to enter the bin subdirectory under the JDK installation directory
cd F:\Android\SdkJdk\jdk\bin
2. Copy the .keystore file and the .apk file that needs to be signed to the bin directory
3. Execute the following command to sign
// 其中 updatexxx.apk 是签名后的 apk 包名,xxx.apk 是当前没有签名的 apk 包名
// Key.keystore 是当前证书的路径,如果将证书放在 bin 目录下,则就是证书的文件名
// keyname 是该证书的别名,如果没有填写正确就会在签名时报错 “找不到证书链”
jarsigner -verbose -keystore Key.keystore -signedjar updatexxx.apk xxx.apk keyname
-verbos: 指定生成详细输出
-keystore: 指定数字证书存储路径
-signedjar: 该选项的三个参数为:签名后的 apk 包名、未签名的 apk 包名、数字证书别名

The signed apk will be directly generated in the current bin directory.

4. Optimize with Zipalign

Use Zipalign in the android sdk to optimize the signed apk file. For the introduction of Zipalign, please refer to this article .
My Zipalign here is at F:\Android\SdkJdk\android-sdk-windows\build-tools\29.0.2this location, so I need to move the signed .apk file to this directory.
Then use the following command in cmd

// 进入对应的目录
cd F:\Android\SdkJdk\android-sdk-windows\build-tools\29.0.2
// 对签名 apk 进行优化
zipalign -f -v 4 UpdateKeyStore.apk New.apk
-f:指定强制覆盖已有文件
-v:指定生成详细输出
4:指定档案整理基于的字节数,一般是 4,也有基于 32 位的
UpdateKeyStore.apk:优化前的 apk
New.apk:优化后的 apk

After the optimization is completed, the installation package New.apk can be released to the public.


Reference article:

1. Command line jarsigner signature and solution cannot find the certificate chain error
2. Android signature tool autoSign jarsigner
3. How to use the command to sign the APK package

Guess you like

Origin blog.csdn.net/EverNess010/article/details/114630571