方法一:使用Android SDK中的签名工具给apk签名:
(1)Android源码的 build/target/product/security/ 目录下有 media.pk8、media.x509.pem、platform.pk8、platform.x509.pem、shared.pk8、shared.x509.pem、testkey.pk8、testkey.x509.pem等签名文件,不同的签名文件对应不同的权限,Android默认的签名文件为testkey.pk8、testkey.x509.pem。
(2) Android SDK中的签名工具为 signapk.jar,具体路径:out/host/linux-x86/framework/signapk.jar,签名指令如下:
java -jar signapk.jar platform.x509.pem platform.pk8 old.apk new.apk
8.0上验证需要额外的一些参数: (直接在源码根目录执行签名,但是7.0以上采用 APK Signature Scheme v2,会导致安装不了,但是增加参数 --disable-v2 无效,知道原因的朋友还请告知一下!)
java -Xmx2048m -Djava.library.path="out/host/linux-x86/lib64" \
-jar out/host/linux-x86/framework/signapk.jar --disable-v2 \
-w build/target/product/security/platform.x509.pem \
build/target/product/security/platform.pk8 \
old.apk new.apk
方法二:通过Android.mk配置编译成签名apk:
(1)编写Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := app //要签名的apk名称
LOCAL_SRC_FILES := app.apk //apk文件
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_SUFFIX := .apk
LOCAL_BUILT_MODULE_STEM := package.apk
LOCAL_CERTIFICATE := platform //系统签名
LOCAL_DEX_PREOPT := false
LOCAL_PRIVILEGED_MODULE := true
include $(BUILD_PREBUILT)
(2) 将apk放入.mk同目录(命名为app.apk),配置好sdk编译环境后执行mm指令编译,签名apk生成在:out/target/product/xxxx/system/priv-app/app/app.apk 。
注:LOCAL_CERTIFICATE := platform 表示使用系统签名
LOCAL_DEX_PREOPT := false 不提前优化,无oat文件
方法三:Android studio配置key签名: (1)生成key: ------>生成本地key:
----------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
------>生成系统平台key:
下载 keytool-importkeypair 工具,使用sdk的security文件生成对应平台的key:
./keytool-importkeypair -k [jks文件名] -p [jks的密码] -pk8 platform.pk8 -cert platform.x509.pem -alias [jks的别名]
如:
./keytool-importkeypair -k ./SignDemo.jks -p 123456 -pk8 platform.pk8 -cert platform.x509.pem -alias SignDemo
(2)使用key:
通过以上方法生成的SignDemo.jks更新到android studio原来设置的目录,下一步即可生成签名APK:
签名apk生成路径:\xxxx\app\release
实质是运行 Sdk\build-tools\27.0.3\lib\apksigner.jar 进行签名:
V1签名(jarsigner方式):
//jarsigner -verbose -keystore (签名地址) -signedjar (签名后的apk地址) (待签名apk地址) (别名)
jarsigner -verbose -keystore D:\itlao5.keystore -signedjar D:\itlao5_signed.apk D:\itlao5.apk itlao5
V2签名(apksigner方式):
// apksigner sign --ks (签名地址) --ks-key-alias (别名) --out (签名后的apk地址) (待签名apk地址)
apksigner sign --ks D:\itlao5.keystore --ks-key-alias itlao5 --out D:\itlao5_signed.apk D:\itlao5.apk
验证签名是否成:
apksigner verify -v --print-certs (apk地址)
每次生成签名apk都要选择 Build->Generate Signed APK 配置一下key,岂不是很麻烦?当然有简化方法,生成.jks后可以在项目的app目录下的build.gradle中进行配置,步骤如下:
① File -> Project structure -> Signing:
② File -> Project structure -> Flavors:
③ File -> Project structure -> Build Types:
通过以上配置步骤后,可在build.gradle中可以看到增加了如下配置信息(粗体),当然也可以手动直接在build.gradle中输入配置信息:
android {
signingConfigs {
release {
keyAlias 'SignDemo'
keyPassword ''
storeFile file('E:/project/androidStudio/signAPK/SignDemo.jks')
storePassword ''
}
}
......
buildTypes {
release { //生成release apk
zipAlignEnabled true //4字节对齐,减少运行内存消耗
minifyEnabled true //false = 关闭混淆
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
debug { //生成debug apk
zipAlignEnabled true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
} }
然后通过Build Variants选择对应的项目版本,直接编译生成签名apk,省去之前每次选择key麻烦。
然而对于一些开源项目,直接在build.gradle中显式配置key,那密码等信息就泄露了,所以可以把相关信息定义在local.properties中,因为local.properties存储的是本地环境资源的一些相关信息,默认不加入代码版本管理,然后build.gradle中引用其变量即可:
①在local.properties中添加信息:
## This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Mon Nov :: CST
ndk.dir=C\:\\Users\\Administrator\\AppData\\Local\\Android\\Sdk\\ndk-bundle
sdk.dir=C\:\\Users\\Administrator\\AppData\\Local\\Android\\Sdk keystore.path=E\:/project/androidStudio/signAPK/SignDemo.jks
keystore.password=
keystore.alias=SignDemo
keystore.alias_password=
②在build.gradle中引用其定义的值:
signingConfigs {
release {
//加载资源
Properties properties = new Properties()
InputStream inputStream = project.rootProject.file('local.properties').newDataInputStream()
properties.load(inputStream) //读取文件
def sdkDir = properties.getProperty('keystore.path')
storeFile file(sdkDir) //读取字段
def key_keyAlias = properties.getProperty('keystore.alias')
def key_keyPassword = properties.getProperty('keystore.password')
def key_storePassword = properties.getProperty('keystore.alias_password') keyAlias key_keyAlias
keyPassword key_keyPassword
storePassword key_storePassword
}
} ...... buildTypes {
release { //生成release apk
zipAlignEnabled true //4字节对齐,减少运行内存消耗
minifyEnabled true //false = 关闭混淆
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
通过以上方法就可以在build.gradle中隐式配置key,而Project Structure中就不用再配置key相关信息了:
关于system app的提示:
平台签名的apk,如果 AndroidManifest.xml 中指定是 android:sharedUserId="android.uid.system",即为system app:
-end-