概述
在 Android 9 系统rom定制化开发过程中,经常需要对app进程实现保活管理。如在系统内存不足时,app进程不能被关闭,并且即使在最近任务中上划也不能结束任务进程。
核心类
frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
frameworks/base/services/core/java/com/android/server/am/ActivityStackSupervisor.java
代码
- 在
ActivityManagerService
构造方法ActivityManagerService(Context systemContext)
调用初始化白名单方法initKillAppWhiteList();
public ActivityManagerService(Context systemContext) {
mProcessStats = new ProcessStatsService(this, new File(systemDir, "procstats"));
mAppOpsService = mInjector.getAppOpsService(new File(systemDir, "appops.xml"), mHandler);
mGrantFile = new AtomicFile(new File(systemDir, "urigrants.xml"), "uri-grants");
+ initKillAppWhiteList();
mUserController = new UserController(this);
mVrController = new VrController(this);
}
- 在
ActivityManagerService
中定义initKillAppWhiteList()
方法并定义用来存储保活应用名单静态成员变量keepAlivePackageList
,还有判断是否在白名单内的静态方法isInKeepAliveList
。
private static List<String> keepAlivePackageList;
private void initKillAppWhiteList() {
keepAlivePackageList = new ArrayList<String>();
File keepAliveListFile = new File("/oem/etc/keep_alive_packages.xml");
if (keepAliveListFile.exists()) {
FileInputStream stream = null;
try {
stream = new FileInputStream(keepAliveListFile);
XmlPullParser parser = Xml.newPullParser();
parser.setInput(stream, null);
int type;
do {
type = parser.next();
if (type == XmlPullParser.START_TAG) {
String tag = parser.getName();
if ("app".equals(tag)) {
String pkgName = parser.getAttributeValue(null, "package");
keepAlivePackageList.add(pkgName);
Slog.i(TAG, "keep alive package " + pkgName);
}
}
} while (type != XmlPullParser.END_DOCUMENT);
} catch (NullPointerException e) {
Slog.w(TAG, "failed parsing " + keepAliveListFile, e);
} catch (NumberFormatException e) {
Slog.w(TAG, "failed parsing " + keepAliveListFile, e);
} catch (XmlPullParserException e) {
Slog.w(TAG, "failed parsing " + keepAliveListFile, e);
} catch (IOException e) {
Slog.w(TAG, "failed parsing " + keepAliveListFile, e);
} catch (IndexOutOfBoundsException e) {
Slog.w(TAG, "failed parsing " + keepAliveListFile, e);
} finally {
if (stream != null) {
try {
stream.close();
} catch (Exception e) {
Slog.e(TAG, "close exception" + e);
}
}
}
} else {
Log.d(TAG, "keepAliveListFile not exist!Closed performance mode!" + keepAliveListFile.toString());
}
}
static boolean isInKeepAliveList(String packageName) {
if (!TextUtils.isEmpty(packageName) && keepAlivePackageList != null && keepAlivePackageList.size() > 0) {
for (String itemName : keepAlivePackageList) {
if (itemName.equals(packageName)) {
return true;
}
}
}
return false;
}
initKillAppWhiteList()
方法将会读取配置文件/oem/etc/keep_alive_packages.xml
,该文件中配置了需要保活的app,配置如下
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<keep-alive-package>
<app package="com.test.portal" />
<app package="com.test.emm.android.helper"/>
</keep-alive-package>
- 在
ActivityManagerService
的cleanUpApplicationRecordLocked
方法中if (!app.persistent || app.isolated)
多加一个判断,改为if ((!app.persistent || app.isolated) && !isInKeepAliveList(app.info.packageName))
// if (!app.persistent || app.isolated) {
if ((!app.persistent || app.isolated) && !isInKeepAliveList(app.info.packageName)) {
- 在
ActivityManagerService
的applyOomAdjLocked
修改如下
private final boolean applyOomAdjLocked(ProcessRecord app, boolean doingAll, long now,
long nowElapsed) {
boolean success = true;
if (app.curRawAdj != app.setRawAdj) {
app.setRawAdj = app.curRawAdj;
}
int changes = 0;
// if (app.curAdj != app.setAdj) {
// ProcessList.setOomAdj(app.pid, app.uid, app.curAdj);
// if (DEBUG_SWITCH || DEBUG_OOM_ADJ || mCurOomAdjUid == app.info.uid) {
// String msg = "Set " + app.pid + " " + app.processName + " adj "
// + app.curAdj + ": " + app.adjType;
// reportOomAdjMessageLocked(TAG_OOM_ADJ, msg);
// }
// app.setAdj = app.curAdj;
// app.verifiedAdj = ProcessList.INVALID_ADJ;
// }
if (isInKeepAliveList(app.info.packageName) && (app.curAdj > ProcessList.PERSISTENT_SERVICE_ADJ)) {
Slog.d(TAG, app.info.packageName + " is in keep alive list");
ProcessList.setOomAdj(app.pid, app.uid, ProcessList.PERSISTENT_SERVICE_ADJ);
if (DEBUG_SWITCH || DEBUG_OOM_ADJ || mCurOomAdjUid == app.info.uid) {
String msg = "Set " + app.pid + " " + app.processName + " adj "
+ app.curAdj + ": " + app.adjType;
reportOomAdjMessageLocked(TAG_OOM_ADJ, msg);
}
app.setAdj = ProcessList.PERSISTENT_SERVICE_ADJ;
app.verifiedAdj = ProcessList.INVALID_ADJ;
} else {
if (app.curAdj != app.setAdj) {
ProcessList.setOomAdj(app.pid, app.uid, app.curAdj);
if (DEBUG_SWITCH || DEBUG_OOM_ADJ || mCurOomAdjUid == app.info.uid) {
String msg = "Set " + app.pid + " " + app.processName + " adj "
+ app.curAdj + ": " + app.adjType;
reportOomAdjMessageLocked(TAG_OOM_ADJ, msg);
}
app.setAdj = app.curAdj;
app.verifiedAdj = ProcessList.INVALID_ADJ;
}
}
}
- 在
ActivityStackSupervisor.java
的removeTaskByIdLocked
方法中增加判断
boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
boolean pauseImmediately, String reason) {
final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
if (tr != null) {
+ ComponentName componentName = tr.getBaseIntent().getComponent();
+ if(componentName != null){
+ String packageName = componentName.getPackageName();
+ if(ActivityManagerService.isInKeepAliveList(packageName)){
+ return false;
+ }
+ }
tr.removeTaskActivitiesLocked(pauseImmediately, reason);
cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
mService.getLockTaskController().clearLockedTask(tr);
if (tr.isPersistable) {
mService.notifyTaskPersisterLocked(null, true);
}
return true;
}
Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
return false;
}
编译
在 target 的配置文件 PRODUCT_COPY_FILES
加上配置使得 keep_alive_packages.xml
在打包编译时拷贝的正确的路径(/oem/etc/keep_alive_packages.xml)
PRODUCT_COPY_FILES += \
+ device/rockchip/rk3288/ind3288/keep_alive_packages.xml:$(TARGET_COPY_OUT_OEM)/etc/keep_alive_packages.xml \