为了保证OpenHarmony应用的完整性和来源可靠,在应用构建时需要对应用进行签名。经过签名的应用才能在真机设备上安装、运行、和调试。本文介绍的是使用签名工具以命令行方式进行Hap包签名的方法。
1,在DevEco Studio中新建一个OpenHarmony应用,在build-profile.json5中签名配置先设置为空,即"signingConfigs": [],
2,编译成功后,在工程目录\entry\build\default\outputs\default路径下,可见生成的未签名entry-default-unsigned包。
3,签名所需文件如下:
(1)签名密钥库文件:OpenHarmony.p12
(2)Profile签名证书:OpenHarmonyProfileRelease.pem、OpenHarmonyProfileDebug.pem
(3)Profile模板文件:UnsgnedReleasedProfileTemplate.json、UnsgnedDebugProfileTemplate.json
(4)证书文件:OpenHarmonyApplication.pem
(5)签名工具:hap-sign-tool.jar
以上文件可在SDK中会获得.
4,UnsgnedReleasedProfileTemplate.json文件中,会定义当前应用名称"bundle-name"、应用的权限等级"apl"。权限等级有normal、system_basic、system_core三种,默认等级为normal,对于系统应用的话,需要修改权限,此处改为system_core。
5,在DevEcoStudio的终端通过命令行生成签名的Profile文件,文件格式为*.p7b
java -jar hap-sign-tool.jar sign-profile -keyAlias "OpenHarmony Application Release" -signAlg "SHA256withECDSA" -mode "localSign" -profileCertFile "OpenHarmonyApplication.pem" -inFile "UnsgnedReleasedProfileTemplate.json" -keystoreFile "OpenHarmony.p12" -outFile "profile.p7b" -keyPwd "123456" -keystorePwd "123456"
命令输出如下:
03-20 19:03:08 INFO - Start sign-profile
03-20 19:03:08 INFO - OpenHarmony.p12 is exist. Try to load it with given passwd
03-20 19:03:09 INFO - sign-profile success
6,根据未签名的应用包以及生成的签名Profile文件,可以生成签名的应用
java -jar hap-sign-tool.jar sign-app -keyAlias "OpenHarmony Application Release" -signAlg "SHA256withECDSA" -mode "localSign" -appCertFile "OpenHarmonyApplication.pem" -profileFile "profile.p7b" -inFile "entry-default-unsigned.hap" -keystoreFile "OpenHarmony.p12" -outFile "Eric.hap" -keyPwd "123456" -keystorePwd "123456"
命令输出如下:
03-20 19:13:06 INFO - certificate in profile: ide_demo_app
03-20 19:13:06 INFO - Start to sign code.
03-20 19:13:06 INFO - OpenHarmony.p12 is exist. Try to load it with given passwd
03-20 19:13:07 INFO - Create a sign info successfully.
03-20 19:13:07 INFO - No native libs.
03-20 19:13:07 INFO - Sign successfully.
03-20 19:13:07 INFO - Add sign data in sign info list success.
03-20 19:13:07 INFO - Generate signing block success, begin write it to output file
03-20 19:13:07 INFO - Sign Hap success!
03-20 19:13:07 INFO - sign-app success
7,将生成的签名应用安装到/system/app/xxx.xxx.xxx文件中。
hdc file send .\Eric.hap /system/app/com.ohos.eric
hdc shell
cd /system/app/com.ohos.eric
bm install -p Eric.hap
此时即可在桌面上看到安装的应用。