Fix: crash when running am.jar without parameters

Issure:
When no args to run am.jar,mAm is not initialized before use
process will crash in NRE

Solution:
Initialize mAm in constructor

Bug: 202471754
Test: manual
Running follow sh on the device displays help messages correctly

base=/system
export CLASSPATH=$base/framework/am.jar
exec app_process $base/bin com.android.commands.am.Am

Change-Id: I088f4f5b4072d350c217655a291658d0bfd506e9
This commit is contained in:
hongzhu wang
2021-10-09 04:42:09 +00:00
parent bbed692810
commit 4201a5ddbc

View File

@@ -41,6 +41,10 @@ public class Am extends BaseCommand {
private IActivityManager mAm;
private IPackageManager mPm;
Am() {
svcInit();
}
/**
* Command-line entry point.
*
@@ -50,6 +54,20 @@ public class Am extends BaseCommand {
(new Am()).run(args);
}
private void svcInit() {
mAm = ActivityManager.getService();
if (mAm == null) {
System.err.println(NO_SYSTEM_ERROR_CODE);
return;
}
mPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
if (mPm == null) {
System.err.println(NO_SYSTEM_ERROR_CODE);
return;
}
}
@Override
public void onShowUsage(PrintStream out) {
try {
@@ -61,19 +79,6 @@ public class Am extends BaseCommand {
@Override
public void onRun() throws Exception {
mAm = ActivityManager.getService();
if (mAm == null) {
System.err.println(NO_SYSTEM_ERROR_CODE);
throw new AndroidException("Can't connect to activity manager; is the system running?");
}
mPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
if (mPm == null) {
System.err.println(NO_SYSTEM_ERROR_CODE);
throw new AndroidException("Can't connect to package manager; is the system running?");
}
String op = nextArgRequired();
if (op.equals("instrument")) {