Android源码笔记--APK卸载过程

            Android中应用的卸载主要是通过PackageManager中提供的deletePackage()函数来卸载,该函数通过IPC调用到Pms的deletePackage()函数,继而调用到deletePackageX();

           当在设置中的应用列表中点击一个安装的应用,点击卸载后,会发送一个Intent给UninstallerActivity,在UninstallerActivity最后会启动UninstallAppProgress的initView方法,如下:

UninstallerActivity.java
 
private void startUninstallProgress() {
        Intent newIntent = new Intent(Intent.ACTION_VIEW);
        newIntent.putExtra(PackageUtil.INTENT_ATTR_APPLICATION_INFO, 
                                                  mAppInfo);
        newIntent.putExtra(Intent.EXTRA_UNINSTALL_ALL_USERS, mAllUsers);
        if (getIntent().getBooleanExtra(Intent.EXTRA_RETURN_RESULT, false)) {
            newIntent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
            newIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
        }
        newIntent.setClass(this, UninstallAppProgress.class);
        startActivity(newIntent);
        finish();
    }
UninstallAppProgress.java
 
public void initView() {
  ...
PackageDeleteObserver observer = new PackageDeleteObserver(); getPackageManager().deletePackage(mAppInfo.packageName, observer,
                mAllUsers ? PackageManager.DELETE_ALL_USERS : 0);
}

分析:mAllUsers默认是false。getPackageManager()函数的实现在ContextImpl.java,它最后会调用到ApplicantPackageManger.java的deletePackage方法:

ApplicationPackageManager.java

@Override
    public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
        try {
            mPM.deletePackageAsUser(packageName, observer, UserHandle.myUserId(), flags);
        } catch (RemoteException e) {
            // Should never happen!
        }
    }

通过Binde调用到PMS的

PackageManagerService.java

@Override
    public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer, int userId,
            int flags) {
        deletePackage(packageName, new LegacyPackageDeleteObserver(observer).getBinder(), userId,
                flags);
    }

    @Override
    public void deletePackage(final String packageName,
            final IPackageDeleteObserver2 observer, final int userId, final int flags) {
        mContext.enforceCallingOrSelfPermission(
                android.Manifest.permission.DELETE_PACKAGES, null);
        final int uid = Binder.getCallingUid();
        if (UserHandle.getUserId(uid) != userId) {
            mContext.enforceCallingPermission(
                    android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
                    "deletePackage for user " + userId);
        }
        if (isUserRestricted(userId, UserManager.DISALLOW_UNINSTALL_APPS)) {
            try {
                observer.onPackageDeleted(packageName,
                        PackageManager.DELETE_FAILED_USER_RESTRICTED, null);
            } catch (RemoteException re) {
            }
            return;
        }

        boolean uninstallBlocked = false;
        if ((flags & PackageManager.DELETE_ALL_USERS) != 0) {
            int[] users = sUserManager.getUserIds();
            for (int i = 0; i < users.length; ++i) {
                if (getBlockUninstallForUser(packageName, users[i])) {
                    uninstallBlocked = true;
                    break;
                }
            }
        } else {
            uninstallBlocked = getBlockUninstallForUser(packageName, userId);
        }
        if (uninstallBlocked) {
            try {
                observer.onPackageDeleted(packageName, PackageManager.DELETE_FAILED_OWNER_BLOCKED,
                        null);
            } catch (RemoteException re) {
            }
            return;
        }

        if (DEBUG_REMOVE) {
            Slog.d(TAG, "deletePackageAsUser: pkg=" + packageName + " user=" + userId);
        }
        // Queue up an async operation since the package deletion may take a little while.
        mHandler.post(new Runnable() {
            public void run() {
                mHandler.removeCallbacks(this);
                final int returnCode = deletePackageX(packageName, userId, flags);
                if (observer != null) {
                    try {
                        observer.onPackageDeleted(packageName, returnCode, null);
                    } catch (RemoteException e) {
                        Log.i(TAG, "Observer no longer exists.");
                    } //end catch
                } //end if
            } //end run
        });
    }

在deletePackageAsUser方法中,首先做权限检查,然后就调用deletePackageX方法去执行卸载任务:

PMS

private int deletePackageX(String packageName, int userId, int flags) {
        final PackageRemovedInfo info = new PackageRemovedInfo();
        final boolean res;

        final UserHandle removeForUser = (flags & PackageManager.DELETE_ALL_USERS) != 0
                ? UserHandle.ALL : new UserHandle(userId);

        if (isPackageDeviceAdmin(packageName, removeForUser.getIdentifier())) {
            Slog.w(TAG, "Not removing package " + packageName + ": has active device admin");
            return PackageManager.DELETE_FAILED_DEVICE_POLICY_MANAGER;
        }

        boolean removedForAllUsers = false;
        boolean systemUpdate = false;

        // for the uninstall-updates case and restricted profiles, remember the per-
        // userhandle installed state
        int[] allUsers;
        boolean[] perUserInstalled;
        synchronized (mPackages) {
            PackageSetting ps = mSettings.mPackages.get(packageName);
            allUsers = sUserManager.getUserIds();
            perUserInstalled = new boolean[allUsers.length];
            for (int i = 0; i < allUsers.length; i++) {
                perUserInstalled[i] = ps != null ? ps.getInstalled(allUsers[i]) : false;
            }
        }

        synchronized (mInstallLock) {
            if (DEBUG_REMOVE) Slog.d(TAG, "deletePackageX: pkg=" + packageName + " user=" + userId);
            res = deletePackageLI(packageName, removeForUser,
                    true, allUsers, perUserInstalled,
                    flags | REMOVE_CHATTY, info, true);
            systemUpdate = info.isRemovedPackageSystemUpdate;
            if (res && !systemUpdate && mPackages.get(packageName) == null) {
                removedForAllUsers = true;
            }
            if (DEBUG_REMOVE) Slog.d(TAG, "delete res: systemUpdate=" + systemUpdate
                    + " removedForAllUsers=" + removedForAllUsers);
        }

        if (res) {
            info.sendBroadcast(true, systemUpdate, removedForAllUsers);

            // If the removed package was a system update, the old system package
            // was re-enabled; we need to broadcast this information
            if (systemUpdate) {
                Bundle extras = new Bundle(1);
                extras.putInt(Intent.EXTRA_UID, info.removedAppId >= 0
                        ? info.removedAppId : info.uid);
                extras.putBoolean(Intent.EXTRA_REPLACING, true);

                sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, packageName,
                        extras, null, null, null);
                sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED, packageName,
                        extras, null, null, null);
                sendPackageBroadcast(Intent.ACTION_MY_PACKAGE_REPLACED, null,
                        null, packageName, null, null);
            }
        }
        // Force a gc here.
        Runtime.getRuntime().gc();
        // Delete the resources here after sending the broadcast to let
        // other processes clean up before deleting resources.
        if (info.args != null) {
            synchronized (mInstallLock) {
                info.args.doPostDeleteLI(true);
            }
        }

        return res ? PackageManager.DELETE_SUCCEEDED : PackageManager.DELETE_FAILED_INTERNAL_ERROR;
    }
 /*
     * This method handles package deletion in general
     */
    private boolean deletePackageLI(String packageName, UserHandle user,
            boolean deleteCodeAndResources, int[] allUserHandles, boolean[] perUserInstalled,
            int flags, PackageRemovedInfo outInfo,
            boolean writeSettings) {
        if (packageName == null) {
            Slog.w(TAG, "Attempt to delete null packageName.");
            return false;
        }
       
        PackageSetting ps;
        boolean dataOnly = false;
        int removeUser = -1;
        int appId = -1;
        synchronized (mPackages) {
            ps = mSettings.mPackages.get(packageName);
            if (ps == null) {
                Slog.w(TAG, "Package named '" + packageName + "' doesn't exist.");
                return false;
            }
            if ((!isSystemApp(ps) || (flags&PackageManager.DELETE_SYSTEM_APP) != 0) && user != null
                    && user.getIdentifier() != UserHandle.USER_ALL) {
                // The caller is asking that the package only be deleted for a single
                // user.  To do this, we just mark its uninstalled state and delete
                // its data.  If this is a system app, we only allow this to happen if
                // they have set the special DELETE_SYSTEM_APP which requests different
                // semantics than normal for uninstalling system apps.
                if (DEBUG_REMOVE) Slog.d(TAG, "Only deleting for single user");
                ps.setUserState(user.getIdentifier(),
                        COMPONENT_ENABLED_STATE_DEFAULT,
                        false, //installed
                        true,  //stopped
                        true,  //notLaunched
                        false, //hidden
                        null, null, null,
                        false // blockUninstall
                        );
                if (!isSystemApp(ps)) {
                    if (ps.isAnyInstalled(sUserManager.getUserIds())) {
                        // Other user still have this package installed, so all
                        // we need to do is clear this user's data and save that
                        // it is uninstalled.
                        if (DEBUG_REMOVE) Slog.d(TAG, "Still installed by other users");
                        removeUser = user.getIdentifier();
                        appId = ps.appId;
                        mSettings.writePackageRestrictionsLPr(removeUser);
                    } else {
                        // We need to set it back to 'installed' so the uninstall
                        // broadcasts will be sent correctly.
                        if (DEBUG_REMOVE) Slog.d(TAG, "Not installed by other users, full delete");
                        ps.setInstalled(true, user.getIdentifier());
                    }
                } else {
                    // This is a system app, so we assume that the
                    // other users still have this package installed, so all
                    // we need to do is clear this user's data and save that
                    // it is uninstalled.
                    if (DEBUG_REMOVE) Slog.d(TAG, "Deleting system app");
                    removeUser = user.getIdentifier();
                    appId = ps.appId;
                    mSettings.writePackageRestrictionsLPr(removeUser);
                }
            }
        }

        if (removeUser >= 0) {
            // From above, we determined that we are deleting this only
            // for a single user.  Continue the work here.
            if (DEBUG_REMOVE) Slog.d(TAG, "Updating install state for user: " + removeUser);
            if (outInfo != null) {
                outInfo.removedPackage = packageName;
                outInfo.removedAppId = appId;
                outInfo.removedUsers = new int[] {removeUser};
            }
            mInstaller.clearUserData(packageName, removeUser);
            removeKeystoreDataIfNeeded(removeUser, appId);
            schedulePackageCleaning(packageName, removeUser, false);
            return true;
        }

       ...

        boolean ret = false;
        if (isSystemApp(ps)) {
            if (DEBUG_REMOVE) Slog.d(TAG, "Removing system package:" + ps.name);
            // When an updated system application is deleted we delete the existing resources as well and
            // fall back to existing code in system partition
            ret = deleteSystemPackageLI(ps, allUserHandles, perUserInstalled,
                    flags, outInfo, writeSettings);
        } else {
            if (DEBUG_REMOVE) Slog.d(TAG, "Removing non-system package:" + ps.name);
            // Kill application pre-emptively especially for apps on sd.
            killApplication(packageName, ps.appId, "uninstall pkg");
            ret = deleteInstalledPackageLI(ps, deleteCodeAndResources, flags,
                    allUserHandles, perUserInstalled,
                    outInfo, writeSettings);
        }

        return ret;
    }

在deletePackageLI函数中根据是否是systemApp调用不同的流程,如果是systemApp,则调用deleteSystemPackageLI完成卸载;如果非systemApp,则调用deleteInstalledPackageLI完成卸载; 在卸载之前,首先会调用AMS的killApplication方法先让这个APP停止运行。

主要看一下非系统App的卸载流程:

private boolean deleteInstalledPackageLI(PackageSetting ps,  
        boolean deleteCodeAndResources, int flags,  
        int[] allUserHandles, boolean[] perUserInstalled,  
        PackageRemovedInfo outInfo, boolean writeSettings) {  
    if (outInfo != null) {  
        outInfo.uid = ps.appId;  
    }  
  
    removePackageDataLI(ps, allUserHandles, perUserInstalled, outInfo, flags, writeSettings);  
  
    if (deleteCodeAndResources && (outInfo != null)) {  
        outInfo.args = createInstallArgs(packageFlagsToInstallFlags(ps), ps.codePathString,  
                ps.resourcePathString, ps.nativeLibraryPathString);  
    }  
    return true;  
}  

         在deleteInstalledPackageLI方法中,分为两步去卸载应用:第一步删除/data/data下面的数据目录,并从PMS的内部数据结构上清除当前卸载的package信息;第二步就删除code和resource文件。我们先来看第一步:

private void removePackageDataLI(PackageSetting ps,  
        int[] allUserHandles, boolean[] perUserInstalled,  
        PackageRemovedInfo outInfo, int flags, boolean writeSettings) {  
    String packageName = ps.name;  
    removePackageLI(ps, (flags&REMOVE_CHATTY) != 0);  
    final PackageSetting deletedPs;  
  
    synchronized (mPackages) {  
        deletedPs = mSettings.mPackages.get(packageName);  
        if (outInfo != null) {  
            outInfo.removedPackage = packageName;  
            outInfo.removedUsers = deletedPs != null  
                    ? deletedPs.queryInstalledUsers(sUserManager.getUserIds(), true)  
                    : null;  
        }  
    }  
    if ((flags&PackageManager.DELETE_KEEP_DATA) == 0) {  
        removeDataDirsLI(packageName);  
        schedulePackageCleaning(packageName, UserHandle.USER_ALL, true);  
    }

           removePackageDataLI用于删除应用的/data/data数据目录,并且从PMS内部数据结构里面清除package的信息。首先调用removePackageLI从PMS内部的数据结构上删除要卸载的package信息:

void removePackageLI(PackageSetting ps, boolean chatty) {  
    synchronized (mPackages) {  
        mPackages.remove(ps.name);  
        if (ps.codePathString != null) {  
            mAppDirs.remove(ps.codePathString);  
        }  
  
        final PackageParser.Package pkg = ps.pkg;  
        if (pkg != null) {  
            cleanPackageDataStructuresLILPw(pkg, chatty);  
        }  
    }  
}  

           cleanPackageDataStructuresLILPw用于将package的providers、services、receivers、activities等信息去PMS的全局数据结构上移除,这部分代码比较简单。如果没有设置DELETE_KEEP_DATA这个flag,就会首先调用removeDataDirsLI去删除/data/data下面的目录:

private int removeDataDirsLI(String packageName) {  
    int[] users = sUserManager.getUserIds();  
    int res = 0;  
    for (int user : users) {  
        int resInner = mInstaller.remove(packageName, user);  
        if (resInner < 0) {  
            res = resInner;  
        }  
    }  
  
    final File nativeLibraryFile = new File(mAppLibInstallDir, packageName);  
    NativeLibraryHelper.removeNativeBinariesFromDirLI(nativeLibraryFile);  
    if (!nativeLibraryFile.delete()) {  
        Slog.w(TAG, "Couldn't delete native library directory " + nativeLibraryFile.getPath());  
    }  
  
    return res;  
}  

         这里首先调用installd的remove方法去删除/data/data下面的目录。然后去删除/data/app-lib下面的应用程序的library信息,但因为这里的nativeLibraryFile为/data/app-lib/packageName,和前面介绍的APK安装过程中的目录/data/app-lib/packageName-num不一样,所以实际上,这里并没有真正的去删除library目录。
先来看installd的remove方法:

static int do_remove(char **arg, char reply[REPLY_MAX])  
{  
    return uninstall(arg[0], atoi(arg[1])); /* pkgname, userid */  
}  
  
int uninstall(const char *pkgname, userid_t userid)  
{  
    char pkgdir[PKG_PATH_MAX];  
  
    if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid))  
        return -1;  
  
    return delete_dir_contents(pkgdir, 1, NULL);  
}  
  
int delete_dir_contents(const char *pathname,  
                        int also_delete_dir,  
                        const char *ignore)  
{  
    int res = 0;  
    DIR *d;  
  
    d = opendir(pathname);  
    if (d == NULL) {  
        ALOGE("Couldn't opendir %s: %s\n", pathname, strerror(errno));  
        return -errno;  
    }  
    res = _delete_dir_contents(d, ignore);  
    closedir(d);  
    if (also_delete_dir) {  
        if (rmdir(pathname)) {  
            ALOGE("Couldn't rmdir %s: %s\n", pathname, strerror(errno));  
            res = -1;  
        }  
    }  
    return res;  
}  

         create_pkg_path方法构造/data/data/packageName的文件路径名,然后调用delete_dir_contents来删除文件内容以及目录,前面介绍过,/data/data/packageName的文件其实都是符号链接,所以_delete_dir_contents的实现中都是调用unlinkat去删除这些符号链接。回到removePackageDataLI中,接着调用schedulePackageCleaning来安排清理动作:

void schedulePackageCleaning(String packageName, int userId, boolean andCode) {  
    mHandler.sendMessage(mHandler.obtainMessage(START_CLEANING_PACKAGE,  
            userId, andCode ? 1 : 0, packageName));  
}  

          这里向PackageHandler发送START_CLEANING_PACKAGE消息,PMS会调用ContainService的函数去删除/storage/sdcard0/Android/data和/storage/sdcard0/Android/media下面与package相关的文件,接着来看removePackageDataLI方法:

synchronized (mPackages) {  
    if (deletedPs != null) {  
        if ((flags&PackageManager.DELETE_KEEP_DATA) == 0) {  
            if (outInfo != null) {  
                outInfo.removedAppId = mSettings.removePackageLPw(packageName);  
            }  
            if (deletedPs != null) {  
                updatePermissionsLPw(deletedPs.name, null, 0);  
                if (deletedPs.sharedUser != null) {  
                    // remove permissions associated with package  
                    mSettings.updateSharedUserPermsLPw(deletedPs, mGlobalGids);  
                }  
            }  
            clearPackagePreferredActivitiesLPw(deletedPs.name, UserHandle.USER_ALL);  
        }  
    }  
  
    if (writeSettings) {  
        mSettings.writeLPr();  
    }  
}  
if (outInfo != null) {  
    removeKeystoreDataIfNeeded(UserHandle.USER_ALL, outInfo.removedAppId);  
}  

这里首先从Settings中删除PackageSettings的信息:

int removePackageLPw(String name) {  
    final PackageSetting p = mPackages.get(name);  
    if (p != null) {  
        mPackages.remove(name);  
        if (p.sharedUser != null) {  
            p.sharedUser.removePackage(p);  
            if (p.sharedUser.packages.size() == 0) {  
                mSharedUsers.remove(p.sharedUser.name);  
                removeUserIdLPw(p.sharedUser.userId);  
                return p.sharedUser.userId;  
            }  
        } else {  
            removeUserIdLPw(p.appId);  
            return p.appId;  
        }  
    }  
    return -1;  
}  
void updateSharedUserPermsLPw(PackageSetting deletedPs, int[] globalGids) {  
    SharedUserSetting sus = deletedPs.sharedUser;  
  
    for (String eachPerm : deletedPs.pkg.requestedPermissions) {  
        boolean used = false;  
        if (!sus.grantedPermissions.contains(eachPerm)) {  
            continue;  
        }  
        for (PackageSetting pkg:sus.packages) {  
            if (pkg.pkg != null &&  
                    !pkg.pkg.packageName.equals(deletedPs.pkg.packageName) &&  
                    pkg.pkg.requestedPermissions.contains(eachPerm)) {  
                used = true;  
                break;  
            }  
        }  
        if (!used) {  
            sus.grantedPermissions.remove(eachPerm);  
        }  
    }  
    int newGids[] = globalGids;  
    for (String eachPerm : sus.grantedPermissions) {  
        BasePermission bp = mPermissions.get(eachPerm);  
        if (bp != null) {  
            newGids = PackageManagerService.appendInts(newGids, bp.gids);  
        }  
    }  
    sus.gids = newGids;  
}  

            循环的从要被卸载的Package所在的sharedUser组中找被申请的权限是否还被同一组的其它package使用,如果没有使用者,就从sharedUser的grantedPermissions删除。clearPackagePreferredActivitiesLPw与AMS相关,我们留到以后再来介绍。在removePackageDataLI方法最好调用Settings.writeLPr()方法将改动的信息写到Package.xml中。到这里,我们前面所说的deleteInstalledPackageLI方法中的第一步已经完成,来看第二部分:

if (deleteCodeAndResources && (outInfo != null)) {  
        outInfo.args = createInstallArgs(packageFlagsToInstallFlags(ps), ps.codePathString,  
                ps.resourcePathString, ps.nativeLibraryPathString);  
    }  
  
private InstallArgs createInstallArgs(int flags, String fullCodePath, String fullResourcePath,  
        String nativeLibraryPath) {  
    final boolean isInAsec;  
    if (installOnSd(flags)) {  
        isInAsec = true;  
    } else if (installForwardLocked(flags)  
            && !fullCodePath.startsWith(mDrmAppPrivateInstallDir.getAbsolutePath())) {  
        isInAsec = true;  
    } else {  
        isInAsec = false;  
    }  
  
    if (isInAsec) {  
        return new AsecInstallArgs(fullCodePath, fullResourcePath, nativeLibraryPath,  
                installOnSd(flags), installForwardLocked(flags));  
    } else {  
        return new FileInstallArgs(fullCodePath, fullResourcePath, nativeLibraryPath);  
    }  
}  

        这里根据安装目录的不同,分别构造FileInstallArgs和AsecInstallArgs来完成code和resource资源的清除。这里我们主要介绍卸载内部存储空间上面的APK,来看FileInstallArgs的doPostDeleteLI方法:

boolean doPostDeleteLI(boolean delete) {  
    cleanUpResourcesLI();  
    return true;  
}  
  
void cleanUpResourcesLI() {  
    String sourceDir = getCodePath();  
    if (cleanUp()) {  
        int retCode = mInstaller.rmdex(sourceDir);  
        if (retCode < 0) {  
            Slog.w(TAG, "Couldn't remove dex file for package: "  
                    +  " at location "  
                    + sourceDir + ", retcode=" + retCode);  
            // we don't consider this to be a failure of the core package deletion  
        }  
    }  
}  
private boolean cleanUp() {  
    boolean ret = true;  
    String sourceDir = getCodePath();  
    String publicSourceDir = getResourcePath();  
    if (sourceDir != null) {  
        File sourceFile = new File(sourceDir);  
        if (!sourceFile.exists()) {  
            Slog.w(TAG, "Package source " + sourceDir + " does not exist.");  
            ret = false;  
        }  
  
        sourceFile.delete();  
    }  
    if (publicSourceDir != null && !publicSourceDir.equals(sourceDir)) {  
        final File publicSourceFile = new File(publicSourceDir);  
        if (!publicSourceFile.exists()) {  
            Slog.w(TAG, "Package public source " + publicSourceFile + " does not exist.");  
        }  
        if (publicSourceFile.exists()) {  
            publicSourceFile.delete();  
        }  
    }  
  
    if (libraryPath != null) {  
        File nativeLibraryFile = new File(libraryPath);  
        NativeLibraryHelper.removeNativeBinariesFromDirLI(nativeLibraryFile);  
        if (!nativeLibraryFile.delete()) {  
            Slog.w(TAG, "Couldn't delete native library directory " + libraryPath);  
        }  
    }  
  
    return ret;  
}  

然后cleanUpResourcesLI调用installd的rmdex方法去删除存在/data/dalvik-cache文件:

static int do_rm_dex(char **arg, char reply[REPLY_MAX])  
{  
    return rm_dex(arg[0]); /* pkgname */  
}  
  
int rm_dex(const char *path)  
{  
    char dex_path[PKG_PATH_MAX];  
  
    if (validate_apk_path(path)) return -1;  
    if (create_cache_path(dex_path, path)) return -1;  
  
    ALOGV("unlink %s\n", dex_path);  
    if (unlink(dex_path) < 0) {  
        ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));  
        return -1;  
    } else {  
        return 0;  
    }  
}  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值