Android 7.0: Force stop application 3 times during package OTA.

这篇博客展示了在Android 7.0中,当进行包更新(OTA)时如何处理`Intent.ACTION_PACKAGE_REMOVED`事件。在`ActivityManagerService.java`中,系统通过`forceStopPackageLocked()`方法强制停止应用,例如com.exmaple.ota,以完成更新过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

An example of package OTA:

Logs:

D/DefContainer(15910): Copying /data/local/tmp/com.exmaple.ota.apk to base.apk

W/PackageManager(  653): Centralized permission: Package com.exmaple.ota is declaring a permission com.exmaple.ota.INTERNAL_PERMISSION which is notdeclared in the centralized apk

D/PackageManager(  653): Renaming /data/app/vmdl1120247996.tmp to /data/app/com.exmaple.ota-2


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

11062    private void replaceSystemPackageLI(PackageParser.Package deletedPackage,
11063            PackageParser.Package pkg, int parseFlags, int scanFlags, UserHandle user,
11064            int[] allUsers, boolean[] perUserInstalled,
11065            String installerPackageName, PackageInstalledInfo res) {
.................................
11094        killApplication(packageName, oldPkg.applicationInfo.uid, "replace sys pkg");
11095
11098        // Remove existing system package
11099        removePackageLI(oldPkgSetting, true);
11100        // writer
11101        synchronized (mPackages) {
11102            disabledSystem = mSettings.disableSystemPackageLPw(packageName);
.......................................
11121        }
11122
11123        // Successfully disabled the old package. Now proceed with re-installation
11124        deleteCodeCacheDirsLI(packageName);
11125
11126        res.returnCode = PackageManager.INSTALL_SUCCEEDED;
11127        pkg.applicationInfo.flags |= ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
11128
11129        PackageParser.Package newPackage = null;
11130        try {
11131            newPackage = scanPackageLI(pkg, parseFlags, scanFlags, 0, user);
11132            if (newPackage.mExtras != null) {
11133                final PackageSetting newPkgSetting = (PackageSetting) newPackage.mExtras;
11134                newPkgSetting.firstInstallTime = oldPkgSetting.firstInstallTime;
11135                newPkgSetting.lastUpdateTime = System.currentTimeMillis();
11136
11137                // is the update attempting to change shared user? that isn't going to work...
11138                if (oldPkgSetting.sharedUser != newPkgSetting.sharedUser) {
11139                    res.setError(INSTALL_FAILED_SHARED_USER_INCOMPATIBLE,
11140                            "Forbidding shared user change from " + oldPkgSetting.sharedUser
11141                            + " to " + newPkgSetting.sharedUser);
11142                    updatedSettings = true;
11143                }
11144            }
11205    }

7122    private void killApplication(String pkgName, int appId, String reason) {
7126        IActivityManager am = ActivityManagerNative.getDefault();
7127        if (am != null) {
7128            try {
7129                am.killApplicationWithAppId(pkgName, appId, reason);
7130            } catch (RemoteException e) {
7131            }
7132        }
7133    }

In frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

5914    public void killApplicationWithAppId(String pkg, int appid, String reason) {
5925        if (callerUid == Process.SYSTEM_UID) {
5926            // Post an aysnc message to kill the application
5927            Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG);
5934            mHandler.sendMessage(msg);
5935        }
5939    }

1696            case KILL_APPLICATION_MSG: {
1697                synchronized (ActivityManagerService.this) {
1698                    int appid = msg.arg1;
1699                    boolean restart = (msg.arg2 == 1);
1700                    Bundle bundle = (Bundle)msg.obj;
1701                    String pkg = bundle.getString("pkg");
1702                    String reason = bundle.getString("reason");
1703                    forceStopPackageLocked(pkg, appid, restart, false, true, false,
1704                            false, UserHandle.USER_ALL, reason);
1705                }
1706            } break;

6174
6175    private final boolean forceStopPackageLocked(String name, int appId,
6176            boolean callerWillRestart, boolean purgeCache, boolean doit,
6177            boolean evenPersistent, boolean uninstalling, int userId, String reason) {
6180
6192
6193        if (doit) {
6194            if (name != null) {
6195                Slog.i(TAG, "Force stopping " + name + " appid=" + appId
6196                        + " user=" + userId + ": " + reason);                         --> I/ActivityManager(  653): Force stopping com.exmaple.ota appid=32120 user=-1: replace sys pkg
6197            } else {
6198                Slog.i(TAG, "Force stopping u" + userId + ": " + reason);
6199            }
6229
6230        boolean didSomething = killPackageProcessesLocked(name, appId, userId,
6231                -100, callerWillRestart, true, doit, evenPersistent,
6232                name == null ? ("stop user " + userId) : ("stop " + name));

6102    private final boolean killPackageProcessesLocked(String packageName, int appId,
6103            int userId, int minOomAdj, boolean callerWillRestart, boolean allowRestart,
6104            boolean doit, boolean evenPersistent, String reason) {
6167        int N = procs.size();
6168        for (int i=0; i<N; i++) {
6169            removeProcessLocked(procs.get(i), callerWillRestart, allowRestart, reason);
6170        }
6172        return N > 0;
6173    }

6337    private final boolean removeProcessLocked(ProcessRecord app,
6338            boolean callerWillRestart, boolean allowRestart, String reason) {
6363            app.kill(reason, true);   -> I/ActivityManager(  653): Killing 16640:com.exmaple.ota/u0a22120 (adj 9): stop com.exmaple.ota
6378        return needRestart;
6379    }

I/ActivityManager(  653): Force stopping com.exmaple.ota appid=32120 user=-1: replace sys pkg

I/ActivityManager(  653): Killing 16640:com.exmaple.ota/u0a22120 (adj 9): stop com.exmaple.ota



W/PackageManager(  653): Trying to update system app code path from /data/app/com.exmaple.ota-1 to /data/app/com.exmaple.ota-2

I/PackageManager(  653): Derived cpuAbiOverride=null

D/PackageManager(  653): Resolved nativeLibraryRoot for com.exmaple.ota to root=/data/app/com.exmaple.ota-2/lib, isa=true

D/PackageManager(  653): Abis for package[com.exmaple.ota] are primary=null secondary=null

I/art     (  653): DexFile_isDexOptNeeded failed to open oat file '/data/dalvik-cache/arm/data@app@com.exmaple.ota-2@base.apk@classes.dex' for file location '/data/app/com.exmaple.ota-2/base.apk': Failed to open oat filename for reading: No such file or directory

I/art     (  653): DexFile_isDexOptNeeded failed to open oat file '/data/app/com.exmaple.ota-2/arm/base.odex' for file location '/data/app/com.exmaple.ota-2/base.apk': Failed to open oat filename for reading: No such file or directory

I/PackageManager(  653): Running dexopt on: /data/app/com.exmaple.ota-2/base.apk pkg=com.exmaple.ota isa=arm vmSafeMode=false

I/dex2oat (16828): /system/bin/dex2oat --zip-fd=6 --zip-location=/data/app/com.exmaple.ota-2/base.apk --oat-fd=7 --oat-location=/data/dalvik-cache/arm/data@app@com.exmaple.ota-2@base.apk@classes.dex --instruction-set=arm --compile-pic --instruction-set-features=div --runtime-arg -Xms64m --runtime-arg -Xmx512m --swap-fd=12

I/art     (  653): Background partial concurrent mark sweep GC freed 108107(4MB) AllocSpace objects, 5(1197KB) LOS objects, 14% free, 23MB/27MB, paused 5.185ms total 135.408ms

D/ActivityManager(  653): cleanUpApplicationRecord -- 16640

W/ActivityManager(  653): Spurious death for ProcessRecord{56a9a86 16640:com.exmaple.ota/u0a22120}, curProc for 16640: null

W/PackageManager(  653): Code path for pkg : com.exmaple.ota changing from /data/app/com.exmaple.ota-1 to /data/app/com.exmaple.ota-2

W/PackageManager(  653): Resource path for pkg : com.exmaple.ota changing from /data/app/com.exmaple.ota-1 to /data/app/com.exmaple.ota-2

I/LoggerMetricsFactoryImpl(  653): PackageManager:AppInstalled:PackageVersion=1.6.5+1060500910;DV;1,PackageName=com.exmaple.ota;DV;1:NR


5546    private PackageParser.Package scanPackageLI(PackageParser.Package pkg, int parseFlags,
5547            int scanFlags, long currentTime, UserHandle user) throws PackageManagerException {
5548        boolean success = false;
5550            final PackageParser.Package res = scanPackageDirtyLI(pkg, parseFlags, scanFlags,
5551                    currentTime, user);
5559    }

5561    private PackageParser.Package scanPackageDirtyLI(PackageParser.Package pkg, int parseFlags,
5562            int scanFlags, long currentTime, UserHandle user) throws PackageManagerException {
6392        if ((scanFlags & SCAN_REPLACING) != 0) {
6393            killApplication(pkg.applicationInfo.packageName,
6394                        pkg.applicationInfo.uid, "update pkg");  refer to killApplication definition above, it will print "Force stopping com.exmaple.ota appid=32120 user=-1: update pkg"

I/ActivityManager(  653): Force stopping com.exmaple.ota appid=32120 user=-1: update pkg


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

1047                case POST_INSTALL: {
1048                    if (DEBUG_INSTALL) Log.v(TAG, "Handling post-install for " + msg.arg1);
1049                    PostInstallData data = mRunningInstalls.get(msg.arg1);
1050                    mRunningInstalls.delete(msg.arg1);     Remove the install task from list
1051                    boolean deleteOld = false;
1052
1053                    if (data != null) {
1054                        InstallArgs args = data.args;
1055                        PackageInstalledInfo res = data.res;
1056
1057                        if (res.returnCode == PackageManager.INSTALL_SUCCEEDED) {
1058                            res.removedInfo.sendBroadcast(false, true, false);       send out "Intent.ACTION_PACKAGE_REMOVED"
.....................................
1094                            sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED,
1095                                    res.pkg.applicationInfo.packageName,
1096                                    extras, null, null, firstUsers);
1097                            final boolean update = res.removedInfo.removedPackage != null;
1098                            if (update) {
1099                                extras.putBoolean(Intent.EXTRA_REPLACING, true);
1100                            }
1101                            sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED,
1102                                    res.pkg.applicationInfo.packageName,
1103                                    extras, null, null, updateUsers);
1104                            if (update) {
1105                                sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED,
1106                                        res.pkg.applicationInfo.packageName,
1107                                        extras, null, null, updateUsers);
1108                                sendPackageBroadcast(Intent.ACTION_MY_PACKAGE_REPLACED,
1109                                        null, null,
1110                                        res.pkg.applicationInfo.packageName, null, updateUsers);
1124                            }
1137                        if (deleteOld) {
1138                            synchronized (mInstallLock) {
1139                                res.removedInfo.args.doPostDeleteLI(true);  Remove the old apk file
1140                            }
1141                        }

How to handle "Intent.ACTION_PACKAGE_REMOVED"

In frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
17345                        case Intent.ACTION_PACKAGE_REMOVED:
17346                        case Intent.ACTION_PACKAGE_CHANGED:
17347                            Uri data = intent.getData();
17348                            String ssp;
17349                            if (data != null && (ssp=data.getSchemeSpecificPart()) != null) {
17350                                boolean removed = Intent.ACTION_PACKAGE_REMOVED.equals(action);
17351                                boolean fullUninstall = removed &&
17352                                        !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
17353                                if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) {
17354                                    forceStopPackageLocked(ssp, UserHandle.getAppId(
17355                                            intent.getIntExtra(Intent.EXTRA_UID, -1)),
17356                                            false, true, true, false, fullUninstall, userId,
17357                                            removed ? "pkg removed" : "pkg changed");   --->  Force stopping com.exmaple.ota appid=32120 user=0: pkg removed
17358                                }

...........................

I/ActivityManager(  653): Force stopping com.exmaple.ota appid=32120 user=0: pkg removed


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值