首页 > 学院 > 开发设计 > 正文

apk的安装分析

2019-11-09 16:25:42
字体:
来源:转载
供稿:网友

一、概述

        APK为AndroidPackage的缩写,Android应用安装有如下四种方式:

(1)系统应用安装――开机时完成,没有安装界面

(2)网络下载应用安装――通过market应用完成,没有安装界面

(3)ADB工具安装――没有安装界面

(4)第三方应用安装――通过SD卡里的APK文件安装,有安装界面,由 packageinstaller.apk 应用处理安装及卸载。

       应用安装涉及到如下几个目录: 

(1)system/app:系统自带的应用程序,获得adb root权限才能删除

(2)data/app:用户程序安装的目录。用户 安装时把apk文件 复制 到此目录

(3)data/data:存放应用程序运行产生的数据

(4)data/dalvik-cache:将apk中的dex文件安装到dalvik-cache目录下(dex文件是dalvik虚拟机的可执行文件,其大小约为原始apk文件大小的四分之一)

       安装过程:

(1)复制APK安装包到data/app目录下,解压并扫描安装包,

(2)将dex文件(Dalvik字节码)保存到dalvik-cache目录,

(3)在data/data目录下创建对应的应用数据目录。

       卸载过程:

删除 安装过程中在上述三个目录下创建的文件及目录。

1、开机安装 

          PackageManagerService处理各种应用的安装,卸载,管理等工作,开机时由systemServer启动此服务

(1)首先扫描安装“system/framework”目录下的jar包

scanDirLI(mFrameworkDir, PackageParser.PARSE_IS_SYSTEM, scanMode | SCAN_NO_DEX);

(2)扫描安装“system/app”目录下的各个系统应用

scanDirLI(mSystemAppDir,PackageParser.PARSE_IS_SYSTEM, scanMode);

扫描“data/app”目录,即用户安装的第三方应用

scanDirLI(mAppInstallDir, 0, scanMode);

扫描" data/app-PRivate"目录,即安装DRM保护的APK文件(目前没有遇到过此类的应用)

scanDirLI(mDrmAppPrivateInstallDir,0, scanMode | SCAN_FORWARD_LOCKED)

(3)安装应用的过程

scanDirLI(Filedir, int flags, int scanMode) 遍历安装指定目录下的文件

scanPackageLI(FilescanFile,File destCodeFile, FiledestResourceFile, int parseFlags,int scanMode) 安装package文件

scanPackageLI(File scanFile, File destCodeFile, FiledestResourceFile,PackageParser.Package pkg, intparseFlags, int scanMode)通过解析安装包parsePackage获取到安装包的信息结构

mInstaller.install(pkgName,pkg.applicationInfo.uid,pkg.applicationInfo.uid); 实现文件复制的安装过程

2、从market上下载应用

  Google Market应用需要使用Gmail账户登录才可以使用,选择某一应用后,开始下载安装包,此过程中,在手机的信号区有进度条提示,下载完成后,会自动调用PackageManager#installPackage接口

3、从ADB工具安装

Android Debug Bridge (adb) 是SDK自带的管理设备的工具,通过ADB命令行的方式也可以为手机或模拟器安装应用,其入口函数源文件为pm.java 

源文件路径:android/frameworks/base/cmds/pm/src/com/android/commands/pm/pm.java

ADB命令行的形式为adb install <path_to_apk> ,还可以带安装参数如:"-l""-r" "-i" "-t"

函数runInstall()中判断参数:"-l"――INSTALL_FORWARD_LOCK"-r"——INSTALL_REPLACE_EXISTING "-i" ——installerPackageName"-t"——INSTALL_ALLOW_TEST

我们常用的参数为-r,表示覆盖安装手机上已安装的同名应用。从market上下载的应用,也是直接传入这个参数安装的。

runInstall与market调用同样的接口完成应用安装。

public void installPackage(android.net.Uri packageURI,android.content.pm.ipackageInstallObserver observer, int flags,java.lang.String installerPackageName)

4、第三方应用安装――通过SD卡里的APK文件安装

把APK安装包保存在SD卡中,从手机里访问SD卡中的APK安装包,点击就可以启动安装界面,系统应用Packageinstaller.apk处理这种方式下的安装及卸载界面流程。

首先PackageInstallerActivity负责解析包,判断是否是可用的Apk文件,然后,创建临时安装文件/data/data/com.android.packageinstaller/files/ApiDemos.apk,并启动安装确认界面startInstallConfirm,列出解析得到的该应用基本信息。如果手机上已安装有同名应用,则需要用户确认是否要替换安装。确认安装后,启动InstallAppProgress,调用安装接口完成安装。

pm.installPackage(mPackageURI,observer, installFlags);

【PS】

(1)PackageManagerService.java的内部类AppDirObserver实现了监听app目录的功能:当把某个APK拖到app目录下时,可以直接调用scanPackageLI完成安装。

(2)手机数据区目录“data/system/packages.xml”文件中,包含了手机上所有已安装应用的基本信息,如安装路径,申请的permission等信息。

二、PMS#installPackage

installPackage方法中主要调用了installPackageAsUser,其源码如下:

    public void installPackage(String originPath, IPackageInstallObserver2 observer,            int installFlags, String installerPackageName, VerificationParams verificationParams,            String packageAbiOverride) {        installPackageAsUser(originPath, observer, installFlags, installerPackageName, verificationParams,                packageAbiOverride, UserHandle.getCallingUserId());    } 再看installPackageAsUser,

public void installPackageAsUser(String originPath, IPackageInstallObserver2 observer,            int installFlags, String installerPackageName, VerificationParams verificationParams,            String packageAbiOverride, int userId) {        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.INSTALL_PACKAGES, null);        final int callingUid = Binder.getCallingUid();        enforceCrossUserPermission(callingUid, userId, true, true, "installPackageAsUser");        if (isUserRestricted(userId, UserManager.DISALLOW_INSTALL_APPS)) {            try {                if (observer != null) {                    observer.onPackageInstalled("", INSTALL_FAILED_USER_RESTRICTED, null, null);                }            } catch (RemoteException re) {            }            return;        }        if ((callingUid == Process.SHELL_UID) || (callingUid == Process.ROOT_UID)) {            installFlags |= PackageManager.INSTALL_FROM_ADB;        } else {            // Caller holds INSTALL_PACKAGES permission, so we're less strict            // about installerPackageName.            installFlags &= ~PackageManager.INSTALL_FROM_ADB;            installFlags &= ~PackageManager.INSTALL_ALL_USERS;        }        UserHandle user;        if ((installFlags & PackageManager.INSTALL_ALL_USERS) != 0) {            user = UserHandle.ALL;        } else {            user = new UserHandle(userId);        }        // Only system components can circumvent runtime permissions when installing.        if ((installFlags & PackageManager.INSTALL_GRANT_RUNTIME_PERMISSIONS) != 0                && mContext.checkCallingOrSelfPermission(Manifest.permission                .INSTALL_GRANT_RUNTIME_PERMISSIONS) == PackageManager.PERMISSION_DENIED) {            throw new SecurityException("You need the "                    + "android.permission.INSTALL_GRANT_RUNTIME_PERMISSIONS permission "                    + "to use the PackageManager.INSTALL_GRANT_RUNTIME_PERMISSIONS flag");        }        verificationParams.setInstallerUid(callingUid);        final File originFile = new File(originPath);        final OriginInfo origin = OriginInfo.fromUntrustedFile(originFile);        // 直接交给mHandler来处理了,这个mHandler在PackageManagerService构造函数中初始化,是一个PackageHandler实例        final Message msg = mHandler.obtainMessage(INIT_COPY);        msg.obj = new InstallParams(origin, null, observer, installFlags, installerPackageName,                null, verificationParams, user, packageAbiOverride, null);        mHandler.sendMessage(msg);    } 再看PackageHandler类

    class PackageHandler extends Handler {        PackageHandler(Looper looper) {            super(looper);        }        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;                    int idx = mPendingInstalls.size();                    if (DEBUG_INSTALL) Slog.i(TAG, "init_copy idx=" + idx + ": " + params);                    // If a bind was already initiated we dont really                    // need to do anything. The pending install                    // will be processed later on.                    if (!mBound) {                        // If this is the only one pending we might                        // have to bind to the service again.                        if (!connectToService()) {                            Slog.e(TAG, "Failed to bind to media container service");                            params.serviceError();                            return;                        } else {                            // Once we bind to the service, the first                            // pending request will be processed.                            mPendingInstalls.add(idx, params);                        }                    } else {                        mPendingInstalls.add(idx, params);                        // Already bound to the service. Just make                        // sure we trigger off processing the first request.                        if (idx == 0) {                            mHandler.sendEmptyMessage(MCS_BOUND);                        }                    }                    break;                }                case MCS_BOUND: {                    if (DEBUG_INSTALL) Slog.i(TAG, "mcs_bound");                    if (msg.obj != null) {                        mContainerService = (IMediaContainerService) msg.obj;                    }                    if (mContainerService == null) {                        // Something seriously wrong. Bail out                        Slog.e(TAG, "Cannot bind to media container service");                        for (HandlerParams params : mPendingInstalls) {                            // Indicate service bind error                            params.serviceError();                        }                        mPendingInstalls.clear();                    } else if (mPendingInstalls.size() > 0) {                        HandlerParams params = mPendingInstalls.get(0);//获取第一个任务                        if (params != null) {                            if (params.startCopy()) {//开始安装                                // We are done...  look for more work or to                                // go idle.                                if (DEBUG_SD_INSTALL) Log.i(TAG,                                        "Checking for more work or unbind...");                                // Delete pending install                                if (mPendingInstalls.size() > 0) {                                    mPendingInstalls.remove(0);                                }                                if (mPendingInstalls.size() == 0) {                                    if (mBound) {                                        if (DEBUG_SD_INSTALL) Log.i(TAG,                                                "Posting delayed MCS_UNBIND");                                        removeMessages(MCS_UNBIND);                                        Message ubmsg = obtainMessage(MCS_UNBIND);                                        // Unbind after a little delay, to avoid                                        // continual thrashing.                                        sendMessageDelayed(ubmsg, 10000);                                    }                                } else {                                    // There are more pending requests in queue.                                    // Just post MCS_BOUND message to trigger processing                                    // of next pending install.                                    if (DEBUG_SD_INSTALL) Log.i(TAG,                                            "Posting MCS_BOUND for next work");                                    mHandler.sendEmptyMessage(MCS_BOUND);                                }                            }                        }                    } else {                        // Should never happen ideally.                        Slog.w(TAG, "Empty queue");                    }                    break;                }                ...         }     }...} 首先,sendMessage发出的消息由PackageHandler.handleMessage()捕获,而具体的执行由PackageHandler.doHandleMessage()完成,接着,调用抽象类HandlerParams中的一个startCopy()方法,进而调用scanPackageLI()。


上一篇:Unity问题集合

下一篇:修改Linux IO调度器

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表