BundleMgr提供了一个方法CheckIsSystemAppByUid,可以用来判断该应用是不是系统应用。起用法如下:
BundleMgr实例,如下:static sptr<IBundleMgr> GetBundleMgr()
{
auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (sam == nullptr) {
USB_HILOGW(MODULE_USB_SERVICE, "GetSystemAbilityManager return nullptr");
return nullptr;
}
auto bundleMgrSa = sam->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
if (bundleMgrSa == nullptr) {
USB_HILOGW(MODULE_USB_SERVICE, "GetSystemAbility return nullptr");
return nullptr;
}
auto bundleMgr = iface_cast<IBundleMgr>(bundleMgrSa);
if (bundleMgr == nullptr) {
USB_HILOGW(MODULE_USB_SERVICE, "iface_cast return nullptr");
}
return bundleMgr;
}
uid,将该uid传给CheckIsSystemAppByUid来判断其是否是系统应用。bool UsbRightManager::IsSystemHap()
{
pid_t uid = IPCSkeleton::GetCallingUid();
auto bundleMgr = GetBundleMgr();
if (bundleMgr == nullptr) {
USB_HILOGW(MODULE_USB_SERVICE, "BundleMgr is nullptr, return false");
return false;
}
return bundleMgr->CheckIsSystemAppByUid(uid);
}
hap-sign-tool.jar,如下(选自官方教程):1、该工具基于Gradle 7.1编译构建,请确认环境已安装配置Gradle环境,并且版本正确
gradle -v
2、命令行打开文件目录至developtools_hapsigner/hapsigntool,执行命令进行编译打包
gradle build 或者 gradle jar
3、编译后得到二进制文件,目录为: ./hap_sign_tool/build/libs/hap-sign-tool.jar
autosign/UnsgnedReleasedProfileTemplate.json文件中的app-feature值为ohos_system_app,bundle-name值为自己应用的包名,可以在应用工程的AppScope/app.json5文件中查看包名;有些接口对apl要求较高,如有需要可将apl值改为system_core如下"bundle-name": "自己应用的包名",
"apl": "system_core",
"app-feature": "ohos_system_app"`
app1-unsigned.hap放到autosign/目录下。create_appcert_sign_profile.bat和sign_hap.bat脚本,就会在目录developtools_hapsigner-master/autosign/result下生成一个名为app1-signed.hap的已签名的系统应用。git checkout autosign/result/OpenHarmony.p12回退OpenHarmony.p12文件的修改。然后再次执行create_appcert_sign_profile.bat和sign_hap.bat。后续签名只需要执行sign_hap.bat。app-feature字段,如果是hos_normal_app则为普通应用,是ohos_system_app则为系统应用。1、deveco官方下载:https://developer.harmonyos.com/cn/develop/deveco-studio
2、OpenHarmony包签名工具:https://gitee.com/openharmony/developtools_hapsigner
3、gradle安装与使用:https://gradle.org/install/#manually