Skip to content

Commit 908988e

Browse files
DeviceInfracopybara-github
authored andcommitted
Internal change
PiperOrigin-RevId: 875635253
1 parent 53b6fdb commit 908988e

File tree

2 files changed

+47
-59
lines changed

2 files changed

+47
-59
lines changed

src/java/com/google/devtools/mobileharness/platform/android/packagemanager/AndroidPackageManagerUtil.java

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,6 @@ public class AndroidPackageManagerUtil {
8282
/** ADB arg of uninstalation. Should be followed with a package name. */
8383
@VisibleForTesting static final String ADB_ARG_UNINSTALL = "uninstall";
8484

85-
/** ADB args for installing a APK. Should be followed by the path of the APK. */
86-
@VisibleForTesting static final String[] ADB_ARGS_INSTALL = new String[] {"install", "-r", "-t"};
87-
88-
/**
89-
* ADB args for installing an app with multiple files (APK or Dex Metadata files). Should be
90-
* followed by the space-delimited paths of multiple files.
91-
*/
92-
@VisibleForTesting
93-
static final String[] ADB_ARGS_INSTALL_MULTIPLE = new String[] {"install-multiple", "-r"};
94-
9585
/**
9686
* split_1.apk:split_2.apk:...:split_n.apk. All apks in each item are the split apks belong to a
9787
* single package.
@@ -103,8 +93,7 @@ public class AndroidPackageManagerUtil {
10393

10494
/** ADB shell command for installing a package. */
10595
@VisibleForTesting
106-
static final ImmutableList<String> ADB_SHELL_INSTALL_PACKAGE =
107-
ImmutableList.of("pm", "install", "-r", "-t");
96+
static final ImmutableList<String> ADB_SHELL_INSTALL_PACKAGE = ImmutableList.of("pm", "install");
10897

10998
/** ADB shell command for uninstalling a package. Should be followed by the package name. */
11099
@VisibleForTesting static final String ADB_SHELL_UNINSTALL_PACKAGE = "pm uninstall";
@@ -1019,14 +1008,18 @@ public void installRemoteApk(
10191008
throws MobileHarnessException, InterruptedException {
10201009
installApk(
10211010
utilArgs,
1011+
InstallCmdArgs.builder()
1012+
.setReplaceExistingApp(true)
1013+
.setAllowTestPackages(true)
1014+
.setAllowVersionCodeDowngrade(true)
1015+
.setGrantPermissions(grantPermissions)
1016+
.addExtraArgs(extraArgs)
1017+
.build(),
10221018
packageName,
10231019
apkPath,
10241020
null,
1025-
grantPermissions,
1026-
/* forceNoStreaming= */ false,
10271021
/* isRemoteInstall= */ true,
1028-
installTimeout,
1029-
extraArgs);
1022+
installTimeout);
10301023
}
10311024

10321025
/**
@@ -1112,52 +1105,41 @@ public void installApk(
11121105

11131106
installApk(
11141107
utilArgs,
1108+
InstallCmdArgs.builder()
1109+
.setReplaceExistingApp(true)
1110+
.setAllowTestPackages(true)
1111+
.setAllowVersionCodeDowngrade(true)
1112+
.setGrantPermissions(grantPermissions)
1113+
.setForceNoStreaming(forceNoStreaming)
1114+
.addExtraArgs(extraArgs)
1115+
.build(),
11151116
packageName,
11161117
apkPath,
11171118
dexMetadataPath,
1118-
grantPermissions,
1119-
forceNoStreaming,
11201119
/* isRemoteInstall= */ false,
1121-
installTimeout,
1122-
extraArgs);
1120+
installTimeout);
11231121
}
11241122

11251123
private void installApk(
11261124
UtilArgs utilArgs,
1125+
InstallCmdArgs installCmdArgs,
11271126
String packageName,
11281127
String apkPath,
11291128
@Nullable String dexMetadataPath,
1130-
boolean grantPermissions,
1131-
boolean forceNoStreaming,
11321129
boolean isRemoteInstall,
1133-
@Nullable Duration installTimeout,
1134-
String... extraArgs)
1130+
@Nullable Duration installTimeout)
11351131
throws MobileHarnessException, InterruptedException {
1136-
// Installs the apk.
1137-
// "-d" only works with 17+ devices to allow app downgrade.
1138-
// "-g" only works with 23+ devices to grant all required permissions.
11391132
String serial = utilArgs.serial();
11401133
int sdkVersion = utilArgs.sdkVersion().orElse(0);
11411134
String[] installFiles =
11421135
dexMetadataPath != null ? new String[] {apkPath, dexMetadataPath} : new String[] {apkPath};
11431136
String[] installCommand =
11441137
isRemoteInstall
11451138
? ADB_SHELL_INSTALL_PACKAGE.toArray(new String[0])
1146-
: installFiles.length > 1 ? ADB_ARGS_INSTALL_MULTIPLE : ADB_ARGS_INSTALL;
1147-
if (sdkVersion >= 17) {
1148-
installCommand = ArrayUtil.join(installCommand, "-d");
1149-
}
1150-
if (sdkVersion >= 23 && grantPermissions) {
1151-
installCommand = ArrayUtil.join(installCommand, "-g");
1152-
}
1153-
1154-
if (sdkVersion >= 27 && forceNoStreaming) {
1155-
installCommand = ArrayUtil.join(installCommand, "--no-streaming");
1156-
}
1157-
1158-
if (extraArgs.length > 0) {
1159-
installCommand = ArrayUtil.join(installCommand, extraArgs);
1160-
}
1139+
: installFiles.length > 1
1140+
? new String[] {"install-multiple"}
1141+
: new String[] {"install"};
1142+
installCommand = ArrayUtil.join(installCommand, installCmdArgs.getInstallArgsArray(sdkVersion));
11611143

11621144
if (utilArgs.userId().isPresent()) {
11631145
installCommand = ArrayUtil.join(installCommand, "--user", utilArgs.userId().get());

src/java/com/google/devtools/mobileharness/platform/android/packagemanager/InstallCmdArgs.java

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@
1717
package com.google.devtools.mobileharness.platform.android.packagemanager;
1818

1919
import com.google.auto.value.AutoValue;
20-
import com.google.auto.value.extension.memoized.Memoized;
2120
import com.google.common.collect.ImmutableList;
2221
import com.google.errorprone.annotations.CanIgnoreReturnValue;
23-
import java.util.ArrayList;
24-
import java.util.List;
2522

2623
/**
2724
* Common args for adb app installation: install, install-multiple, install-multi-package. Only used
@@ -68,29 +65,38 @@ public abstract class InstallCmdArgs {
6865

6966
abstract Builder toBuilder();
7067

71-
@Memoized
68+
/** Gets the install args array. */
7269
public String[] getInstallArgsArray() {
73-
List<String> argList = new ArrayList<>();
70+
return getInstallArgsArray(Integer.MAX_VALUE);
71+
}
72+
73+
/**
74+
* Gets the install args array for the given sdk version.
75+
*
76+
* @param sdkVersion the sdk version of the device, used to determine if the arg is supported.
77+
*/
78+
public String[] getInstallArgsArray(int sdkVersion) {
79+
ImmutableList.Builder<String> args = ImmutableList.builder();
7480
if (replaceExistingApp()) {
75-
argList.add(ARG_REPLACE_EXISTING_APP);
81+
args.add(ARG_REPLACE_EXISTING_APP);
7682
}
7783
if (allowTestPackages()) {
78-
argList.add(ARG_ALLOW_TEST_PACKAGES);
84+
args.add(ARG_ALLOW_TEST_PACKAGES);
7985
}
80-
if (allowVersionCodeDowngrade()) {
81-
argList.add(ARG_ALLOW_VERSION_CODE_DOWNGRADE);
86+
if (allowVersionCodeDowngrade() && sdkVersion >= 17) {
87+
args.add(ARG_ALLOW_VERSION_CODE_DOWNGRADE);
8288
}
83-
if (grantPermissions()) {
84-
argList.add(ARG_GRANT_PERMISSIONS);
89+
if (grantPermissions() && sdkVersion >= 23) {
90+
args.add(ARG_GRANT_PERMISSIONS);
8591
}
86-
if (instant()) {
87-
argList.add(ARG_INSTANT);
92+
if (instant() && sdkVersion >= 26) {
93+
args.add(ARG_INSTANT);
8894
}
89-
if (forceNoStreaming()) {
90-
argList.add(ARG_FORCE_NO_STREAMING);
95+
if (forceNoStreaming() && sdkVersion >= 27) {
96+
args.add(ARG_FORCE_NO_STREAMING);
9197
}
92-
argList.addAll(extraArgs());
93-
return argList.toArray(new String[0]);
98+
args.addAll(extraArgs());
99+
return args.build().toArray(new String[0]);
94100
}
95101

96102
/** Gets default InstallArgs builder instance. */

0 commit comments

Comments
 (0)