Android 手機安裝apk流程分析

和你一起終身學習,這里是程序員 Android

本篇文章主要介紹 Android 開發中的部分知識點,通過閱讀本篇文章,您將收獲以下內容:

一、前言
二、PackageInstaller介紹
三、App安裝過程中涉及類
四、App安裝流程分析
五、總結
六、參考文獻

一、前言

首先本文不是做PackageManagerService學習總結,PackageManagerService這貨有1萬2千多行代碼,學習起來頗費勁,并且這貨功能強大,本文只會總結其中一個小小的功能
為何要做這個總結呢?說來話長,鄙人菜鳥一枚,接到一個安裝應用過程中重啟的問題,原因找到,但不知如何解決,無奈,只有硬著頭皮學習了下這部分內容
OK,廢話不多說,接下來直接上干貨,如果文中有問題或有質疑的地方可以直接修改,不勝感激。

二、PackageInstaller介紹

PackageInstaller是個神馬東西呢?
我們知道安裝app有很多中方式,諸如adb install,應用助手(豌豆莢),開機安裝(開機啟動時),下載到手機存儲后點擊安裝。PackageInstaller這哥就是給手動安裝app提供一個界面的apk。
當我們點安裝應用時會啟動這個應用來顯示安裝過程,安裝的事情并不是他在做,正在安裝是由PackageManagerService來完成,當然幕后英雄確是Installer。
代碼位置
packages/apps/PackageInstaller
疑問【以下有幾個問題,如果親都知道的話,那么可以不用再看本文啦】
如何使用PackageInstaller來安裝應用?
應用首選安裝位置是在何時確定的?是在設置里設置的,還是在app中定義的,還是PackageManagerService這貨說了算?
安裝應用過程中,哪些服務和類會插手這件事?
安裝過程中首先會生成一個.tmp臨時文件,這個文件在何時被rename為apk的?
應用都有uid,這個uid是在什么時候被賦值的?
packages.xml and packages.list有什么用?
安裝app主要做了哪些事?

三、App安裝過程中涉及類

先來盤類圖看看,如對PackageManagerService不熟悉的話,先看后面的流程,看完再來看這個類圖

安裝過程時會插手的主要類如下

packages/apps/PackageInstaller/src/com/android/packageinstaller/PackageInstallerActivity.java
packages/apps/PackageInstaller/src/com/android/packageinstaller/InstallAppProgress.java
frameworks/base /core/java/android/app/ApplicationPackageManager.java
frameworks/base /core/java/android/content/pm/PackageParser.java
frameworks/base /core/java/android/content/res/AssetManager.java
frameworks/base /packages/DefaultContainerService/src/com/android/defcontainer/DefaultContainerService.java
frameworks/base /services/java/com/android/server/pm/Installer.java
frameworks/base /services/java/com/android/server/pm/PackageManagerService.java
frameworks/base /services/java/com/android/server/pm/Settings.java

簡單說明這些類作用

  • PackageInstallerActivity.java:
    在文件管理器里點擊apk后就會調用該類,主要用于顯示要安裝的apk的一些權限信息
  • InstallAppProgress.java:
    當看完所有權限后,點安裝后就會調用該類,用于顯示安裝進度,這時候- PackageManagerService 就在默默的安裝應用
  • ApplicationPackageManager.java:
    這是類是PackageManager的兒子,我們使用mContext.getPackageManager得到的其實就是ApplicationPackageManager的對象,它爹PackageManager是個抽象類,對外的方法都定義在里面
  • PackageParser.java:
    解析app,主要解析apk中的AndroidManifest.xml,解析里面的四大組件以及權限信息放入內存里,最后寫到packages.xml和package.list(/data/system下)中
  • AssetManager.java:
    把AndroidManifest.xml從app中拿出來給PackageParser.java去解析
  • DefaultContainerService.java:
    這個服務用于檢查存儲狀態,得到合適的安裝位置
  • Installer.java:
    PackageManagerService調用它去執行安裝,他會把PackageManagerService傳過來的數據封裝成命令,然后讓底層的Installer去執行
  • PackageManagerService.java:
    管理app的大神,安裝、移動、卸載、查詢等都由他管

四、App安裝流程分析

先來個時序圖--安裝成功的時序圖。點擊兩次可看大圖

安裝流程

流程分析,當然對著代碼看上面的時序圖也很明了
1.當點擊文件管理器中的apk時,會調用FolderFragment的openFile方法,該方法里會將應用信息傳給PackageInstallerActivity,并啟動PackageInstaller

代碼位置:vendor/qcom/proprietary/qrdplus/FileExplorer/src/com/android/qrdfileexplorer/FolderFragment.java

private void openFile(File f) {
        final Uri fileUri = Uri.fromFile(f);
        final Intent intent = new Intent();
        intent.setAction(android.content.Intent.ACTION_VIEW);
        intent.putExtra(Intent.EXTRA_TITLE, f.getName());
        intent.putExtra(EXTRA_ALL_VIDEO_FOLDER, true);
        Uri contentUri = null;
        String type = getMIMEType(f);
        ......
            if (contentUri != null) {
                intent.setDataAndType(contentUri, type);
            } else {
                intent.setDataAndType(fileUri, type);
            }
            try {
                startActivitySafely(intent);
            } 
        ......
    }

2.PackageInstaller啟動過后會檢查是否開啟未知來源,未開啟就需要先進入設置設置后,方可繼續安裝,之后會依次調用initiateInstall()->startInstallConfirm();
在initiateInstall中會檢查是否已經安裝過,是否是系統應用等,調用startInstallConfirm去初始化界面,顯示權限信息,當點擊安裝按鈕時,啟動安裝,切換界面到InstallAppProgress

代碼位置:packages/apps/PackageInstaller/src/com/android/packageinstaller/PackageInstallerActivity.java
 @Override
    protected void onCreate(Bundle icicle) {
        ......
        mPm = getPackageManager();
        boolean requestFromUnknownSource = isInstallRequestFromUnknownSource(intent);
        ......
        initiateInstall();
    }
    private void initiateInstall() {
        String pkgName = mPkgInfo.packageName;
        String[] oldName = mPm.canonicalToCurrentPackageNames(new String[] { pkgName });
        if (oldName != null && oldName.length > 0 && oldName[0] != null) {
            pkgName = oldName[0];
            mPkgInfo.packageName = pkgName;
            mPkgInfo.applicationInfo.packageName = pkgName;
        }
        // Check if package is already installed. display confirmation dialog if replacing pkg
        try {
            // This is a little convoluted because we want to get all uninstalled
            // apps, but this may include apps with just data, and if it is just
            // data we still want to count it as "installed".
            mAppInfo = mPm.getApplicationInfo(pkgName,
                    PackageManager.GET_UNINSTALLED_PACKAGES);
            if ((mAppInfo.flags&ApplicationInfo.FLAG_INSTALLED) == 0) {
                mAppInfo = null;
            }
        } catch (NameNotFoundException e) {
            mAppInfo = null;
        }
        mInstallFlowAnalytics.setReplace(mAppInfo != null);
        mInstallFlowAnalytics.setSystemApp(
                (mAppInfo != null) && ((mAppInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0));
        startInstallConfirm();
    }

3.在InstallAppProgress中會調用initView去初始化界面并調用ApplicationPackageManager的installPackageWithVerificationAndEncryption方法來安裝

代碼位置:packages/apps/PackageInstaller/src/com/android/packageinstaller/InstallAppProgress.java
  @Override
    public void onCreate(Bundle icicle) {
        ......
        initView();
    }
    public void initView() {
        ......
        if ("package".equals(mPackageURI.getScheme())) {
            try {
                pm.installExistingPackage(mAppInfo.packageName);
                observer.packageInstalled(mAppInfo.packageName,
                        PackageManager.INSTALL_SUCCEEDED);
            } catch (PackageManager.NameNotFoundException e) {
                observer.packageInstalled(mAppInfo.packageName,
                        PackageManager.INSTALL_FAILED_INVALID_APK);
            }
        } else {
            pm.installPackageWithVerificationAndEncryption(mPackageURI, observer, installFlags,
                    installerPackageName, verificationParams, null);
        }
    }

4.ApplicationPackageManager的installPackageWithVerificationAndEncryption里也是調用PMS的installPackageWithVerificationAndEncryption方法

代碼位置:frameworks/base/core/java/android/app/ApplicationPackageManager.java
    @Override
    public void installPackageWithVerificationAndEncryption(Uri packageURI,
            IPackageInstallObserver observer, int flags, String installerPackageName,
            VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
        try {
            mPM.installPackageWithVerificationAndEncryption(packageURI, observer, flags,
                    installerPackageName, verificationParams, encryptionParams);
        } catch (RemoteException e) {
            // Should never happen!
        }
    }

5.installPackageWithVerificationAndEncryption方法里,首先會獲取設置中的用戶安裝位置,并且會把InstallParams對象和安裝位置flag封裝到Message里,然后發出一個消息后就撒手不管了。

代碼位置:frameworks/base/services/java/com/android/server/pm/PackageManagerService.java
public void installPackageWithVerificationAndEncryption(Uri packageURI,
            IPackageInstallObserver observer, int flags, String installerPackageName,
            VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.INSTALL_PACKAGES,
                null);
        final int uid = Binder.getCallingUid();
        if(getInstallLocation() == PackageHelper.APP_INSTALL_INTERNAL){
            userFilteredFlags = flags |PackageManager.INSTALL_INTERNAL;
        } else if(getInstallLocation() == PackageHelper.APP_INSTALL_EXTERNAL){
            userFilteredFlags = flags |PackageManager.INSTALL_EXTERNAL;
        } else{
            userFilteredFlags = filteredFlags;
        }
        final Message msg = mHandler.obtainMessage(INIT_COPY);
        msg.obj = new InstallParams(packageURI, observer, userFilteredFlags, installerPackageName,
                verificationParams, encryptionParams, user);
        mHandler.sendMessage(msg);
    }

6.接下來就該PackageHandler上場了,會依次處理INIT_COPY、MCS_BOUN消息,這里面會去連接DefaultContainerService服務,接著會InstallParams的startCopy方法

代碼位置:frameworks/base/services/java/com/android/server/pm/PackageManagerService.java ->內部類:PackageHandler
 public void handleMessage(Message msg) {
        try {
            doHandleMessage(msg);
        } finally {
            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
        }
    }
    void doHandleMessage(Message msg) {
        switch (msg.what) {
            case INIT_COPY: {
                HandlerParams params = (HandlerParams) msg.obj;
                if (!mBound) {
                    if (!connectToService()) {
                        params.serviceError();
                        return;
                    } else {
                        mPendingInstalls.add(idx, params);
                    }
                } else {
                    mPendingInstalls.add(idx, params);
                    if (idx == 0) {
                        mHandler.sendEmptyMessage(MCS_BOUND);
                    }
                }
                break;
            }
            case MCS_BOUN: {
                if (msg.obj != null) {
                    mContainerService = (IMediaContainerService) msg.obj;
                }
                if (mContainerService == null) {
                    for (HandlerParams params : mPendingInstalls) {
                        params.serviceError();
                    }
                    mPendingInstalls.clear();
                } else if (mPendingInstalls.size() > 0) {
                    HandlerParams params = mPendingInstalls.get(0);
                    if (params != null) {
                        if (params.startCopy()) {
                            ......
                        }
                    }
                } else {
                    Slog.w(TAG, "Empty queue");
                }
                break;
            }
            ......
            case POST_INSTALL: {
                ...
                if (data != null) {
                    InstallArgs args = data.args;
                    PackageInstalledInfo res = data.res;
                    if (res.returnCode == PackageManager.INSTALL_SUCCEEDED) {
                        ......
                        sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED,
                                res.pkg.applicationInfo.packageName, extras, null, null, firstUsers);
                        final boolean update = res.removedInfo.removedPackage != null;
                        if (update) {
                            extras.putBoolean(Intent.EXTRA_REPLACING, true);
                        }
                        sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED,
                                res.pkg.applicationInfo.packageName, extras, null, null, updateUsers);
                        if (update) {
                            sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED,
                                    res.pkg.applicationInfo.packageName, extras, null, null, updateUsers);
                            sendPackageBroadcast(Intent.ACTION_MY_PACKAGE_REPLACED,
                                    null, null, res.pkg.applicationInfo.packageName, null, updateUsers);
                            if (isForwardLocked(res.pkg) || isExternal(res.pkg)) {
                                ......
                                sendResourcesChangedBroadcast(true, true, pkgList,uidArray, null);
                            }
                        }
                        if (res.removedInfo.args != null) {
                            deleteOld = true;
                        }
                    }
                    ......
                    if (args.observer != null) {
                        try {
                            args.observer.packageInstalled(res.name, res.returnCode);
                        } catch (RemoteException e) {
                            Slog.i(TAG, "Observer no longer exists.");
                        }
                    }
                } else {
                    Slog.e(TAG, "Bogus post-install token " + msg.arg1);
                }
                break;
            }
        }
    }

7.InstallParams的startCopy方法里,會調用handleStartCopy方法

代碼位置:frameworks/base/services/java/com/android/server/pm/PackageManagerService.java ->內部類:InstallParams 繼承于HandlerParams
    final boolean startCopy() {
        boolean res;
        try {
            if (++mRetries > MAX_RETRIES) {
                Slog.w(TAG, "Failed to invoke remote methods on default container service. Giving up");
                mHandler.sendEmptyMessage(MCS_GIVE_UP);
                handleServiceError();
                return false;
            } else {
                handleStartCopy();
                res = true;
            }
        } catch (RemoteException e) {
            mHandler.sendEmptyMessage(MCS_RECONNECT);
            res = false;
        }
        handleReturnCode();
        return res;
    }

8.handleStartCopy方法中會檢查應用是否能安裝,如不合法則返回FAILED的CODE,接著會調用DefaultContainerService的getMinimalPackageInfo方法,該方法用于獲取存儲狀態,返回合適的安裝位置
如果返回碼是INSTALL_SUCCEEDED,那接下來就會調用InstallParams的copyApk,如果安裝到內置,調用的就是FileInstallArgs的copyApk方法,如安裝到外置就調用AsecInstallArgs的copyApk方法
AsecInstallArgs和FileInstallArgs都是InstallParams的子類

代碼位置:frameworks/base/services/java/com/android/server/pm/PackageManagerService.java ->內部類:FileInstallArgs 繼承于InstallParams
public void handleStartCopy() throws RemoteException {
        ......
        if (onInt && onSd) {
            ret = PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION;
        } else {
            ......
            try {
                mContext.grantUriPermission(DEFAULT_CONTAINER_PACKAGE, mPackageURI,
                        Intent.FLAG_GRANT_READ_URI_PERMISSION);
                ........
                if (packageFile != null) {
                    ......
                    pkgLite = mContainerService.getMinimalPackageInfo(packageFilePath, flags, lowThreshold);
                }
            }
        }
        final InstallArgs args = createInstallArgs(this);
        mArgs = args;
        ......
        if (ret == PackageManager.INSTALL_SUCCEEDED) {
            ......
                ret = args.copyApk(mContainerService, true);
            ......
        }
        mRet = ret;
    }

9.copyApk方法中會依次調用FileInstallArgs 的createCopyFile->PackageManagerService的createTempPackageFile方法去創建臨時文件。

代碼位置:frameworks/base/services/java/com/android/server/pm/PackageManagerService.java ->內部類:FileInstallArgs 繼承于InstallParams
          frameworks/base/services/java/com/android/server/pm/PackageManagerService.java
vmdl*.tmp就是copy成的臨時文件
    void createCopyFile() {
        installDir = isFwdLocked() ? mDrmAppPrivateInstallDir : mAppInstallDir;
        codeFileName = createTempPackageFile(installDir).getPath();
        resourceFileName = getResourcePathFromCodePath();
        libraryPath = getLibraryPathFromCodePath();
        created = true;
    }
    private File createTempPackageFile(File installDir) {
        File tmpPackageFile;
        try {
            tmpPackageFile = File.createTempFile("vmdl", ".tmp", installDir);
        } catch (IOException e) {
            Slog.e(TAG, "Couldn't create temp file for downloaded package file.");
            return null;
        }
        try {
            FileUtils.setPermissions(
                    tmpPackageFile.getCanonicalPath(), FileUtils.S_IRUSR|FileUtils.S_IWUSR,
                    -1, -1);
            if (!SELinux.restorecon(tmpPackageFile)) {
                return null;
            }
        } catch (IOException e) {
            Slog.e(TAG, "Trouble getting the canoncical path for a temp file.");
            return null;
        }
        return tmpPackageFile;
    }

10.臨時文件已經有了,handleStartCopy方法走完,接著回到步驟7,調用InstallParams的handleReturnCode方法,handleReturnCode中會執行processPendingInstall,在該方法中做了大量工作

    @Override
    void handleReturnCode() {
        if (mArgs != null) {
            processPendingInstall(mArgs, mRet);
            if (mTempPackage != null) {
                if (!mTempPackage.delete()) {
                    Slog.w(TAG, "Couldn't delete temporary file: " +
                            mTempPackage.getAbsolutePath());
                }
            }
        }
    }

11.來看看processPendingInstall到底做了什么?processPendingInstall中最關鍵方法--installPackageLI,主要的操作(驗證簽名,創建/data/data,分配UID,dexopt)都在這個方法中完成。

    private void processPendingInstall(final InstallArgs args, final int currentStatus) {
        mHandler.post(new Runnable() {
            public void run() {
                mHandler.removeCallbacks(this);
                PackageInstalledInfo res = new PackageInstalledInfo();
                res.returnCode = currentStatus;
                res.uid = -1;
                res.pkg = null;
                res.removedInfo = new PackageRemovedInfo();
                if (res.returnCode == PackageManager.INSTALL_SUCCEEDED) {
                    args.doPreInstall(res.returnCode);
                    synchronized (mInstallLock) {
                        installPackageLI(args, true, res);
                    }
                    args.doPostInstall(res.returnCode, res.uid);
                }
                ......
                if (!doRestore) {
                    Message msg = mHandler.obtainMessage(POST_INSTALL, token, 0);
                    mHandler.sendMessage(msg);
                }
            }
        });
    }

12.我們來看一下installPackageLI方法,首選會讓parsePackage去解析apk里的AndroidManifest.xml,使用的是parsePackage方法,把解析出來的內容放到Package對象中
接著調用doRename去將之前的tmp文件重命名為apk。apk已經在/data/app下了,apk的屬性也被解析出來放在內存(Package對象)中了
那么現在還需要做什么呢?apk有了,數據目錄(/data/data)還沒有,所以后面會進行uid賦值,驗證簽名,創建相應的/data/data目錄,dexopt操作,這些工作是由installNewPackageLI來完成

    private void installPackageLI(InstallArgs args,
            boolean newInstall, PackageInstalledInfo res) {
        ......
        int parseFlags = mDefParseFlags | PackageParser.PARSE_CHATTY
                | (forwardLocked ? PackageParser.PARSE_FORWARD_LOCK : 0)
                | (onSd ? PackageParser.PARSE_ON_SDCARD : 0);
        PackageParser pp = new PackageParser(tmpPackageFile.getPath());
        pp.setSeparateProcesses(mSeparateProcesses);
        final PackageParser.Package pkg = pp.parsePackage(tmpPackageFile,
                null, mMetrics, parseFlags);
        ......
        if (!args.doRename(res.returnCode, pkgName, oldCodePath)) {
            res.returnCode = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
            return;
        }
        ......
        if (replace) {
            replacePackageLI(pkg, parseFlags, scanMode, args.user,
                    installerPackageName, res);
        } else {
            installNewPackageLI(pkg, parseFlags, scanMode | SCAN_DELETE_DATA_ON_FAILURES, args.user,
                    installerPackageName, res);
        }
        synchronized (mPackages) {
            final PackageSetting ps = mSettings.mPackages.get(pkgName);
            if (ps != null) {
                res.newUsers = ps.queryInstalledUsers(sUserManager.getUserIds(), true);
            }
        }
    }

13.讓我們來看看installNewPackageLI具體怎么完成這些工作的吧。

private void installNewPackageLI(PackageParser.Package pkg,
            int parseFlags, int scanMode, UserHandle user,
            String installerPackageName, PackageInstalledInfo res) {
        ......
        PackageParser.Package newPackage = scanPackageLI(pkg, parseFlags, scanMode, System.currentTimeMillis(), user);
        if (newPackage == null) {
            ......
        } else {
            updateSettingsLI(newPackage, installerPackageName, null, null, res);
            ......
            if (res.returnCode != PackageManager.INSTALL_SUCCEEDED) {
                deletePackageLI(pkgName, UserHandle.ALL, false, null, null, dataDirExists ? PackageManager.DELETE_KEEP_DATA : 0,
                        res.removedInfo, true);
            }
        }
    }

14.scanPackageLI方法中,先調用getPackageLPw->newUserIdLPw(Settings類方法)去設置uid,在調用verifySignaturesLP驗證簽名
然后調用createDataDirsLI創建/data/data數據目錄,最后調用performDexOptLI進行dexopt操作
createDataDirsLI是靠調用mInstaller.install方法來完成目錄創建,framework中的Installer會和底層幕后Installer勾兌,完成目錄創建工作
performDexOptLI操作最后也是通過mInstaller.dexopt來完成的

    private PackageParser.Package scanPackageLI(PackageParser.Package pkg,
            int parseFlags, int scanMode, long currentTime, UserHandle user) {
        ......
        synchronized (mPackages) {
            ......
            pkgSetting = mSettings.getPackageLPw(pkg, origPackage, realName, suid, destCodeFile,
                    destResourceFile, pkg.applicationInfo.nativeLibraryDir,
                    pkg.applicationInfo.flags, user, false);
            if (pkgSetting == null) {
                mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
                return null;
            }
            ......
            if (!verifySignaturesLP(pkgSetting, pkg)) {
                if ((parseFlags&PackageParser.PARSE_IS_SYSTEM_DIR) == 0) {
                    return null;
                }
                // The signature has changed, but this package is in the system
                // image...  let's recover!
                pkgSetting.signatures.mSignatures = pkg.mSignatures;
                // However...  if this package is part of a shared user, but it
                // doesn't match the signature of the shared user, let's fail.
                // What this means is that you can't change the signatures
                // associated with an overall shared user, which doesn't seem all
                // that unreasonable.
                if (pkgSetting.sharedUser != null) {
                    if (compareSignatures(pkgSetting.sharedUser.signatures.mSignatures,
                            pkg.mSignatures) != PackageManager.SIGNATURE_MATCH) {
                        Log.w(TAG, "Signature mismatch for shared user : " + pkgSetting.sharedUser);
                        mLastScanError = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
                        return null;
                    }
                }
                // File a report about this.
                String msg = "System package " + pkg.packageName
                        + " signature changed; retaining data.";
                reportSettingsProblem(Log.WARN, msg);
            }
            ......
        }
        ......
        if (mPlatformPackage == pkg) {
            ......
        } else {
            ......
            if (dataPath.exists()) {
               ......
            } else {
                ......
                int ret = createDataDirsLI(pkgName, pkg.applicationInfo.uid,
                                           pkg.applicationInfo.seinfo);
                if (ret < 0) {
                    // Error from installer
                    mLastScanError = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
                    return null;
                }
                if (dataPath.exists()) {
                    pkg.applicationInfo.dataDir = dataPath.getPath();
                } else {
                    Slog.w(TAG, "Unable to create data directory: " + dataPath);
                    pkg.applicationInfo.dataDir = null;
                }
            }
            /*
             * Set the data dir to the default "/data/data/<package name>/lib"
             * if we got here without anyone telling us different (e.g., apps
             * stored on SD card have their native libraries stored in the ASEC
             * container with the APK).
             *
             * This happens during an upgrade from a package settings file that
             * doesn't have a native library path attribute at all.
             */
            if (pkg.applicationInfo.nativeLibraryDir == null && pkg.applicationInfo.dataDir != null) {
                if (pkgSetting.nativeLibraryPathString == null) {
                    setInternalAppNativeLibraryPath(pkg, pkgSetting);
                } else {
                    pkg.applicationInfo.nativeLibraryDir = pkgSetting.nativeLibraryPathString;
                }
            }
            pkgSetting.uidError = uidError;
        }
        ......
        // We also need to dexopt any apps that are dependent on this library.  Note that
        // if these fail, we should abort the install since installing the library will
        // result in some apps being broken.
        if (clientLibPkgs != null) {
            if ((scanMode&SCAN_NO_DEX) == 0) {
                for (int i=0; i<clientLibPkgs.size(); i++) {
                    PackageParser.Package clientPkg = clientLibPkgs.get(i);
                    if (performDexOptLI(clientPkg, forceDex, (scanMode&SCAN_DEFER_DEX) != 0, false)
                            == DEX_OPT_FAILED) {
                        if ((scanMode & SCAN_DELETE_DATA_ON_FAILURES) != 0) {
                            removeDataDirsLI(pkg.packageName);
                        }
 
                        mLastScanError = PackageManager.INSTALL_FAILED_DEXOPT;
                        return null;
                    }
                }
            }
        }
        // Request the ActivityManager to kill the process(only for existing packages)
        // so that we do not end up in a confused state while the user is still using the older
        // version of the application while the new one gets installed.
        ......
        // Also need to kill any apps that are dependent on the library.
        ......
        return pkg;
    }
    private int createDataDirsLI(String packageName, int uid, String seinfo) {
        int[] users = sUserManager.getUserIds();
        int res = mInstaller.install(packageName, uid, uid, seinfo);
        if (res < 0) {
            return res;
        }
        for (int user : users) {
            if (user != 0) {
                res = mInstaller.createUserData(packageName,
                        UserHandle.getUid(user, uid), user);
                if (res < 0) {
                    return res;
                }
            }
        }
        return res;
    }
    private int performDexOptLI(PackageParser.Package pkg, boolean forceDex, boolean defer,
            boolean inclDependencies) {
        ......
        return performDexOptLI(pkg, forceDex, defer, done);
    }
    private int performDexOptLI(PackageParser.Package pkg, boolean forceDex, boolean defer,
            HashSet<String> done) {
        ......
        if ((pkg.applicationInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0) {
            ......
            try {
                if (forceDex || dalvik.system.DexFile.isDexOptNeeded(path)) {
                    if (!forceDex && defer) {
                        if (mDeferredDexOpt == null) {
                            mDeferredDexOpt = new HashSet<PackageParser.Package>();
                        }
                        mDeferredDexOpt.add(pkg);
                        return DEX_OPT_DEFERRED;
                    } else {
                        final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid);
                        ret = mInstaller.dexopt(path, sharedGid, !isForwardLocked(pkg));
                        pkg.mDidDexOpt = true;
                        performed = true;
                    }
                }
            } catch (FileNotFoundException e) {
                ......
            } catch (IOException e) {
                ......
            } catch (dalvik.system.StaleDexCacheError e) {
                ......
            } catch (Exception e) {
                ......
            }
            if (ret < 0) {
                //error from installer
                return DEX_OPT_FAILED;
            }
        }
        return performed ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED;
    }
performDexOptLI后生成的文件

15.到目前為止,scanPackageLI已經走完了,接下來就該更新packages.list,packages.xml了
系統中所有app的信息都保存在這兩個文件中,當有app安裝、卸載、更新時都會更新這兩個文件
回到步驟13,當installNewPackageLI中的scanPackageLI走完后,后面會調用updateSettingsLI去更新文件
mSettings.writeLPr()來完成往packages.list,packages.xml中更新數據

    private void updateSettingsLI(PackageParser.Package newPackage, String installerPackageName,
            int[] allUsers, boolean[] perUserInstalled,
            PackageInstalledInfo res) {
        String pkgName = newPackage.packageName;
        synchronized (mPackages) {
            ......
            mSettings.setInstallStatus(pkgName, PackageSettingBase.PKG_INSTALL_INCOMPLETE);
            mSettings.writeLPr();
        }
        ......
        synchronized (mPackages) {
            updatePermissionsLPw(newPackage.packageName, newPackage,
                    UPDATE_PERMISSIONS_REPLACE_PKG | (newPackage.permissions.size() > 0
                            ? UPDATE_PERMISSIONS_ALL : 0));
            ......
            mSettings.writeLPr();
        }
    }

packages.list文件部分截取

packages.xml部分截取,包含包名,安裝時間,簽名,權限,文件安裝路徑等信息

16.installPackageLI到這里已經執行完了,現在回到步驟11,后續會執行到Message msg = mHandler.obtainMessage(POST_INSTALL, token, 0); mHandler.sendMessage(msg);發送消息
消息發出后,回到步驟POST_INSTALL,這里面主要做了兩件事,發生一條ACTION_PACKAGE_ADDED廣播告訴大家,又有新包了,這是launcher什么的趕緊把圖標加上
然后回調args.observer.packageInstalled(res.name, res.returnCode);告訴PackageInstaller安裝結果
然后就顯示安裝完成界面。歐拉,應用安裝結束。

五、總結

小結一下安裝app到底主要做了哪些事情?

1.驗證是否允許安裝未知來源應用
2.得到用戶設置的首選安裝位置
3.檢驗app有效性,檢查存儲狀態,得到最佳安裝位置
4.拷貝app到安裝位置,此時為.tmp臨時文件
5.解析AndroidManifest.xml
6.重命名tmp為apk
7.賦值UID,驗證權限
8.創建/data/data/下數據目錄
9.執行dexopt操作
10.更新packages.xml,packages.list
11.發送廣播,回調安裝狀態

原文鏈接:sgzy001/article/details/44857057

六、參考文獻

【騰訊文檔】Android Framework 知識庫
https://docs.qq.com/doc/DSXBmSG9VbEROUXF5

至此,本篇已結束。轉載網絡的文章,小編覺得很優秀,歡迎點擊閱讀原文,支持原創作者,如有侵權,懇請聯系小編刪除,歡迎您的建議與指正。同時期待您的關注,感謝您的閱讀,謝謝!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,836評論 6 540
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,275評論 3 428
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,904評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,633評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,368評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,736評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,740評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,919評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,481評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,235評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,427評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,968評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,656評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,055評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,348評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,160評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,380評論 2 379

推薦閱讀更多精彩內容