@@ -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 ());
0 commit comments