@Override public void installApks(ImmutableList<Path> apks, InstallOptions installOptions) { ImmutableList<File> apkFiles = apks.stream().map(Path::toFile).collect(toImmutableList()); try { if (getVersion() .isGreaterOrEqualThan(AndroidVersion.ALLOW_SPLIT_APK_INSTALLATION.getApiLevel())) { device.installPackages( apkFiles, installOptions.getAllowReinstall(), installOptions.getAllowDowngrade() ? ImmutableList.of("-d") : ImmutableList.of(), installOptions.getTimeout().toMillis(), TimeUnit.MILLISECONDS); } else { device.installPackage( Iterables.getOnlyElement(apkFiles).toString(), installOptions.getAllowReinstall(), installOptions.getAllowDowngrade() ? "-d" : null); } } catch (InstallException e) { throw InstallationException.builder() .withCause(e) .withMessage("Installation of the app failed.") .build(); } } }
@SuppressWarnings("unchecked") @Test public void allowDowngrade_postL() throws Exception { when(mockDevice.getVersion()).thenReturn(new AndroidVersion(VersionCodes.LOLLIPOP)); DdmlibDevice ddmlibDevice = new DdmlibDevice(mockDevice); ddmlibDevice.installApks( ImmutableList.of(APK_PATH), InstallOptions.builder().setAllowDowngrade(true).build()); ArgumentCaptor<List<String>> extraArgsCaptor = ArgumentCaptor.forClass((Class) List.class); verify(mockDevice) .installPackages( eq(ImmutableList.of(APK_PATH.toFile())), anyBoolean(), extraArgsCaptor.capture(), anyLong(), any(TimeUnit.class)); assertThat(extraArgsCaptor.getValue()).contains("-d"); } }
/** * Installs an Android application made of several APK files (one main and 0..n split packages) * * @param apks list of apks to install (1 main APK + 0..n split apks) * @param reinstall set to <code>true</code> if re-install of app should be performed * @param installOptions optional extra arguments to pass. See 'adb shell pm install --help' for * available options. * @param timeout installation timeout * @param timeoutUnit {@link TimeUnit} corresponding to the timeout parameter * @throws InstallException if the installation fails. */ @Override public void installPackages(List<File> apks, boolean reinstall, List<String> installOptions, long timeout, TimeUnit timeoutUnit) throws InstallException { device.installPackages(apks, reinstall, installOptions, timeout, timeoutUnit); }
@Override public void installPackages(@NonNull List<File> splitApkFiles, @NonNull Collection<String> options, int timeoutInMs, ILogger logger) throws DeviceException { try { iDevice.installPackages(splitApkFiles, true /*reinstall*/, ImmutableList.copyOf(options), timeoutInMs, TimeUnit.MILLISECONDS); } catch (Exception e) { List<String> apkFileNames = Lists.transform( splitApkFiles, input -> input != null ? input.getAbsolutePath() : null); logger.error(e, "Unable to install " + Joiner.on(',').join(apkFileNames)); throw new DeviceException(e); } }
@Override public void installPackages(@NonNull List<File> splitApkFiles, @NonNull Collection<String> options, int timeoutInMs, ILogger logger) throws DeviceException { List<String> apkFileNames = Lists.transform(splitApkFiles, new Function<File, String>() { @Override public String apply(@Nullable File input) { return input != null ? input.getAbsolutePath() : null; } }); try { iDevice.installPackages(apkFileNames, timeoutInMs, true /*reinstall*/, options.isEmpty() ? null : options.toArray(new String[options.size()])); } catch (Exception e) { logger.error(e, "Unable to install " + Joiner.on(',').join(apkFileNames)); throw new DeviceException(e); } }