From 4201a5ddbcbc16f482e2a9355c628aa57f1fff19 Mon Sep 17 00:00:00 2001 From: hongzhu wang Date: Sat, 9 Oct 2021 04:42:09 +0000 Subject: [PATCH] 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 --- cmds/am/src/com/android/commands/am/Am.java | 31 ++++++++++++--------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/cmds/am/src/com/android/commands/am/Am.java b/cmds/am/src/com/android/commands/am/Am.java index 4e46aa3f42d56..9564dde7fe06d 100644 --- a/cmds/am/src/com/android/commands/am/Am.java +++ b/cmds/am/src/com/android/commands/am/Am.java @@ -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")) {