Android系统应用(12)

如何成为系统应用

方法一:在Manifest中声明android:sharedUserId的值为:android.uid.system,android.uid.phone,android.uid.log,android.uid.nfc,android.uid.bluetooth,android.uid.shell的应用。

#android/frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java

//PMS中根据privateFlags和flags来判定一个应用是否为SystemApp或PrivilegedApp
private static boolean isSystemApp(PackageParser.Package pkg) {
    return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
}

//特权系统应用
private static boolean isPrivilegedApp(PackageParser.Package pkg) {
    return (pkg.applicationInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0;
}

private static boolean isSystemApp(PackageSetting ps) {
    return (ps.pkgFlags & ApplicationInfo.FLAG_SYSTEM) != 0;
}

//根据sharedUserId赋值flags和privateFlags,全部为特权系统应用
mSettings = new Settings(mPackages);
mSettings.addSharedUserLPw("android.uid.system", Process.SYSTEM_UID,ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
mSettings.addSharedUserLPw("android.uid.phone", RADIO_UID,ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
mSettings.addSharedUserLPw("android.uid.log", LOG_UID,ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
mSettings.addSharedUserLPw("android.uid.nfc", NFC_UID,ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
mSettings.addSharedUserLPw("android.uid.bluetooth", BLUETOOTH_UID,ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
mSettings.addSharedUserLPw("android.uid.shell", SHELL_UID,ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);

方法二:指定目录下的应用:/vendor/overlay,/system/framework,/system/priv-app,/system/app,/vendor/app,/oem/app。

#android/frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java

private static final String VENDOR_OVERLAY_DIR = "/vendor/overlay";
  
File vendorOverlayDir = new File(VENDOR_OVERLAY_DIR);
scanDirLI(vendorOverlayDir, PackageParser.PARSE_IS_SYSTEM | PackageParser.PARSE_IS_SYSTEM_DIR, scanFlags | SCAN_TRUSTED_OVERLAY, 0);

//PackageParser.PARSE_IS_PRIVILEGED标志会转换为ApplicationInfo.PRIVATE_FLAG_PRIVILEGED
// /system/framework为特权系统应用,只是包含资源,不包含代码
File frameworkDir = new File(Environment.getRootDirectory(), "framework");
scanDirLI(frameworkDir, PackageParser.PARSE_IS_SYSTEM | PackageParser.PARSE_IS_SYSTEM_DIR | PackageParser.PARSE_IS_PRIVILEGED,scanFlags | SCAN_NO_DEX, 0);
// /system/priv-app为特权系统应用
final File privilegedAppDir = new File(Environment.getRootDirectory(), "priv-app");
scanDirLI(privilegedAppDir, PackageParser.PARSE_IS_SYSTEM | PackageParser.PARSE_IS_SYSTEM_DIR | PackageParser.PARSE_IS_PRIVILEGED, scanFlags, 0);

final File systemAppDir = new File(Environment.getRootDirectory(), "app");
scanDirLI(systemAppDir, PackageParser.PARSE_IS_SYSTEM | PackageParser.PARSE_IS_SYSTEM_DIR, scanFlags, 0);

File vendorAppDir = new File("/vendor/app");
scanDirLI(vendorAppDir, PackageParser.PARSE_IS_SYSTEM | PackageParser.PARSE_IS_SYSTEM_DIR, scanFlags, 0);

final File oemAppDir = new File(Environment.getOemDirectory(), "app");
scanDirLI(oemAppDir, PackageParser.PARSE_IS_SYSTEM | PackageParser.PARSE_IS_SYSTEM_DIR, scanFlags, 0);

系统应用分类

普通系统应用
特权系统应用
定义:特权系统应用是系统应用,同时有ApplicationInfo.PRIVATE_FLAG_PRIVILEGED标志。
功能:可以使用protectionLevel为signatureOrSystem或signature|privileged的权限。

如何成为特权系统应用

根据如何成为系统应用中PackageManagerService的定义,成为特权系统应用有如下两种方法:
方法一:在Manifest中声明android:sharedUserId的值为:android.uid.system,android.uid.phone,android.uid.log,android.uid.nfc,android.uid.bluetooth,android.uid.shell的应用。
方法二:指定目录下的应用:/system/framework,/system/priv-app。

    原文作者:xcz1899
    原文地址: https://www.jianshu.com/p/3796cbbbefbc
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞