今天组长突然让我给第三方应用系统签名,按照网上搜索的常规方法,没一个奏效,还报错,贼郁闷,最后在龙哥的指导下搞定。
现在将方法整理出来,希望对大家有所帮助~
首先该应用一定要在AndroidManifest中加入系统级权限
android:sharedUserId="android.uid.system"
直接安装会报错提示没有系统签名,需要把该应用系统签名
Failure [INSTALL_FAILED_SHARED_USER_INCOMPATIBLE: Package couldn't be installed in /data/app/com.heyzqt.translationdemo-r7KmIstNnpWKc5auFnisUA==: Package com.heyzqt.translationdemo has no signatures that match those in shared user android.uid.system; ignoring!]
找到Android源码目录android/o-base/build/target/product/security下的platform.pk8和platform.x509.pem文件,以及out/host/linux-x86/framework/signapk.jar文件置于统一目录下,使用cmd执行以下命令:
signapk.jar platform.x509.pem platform.pk8 app.apk new.apk
执行成功,但是没有生成新的签名apk,没有作用
signapk platform.x509.pem platform.pk8 app.apk app1.apk
提示signapk既不是内部或外部命令,也不是可执行的batch文件
java -jar signapk.jar platform.x509.pem platform.pk8 app.apk app.apk
报错:类库conscrypt_openjdk_jni-windows-x86_64和conscrypt_openjdk_jni加载失败,无法签名
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.conscrypt.OpenSSLBIOInputStream.(OpenSSLBIOInputStream.java:37)
at org.conscrypt.OpenSSLX509Certificate.fromX509PemInputStream(OpenSSLX509Certificate.java:129)
at org.conscrypt.OpenSSLX509CertificateFactory$1.fromX509PemInputStream(OpenSSLX509CertificateFactory.java:226)
at org.conscrypt.OpenSSLX509CertificateFactory$1.fromX509PemInputStream(OpenSSLX509CertificateFactory.java:222)
at org.conscrypt.OpenSSLX509CertificateFactory$Parser.generateItem(OpenSSLX509CertificateFactory.java:100)
at org.conscrypt.OpenSSLX509CertificateFactory.engineGenerateCertificate(OpenSSLX509CertificateFactory.java:278)
at java.security.cert.CertificateFactory.generateCertificate(Unknown Source)
at com.android.signapk.SignApk.readPublicKey(SignApk.java:184)
at com.android.signapk.SignApk.main(SignApk.java:1007)
Caused by: java.lang.IllegalArgumentException: Failed to load any of the given libraries: [conscrypt_openjdk_jni-windows-x86_64, conscrypt_openjdk_jni]
at org.conscrypt.NativeLibraryLoader.loadFirstAvailable(NativeLibraryLoader.java:176)
at org.conscrypt.NativeCryptoJni.init(NativeCryptoJni.java:49)
at org.conscrypt.NativeCrypto.(NativeCrypto.java:57)
... 9 more
在终端上执行source build/envsetup.sh,再执行lunch
新建android/o-base/vendor/test目录,在该目录中放入未签名的app.apk文件和Android.mk文件
Android.mk文件参考源码vendor/google/apps/Android.mk文件,因为该目录下都是apk文件,一部分是需要系统签名的apk(LOCAL_CERTIFICATE := platform),而另一部分是不需要签名的apk(LOCAL_CERTIFICATE := PRESIGNED)
具体内容如下
LOCAL_PATH := $(call my-dir)
##############################
include $(CLEAR_VARS)
LOCAL_MODULE := app
LOCAL_SRC_FILES := app.apk
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_SUFFIX := .apk
LOCAL_BUILT_MODULE_STEM := package.apk
LOCAL_CERTIFICATE := platform
LOCAL_PRIVILEGED_MODULE := true
include $(BUILD_PREBUILT)
核心是下面这句话,意思为签署当前的应用名称,实际上也就是用platform.pk8文件和platform.x509.pem来给改应用签名
LOCAL_CERTIFICATE := platform
配置好后,在vendor/app目录下执行mm命令编译apk,
在目录下out/target/product/xxxx/system/priv-app/app/app.apk找到编译好后的apk,此时该apk已经被成功签名
adb install app.apk
success
成功安装,搞定!
参考文章:
关于android:sharedUserId="android.uid.system"这个系统级权限
Android.mk详解
Android Studio自动生成带系统签名的apk