APK要想安装到设备上,无论是debug还是release都是需要先签名的,默认IDE是会使用默认的签名文件给debug的apk签名,所以我们不需要配置签名信息也是可以在设备上运行的,而release的版本强制要求我们配置签名信息,下面整理了一些应用签名相关的知识。
keytool使用的详细方式介绍
keytool -list -printcert -jarfile 【apk路径】
# 如:keytool -list -printcert -jarfile /Users/jiangwenzhong/Downloads/app-debug.apk
keytool -list -v -keystore 【签名证书路径】
# 如:keytool -list -v -keystore /Users/jiangwenzhong/Downloads/debug.keystore
获取安装到设备的应用签名工具:下载地址
这种方式需要将apk上传上去,有风险,所以不建议使用,所以这里就不贴地址了,感兴趣的可以自己搜一下关键词“在线查看apk签名”。
keytool -genkey -alias android.keystore -keyalg RSA -validity 20000 -keystore android.keystore
这种方式比较简单,就不详述了,简单说就是:Build -> Generate Signed Bundle or APK -> Create new…
简单说:v1签名是对jar进行签名,V2签名是对整个apk签名:官方介绍就是:v2签名是在整个APK文件的二进制内容上计算和验证的,v1是在归档文件中解压缩文件内容。
二者签名所产生的结果: v1:在v1中只对未压缩的文件内容进行了验证,所以在APK签名之后可以进行很多修改——文件可以移动,甚至可以重新压缩。即可以对签名后的文件在进行处理 v2:v2签名验证了归档中的所有字节,而不是单独的ZIP条目,如果您在构建过程中有任何定制任务,包括篡改或处理APK文件,请确保禁用它们,否则您可能会使v2签名失效,从而使您的APKs与Android 7.0和以上版本不兼容。
详细区别可参考:
官方说明
浅谈:android签名打包v1和v2的区别
Android开发之签名V1和V2的区别
注:v2的签名文件需要使用apksigner来签名,jarsigner和v2的签名文件不兼容。具体原因可参考:Android开发之签名V1和V2的区别中提到的。
jarsigner -verbose -keystore [您的私钥存放路径] -signedjar [签名后文件存放路径] [未签名的文件路径] [您的证书名称] -storepass 密码库密码
参考:
Android 对apk进行重签名和查看签名(window 和mac)及生成签名
同样,也比较简单,不详述,简单说就是:Build -> Generate Signed Bundle or APK
系统签名文件一般是platform.x509.pem platform.pk8
1、通过platform.x509.pem platform.pk8 生成签名keystore,可在github上下载脚本:keytool-importkeypair
2、将脚本放入platform.x509.pem platform.pk8所在目录
3、执行以下命令
./keytool-importkeypair -k ~/.android/debug.keystore -p android -pk8 platform.pk8 -cert platform.x509.pem -alias platform
-k 表示要生成的 keystore 文件的名字,这里命名为 release.keystore
-p 表示要生成的 keystore 的密码,这里是 youPassword
-pk8 表示要导入的 platform.pk8 文件
-cert 表示要导入的platform.x509.pem
-alias 表示给生成的 release.keystore 取一个别名,这是命名为 youAlias
在Android studio中使用
signingConfigs {
sginconfig {
keyAlias 'platform'
keyPassword 'android'
storeFile file('/Users/macbook/.android/debug.keystore')
storePassword 'android'
}
}
buildTypes {
release {
signingConfig signingConfigs.sginconfig
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
signingConfig signingConfigs.sginconfig
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
#! /bin/bash
#
# This file is part of keytool-importkeypair.
#
# keytool-importkeypair is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# keytool-importkeypair is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with keytool-importkeypair. If not, see
# .
#
DEFAULT_KEYSTORE=$HOME/.keystore
keystore=$DEFAULT_KEYSTORE
pk8=""
cert=""
alias=""
passphrase=""
tmpdir=""
scriptname=`basename $0`
usage() {
cat << EOF
usage: ${scriptname} [-k keystore] [-p storepass]
-pk8 pk8 -cert cert -alias key_alias
This script is used to import a key/certificate pair
into a Java keystore.
If a keystore is not specified then the key pair is imported into
~/.keystore in the user's home directory.
The passphrase can also be read from stdin.
EOF
}
cleanup() {
if [ ! -z "${tmpdir}" -a -d ${tmpdir} ]; then
rm -fr ${tmpdir}
fi
}
while [ $# -gt 0 ]; do
case $1
in
-p | --passphrase | -passphrase)
passphrase=$2
shift 2
;;
-h | --help)
usage
exit 0
;;
-k | -keystore | --keystore)
keystore=$2
shift 2
;;
-pk8 | --pk8 | -key | --key)
pk8=$2
shift 2
;;
-cert | --cert | -pem | --pem)
cert=$2
shift 2
;;
-a | -alias | --alias)
alias=$2
shift 2
;;
*)
echo "${scriptname}: Unknown option $1, exiting" 1>&2
usage
exit 1
;;
esac
done
if [ -z "${pk8}" -o -z "${cert}" -o -z "${alias}" ]; then
echo "${scriptname}: Missing option, exiting..." 1>&2
usage
exit 1
fi
for f in "${pk8}" "${cert}"; do
if [ ! -f "$f" ]; then
echo "${scriptname}: Can't find file $f, exiting..." 1>&2
exit 1
fi
done
if [ ! -f "${keystore}" ]; then
storedir=`dirname "${keystore}"`
if [ ! -d "${storedir}" -o ! -w "${storedir}" ]; then
echo "${scriptname}: Can't access ${storedir}, exiting..." 1>&2
exit 1
fi
fi
# Create temp directory ofr key and pkcs12 bundle
tmpdir=`mktemp -q -d "/tmp/${scriptname}.XXXX"`
if [ $? -ne 0 ]; then
echo "${scriptname}: Can't create temp directory, exiting..." 1>&2
exit 1
fi
key="${tmpdir}/key"
p12="${tmpdir}/p12"
if [ -z "${passphrase}" ]; then
# Request a passphrase
read -p "Enter a passphrase: " -s passphrase
echo ""
fi
# Convert PK8 to PEM KEY
openssl pkcs8 -inform DER -nocrypt -in "${pk8}" -out "${key}"
# Bundle CERT and KEY
openssl pkcs12 -export -in "${cert}" -inkey "${key}" -out "${p12}" -password pass:"${passphrase}" -name "${alias}"
# Print cert
echo -n "Importing \"${alias}\" with "
openssl x509 -noout -fingerprint -in "${cert}"
# Import P12 in Keystore
keytool -importkeystore -deststorepass "${passphrase}" -destkeystore "${keystore}" -srckeystore "${p12}" -srcstoretype PKCS12 -srcstorepass "${passphrase}"
# Cleanup
cleanup