python签名脚本,同时支持v1和v2签名

  • 由于android7.0开始采用v2签名,以前的美团等多渠道打包方式会导致v2签名失效;虽然暂时可以使用v2SigningEnabled false关闭,但不保证今后也能使用;为彻底解决问题,就些了这个脚本对多渠道包进行重新签名。

  • 请自行配置jarsigner zipalign apksigner 的环境变量

  • 代码:

# -*- coding: utf-8 -*-
import os
import shutil
import os.path
import re

BASE_DIR = os.path.dirname(__file__)

out = os.path.exists('out')
if(out):
    shutil.rmtree('out')
    os.mkdir('out')
else:
    os.mkdir('out')

_build = os.path.exists('build')
if(_build):
    shutil.rmtree('build')
    os.mkdir('build')
else:
    os.mkdir('build')

keystore = '@@@@@@@@@'
keypass = '@@@@@@@@@'
keyalias = '@@@@@@@@@'

BASE_DIR = os.path.dirname(__file__)

outDir = os.path.join(BASE_DIR, "out")
buildDir = os.path.join(BASE_DIR, "build")
targetDir = os.path.join(BASE_DIR, "target")
list = os.listdir(os.path.join(BASE_DIR, "target"))
for file in list:
    print("start sign: " + file)
    signedFile = os.path.join(buildDir, file + "signed.apk")
    outFile = os.path.join(outDir, file)
    f = os.path.join(targetDir, file)

    # v1签名
    signcmd = 'jarsigner -sigalg SHA1withRSA -digestalg SHA1 -keystore "%s" -storepass "%s" -signedjar "%s" "%s" "%s"' % (keystore, keypass, signedFile, f, keyalias)
    os.system(signcmd)

    # zipalign
    aligncmd = 'zipalign -f 4 "%s" "%s"' % (signedFile, outFile)
    os.system(aligncmd)

    # v2签名
    signcmd2 = 'apksigner sign --ks %s --ks-pass pass:%s --ks-key-alias %s %s' % (keystore, keypass, keyalias, outFile)
    os.system(signcmd2)

    print(file + " finish\n")
  • v2签名apksigner说明:
--ks 
The signer's private key and certificate chain reside in the given Java-based KeyStore file. If the filename is set to "NONE", the KeyStore containing the key and certificate doesn't need a file specified, which is the case for some PKCS #11 KeyStores.
--ks-key-alias 
The name of the alias that represents the signer's private key and certificate data within the KeyStore. If the KeyStore associated with the signer contains multiple keys, you must specify this option.
--ks-pass 
The password for the KeyStore that contains the signer's private key and certificate. You must provide a password to open a KeyStore. The apksigner tool supports the following formats:

pass: – Password provided inline with the rest of the apksigner sign command.
env: – Password is stored in the given environment variable.
file: – Password is stored as a single line in the given file.
stdin – Password is provided as a single line in the standard input stream. This is the default behavior for --ks-pass.
Note: If you include multiple passwords in the same file, specify them on separate lines. The apksigner tool associates passwords with an APK's signers based on the order in which you specify the signers. If you've provided two passwords for a signer, apksigner interprets the first password as the KeyStore password and the second one as the key password.

https://developer.android.com/studio/command-line/apksigner.html

此为blog备份,原地址:http://blog.yzapp.cn/python签名脚本,同时支持v1和v2签名.html

你可能感兴趣的:(python签名脚本,同时支持v1和v2签名)