The Android PackageManagerService10.0 interpretation of source code (AndroidManifest. XML parsing)

Android determines whether an Activity is registered in androidmanifest.xml.

# Android ActivityManagerService(AMS) source code analysis

The Android PackageManagerService10.0 source interpretation

PackageManagerService PKMS for short

Introduction of PKMS

PackageManagerService (PKMS for short) is one of the core services in the Android system. It is responsible for installing, uninstalling, and querying applications. When Android starts up, it starts (Application Management Server PKMS), which scans the system for APK files in specific directories, parses them, gets information about the application, and installs the application.

When installing an application, PKMS will parse the application’s androidmanifest. XML file to get information about activities, services, BroadcastReceiver, ContextProvider, etc. In combination with the PKMS service, the application can be used normally in OS.

On Android, the PKMS service is started by SystemServer during system startup, and the application installation process is performed after the service is started.

The main functions of PKMS

PKMS, like AMS, is one of the core services of Android system. Parse AndroidNanifest. XML manifest file, parse all node information in the manifest file 2. 3. Management of local applications, mainly including, installation, uninstall, Applications such as information query source path in/frameworks/base/core/Java/android/and/frameworks/base/services/Java/com/android /

PKMS role

The client through the Context. GetPackageManager () to obtain ApplicationPackageManager object, and mPM pointing to the Proxy agent, when calling to mPM. Method, will be called to IPackageManager Proxy Proxy method, and then through the Binder mechanism in mRemote and server side PackageManagerService communication and call the PackageManagerService method. Simply put: PKMS are server roles that belong to Binder mechanisms.

The start of the PKMS

PKMS startup process description:

SystemServer starts PKMS: First in SystemServer. StartBootstrapServices start PKMS service () function, then call startOtherServices of dex () function optimization, disk management functions, let PKMS into systemReady state.

Step 1: Start the Installer service
SystemServer.startBootstrapServices()
Installer installer = mSystemServiceManager.startService(Installer.class);
Copy the code
Step 2: Check whether the phone is encrypted
String cryptState = VoldProperties.decrypt().orElse("");
Copy the code
Step 3: Call the main method of PKMS and implement it
 PackageManagerService m = new PackageManagerService(injector, onlyCore, factoryTest);
        t.traceEnd(); // "create package manager"
        m.installWhitelistedSystemPackages();
        ServiceManager.addService("package", m);
    final PackageManagerNative pmn = m.new PackageManagerNative(a);
        ServiceManager.addService("package_native", pmn);
Copy the code
Step 4:

If the device is not encrypted, operate directly, manage the A/B OTA DexoptSystemServer to start the service, enabling the core service to be in the ready state, startor Services

 if(! mOnlyCore) {boolean disableOtaDexopt = SystemProperties.getBoolean("config.disable_otadexopt".false);
        OtaDexoptService.main(mSystemContext, mPackageManagerService);
    }
Copy the code
Step 5:

If there is no encryption equipment, performDexOptUpgrade dex optimization, achieve PKMS. UpdatePackagesIfNeeded ()

mPackageManagerService.updatePackagesIfNeeded();
Copy the code
Step 6:

Execute performFstrimIfNeeded() to complete disk maintenance,

Step 7:

The main() method of PKMS (1) checks system properties related to Package compilation (2) calls the PackageManagerService constructor (3) enables partial applications to serve multi-user scenarios (4). Register Package and Package_native in ServiceManager.

 public static PackageManagerService main(Context context, Installer installer, boolean factoryTest, boolean onlyCore) {
        // (1) check system attributes associated with Package compilation
        PackageManagerServiceCompilerMapping.checkProperties();
        // call the PackageManagerService constructor,
        PackageManagerService m = new PackageManagerService(context, installer, factoryTest, onlyCore);
        //(3) Enable some applications to serve multi-user scenarios
        m.enableSystemUserPackages();
        //(4) Register package and Package_native with the ServiceManager.
        ServiceManager.addService("package", m);
        final PackageManagerNative pmn = m.new PackageManagerNative(a);
        ServiceManager.addService("package_native", pmn);
        return m;
    }
Copy the code
public PackageManagerService(Context context, Installer installer, boolean factoryTest, boolean onlyCore)
Copy the code

(1) Construct DisplayMetrics, save resolution and other relevant information; (2) Create Installer object and interact with InstalLD; (3) Create the mPermissionManager object for permission management; /data/system/ package. XML, package-backup. XML, package. list, package. list, package. backup-xml, package. list Packages – stopped. XML, packagesstoppedbackup. XML files, etc. (5) Construct PackageDexOptimizer and DexManager class to deal with DEX optimization; (6) Create SystemConfig instance, obtain system configuration information, configure shared lib library; (7) Create the PackageManager handler thread, loop processing external installation related messages.

The APK scanning

The first step: scan APK and parse androidmanifest.xml file to get the contents of each tag in the manifest file. The second step: parse the manifest file to get the information saved by Package. From the member variables of this class, it can be seen that the information related to the four Android components is saved by Activites, Receivers, providers, and Services respectively. Therefore, Activites and Receivers, etc., are declared ArrayList. Now we know when the Androidmanifest.xml file has been parsed and the four components in it.

A brief description of APK installation

Step 2: Call the commit method of PackageInstaller.Session, and send the Apk information to PKMS for processing. Step 3: Copy the Apk to install itThe installation screen we see is actually going into the PackageInstallerActivity.

Installation principle:

PKMS permission scanning

When PackageManagerService executes systemReady(), SystemConfig’s readPermissionsFromXml() can be used to scan /system/etc/permissions XML files, including platform. XML and various hardware modules supported by the system.

void readPermissions(File libraryDir, int permissionFlag) {...// Iterate over the files in the directory and scan .xml files
        File platformFile = null;
        for (File f : libraryDir.listFiles()) {
            if(! f.isFile()) {continue;
            }
            // Finally read platform.xml
            if (f.getPath().endsWith("etc/permissions/platform.xml")) {
                platformFile = f; continue; }... readPermissionsFromXml(f, permissionFlag); }// Read platform permissions last so it will take precedence 
        if(platformFile ! =null) { readPermissionsFromXml(platformFile, permissionFlag); }}Copy the code

Tag nodes that parse XML are stored in member variables such as mGlobalGids, mPermissions, and mSystemPermissions for others to call. Look at the/system/etc/permissions/platform. The content of the XML

<? XML version =" 1.0" encoding=" UTF-8 "? ><permissions>
        <permission name="android.permission.BLUETOOTH_ADMIN">
            <group gid="net_bt_admin"/>
        </permission>
        <permission name="android.permission.INTERNET">
            <group gid="inet"/>
        </permission>
        <permission name="android.permission.READ_LOGS">
            <group gid="log"/>
        </permission>.<assign-permission name = "android.permission.MODIFY_AUDIO_SETTINGS" uid="media"/>
        <assign- permission name = "android.permission.ACCESS_SURFACE_FLINGER" uid="media"/>
        <assign- permission name = "android.permission.WAKE_LOCK"uid="media"/>.<split-permission name="android.permission.ACCESS_FINE_LOCATION">
            <new-permission name="android.permission.ACCESS_COARSE_LOCATION" />
        </split-permission>
        <library name="android.test.base" file="/system/framework/android.test.base.jar" />
    </permissions>
Copy the code
Summary: Permission scan, scan the XML in /system/etc/permissions and store it in the corresponding structure for later permission management

The above is the main source code of PackageManagerService analysis, of course, there are many details did not say, look at the source code first look at the main line, and then go to see the branch, you can look at the source code and this article together, can be more clear.