Search in sources :

Example 1 with InstalledPackageInfo

use of com.android.tools.build.bundletool.device.PackagesParser.InstalledPackageInfo in project bundletool by google.

the class InstallMultiApksCommand method extractApkListFromApks.

/**
 * Extracts the apk/apex files that will be installed from a given .apks.
 */
private static ImmutableList<PackagePathVersion> extractApkListFromApks(DeviceSpec deviceSpec, PackagePathVersion apksArchive, Optional<InstalledPackageInfo> installedPackage, TempDirectory tempDirectory) {
    logger.info(String.format("Extracting package '%s'", apksArchive.getPackageName()));
    try {
        Path output = tempDirectory.getPath().resolve(apksArchive.getPackageName());
        Files.createDirectory(output);
        ExtractApksCommand.Builder extractApksCommand = ExtractApksCommand.builder().setApksArchivePath(apksArchive.getPath()).setDeviceSpec(deviceSpec).setOutputDirectory(output);
        ImmutableList<Path> extractedPaths = fixExtension(extractApksCommand.build().execute(), installedPackage.map(InstalledPackageInfo::isApex).orElse(false));
        return extractedPaths.stream().map(path -> PackagePathVersion.create(path, apksArchive.getPackageName(), apksArchive.getVersionCode())).collect(toImmutableList());
    } catch (IncompatibleDeviceException e) {
        logger.warning(String.format("Package '%s' is not supported by the attached device (SDK version %d). Skipping.", apksArchive.getPackageName(), deviceSpec.getSdkVersion()));
        return ImmutableList.of();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) TimeoutException(java.util.concurrent.TimeoutException) SYSTEM_PATH_VARIABLE(com.android.tools.build.bundletool.model.utils.SdkToolsLocator.SYSTEM_PATH_VARIABLE) DeviceSpec(com.android.bundle.Devices.DeviceSpec) DeviceAnalyzer(com.android.tools.build.bundletool.device.DeviceAnalyzer) TempDirectory(com.android.tools.build.bundletool.io.TempDirectory) ImmutableListMultimap.toImmutableListMultimap(com.google.common.collect.ImmutableListMultimap.toImmutableListMultimap) Locale(java.util.Locale) Flag(com.android.tools.build.bundletool.flags.Flag) Duration(java.time.Duration) AdbShellCommandTask(com.android.tools.build.bundletool.device.AdbShellCommandTask) ZipFile(java.util.zip.ZipFile) FilePreconditions.checkFileHasExtension(com.android.tools.build.bundletool.model.utils.files.FilePreconditions.checkFileHasExtension) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) InstalledPackageInfo(com.android.tools.build.bundletool.device.PackagesParser.InstalledPackageInfo) ImmutableSet(com.google.common.collect.ImmutableSet) BadgingInfo(com.android.tools.build.bundletool.device.BadgingInfoParser.BadgingInfo) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Aapt2Command(com.android.tools.build.bundletool.androidtools.Aapt2Command) Device(com.android.tools.build.bundletool.device.Device) ParsedFlags(com.android.tools.build.bundletool.flags.ParsedFlags) CanIgnoreReturnValue(com.google.errorprone.annotations.CanIgnoreReturnValue) Streams(com.google.common.collect.Streams) Logger(java.util.logging.Logger) UncheckedIOException(java.io.UncheckedIOException) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) SystemEnvironmentProvider(com.android.tools.build.bundletool.model.utils.SystemEnvironmentProvider) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) AutoValue(com.google.auto.value.AutoValue) ByteStreams(com.google.common.io.ByteStreams) Optional(java.util.Optional) FilePreconditions.checkFileExistsAndReadable(com.android.tools.build.bundletool.model.utils.files.FilePreconditions.checkFileExistsAndReadable) ZipPath(com.android.tools.build.bundletool.model.ZipPath) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Supplier(java.util.function.Supplier) ImmutableList(com.google.common.collect.ImmutableList) DefaultSystemEnvironmentProvider(com.android.tools.build.bundletool.model.utils.DefaultSystemEnvironmentProvider) BadgingInfoParser(com.android.tools.build.bundletool.device.BadgingInfoParser) Suppliers(com.google.common.base.Suppliers) Comparator.comparing(java.util.Comparator.comparing) AdbServer(com.android.tools.build.bundletool.device.AdbServer) OutputStream(java.io.OutputStream) ANDROID_HOME_VARIABLE(com.android.tools.build.bundletool.model.utils.SdkToolsLocator.ANDROID_HOME_VARIABLE) FilePreconditions.checkFileExistsAndExecutable(com.android.tools.build.bundletool.model.utils.files.FilePreconditions.checkFileExistsAndExecutable) AdbCommand(com.android.tools.build.bundletool.androidtools.AdbCommand) Files(java.nio.file.Files) ANDROID_SERIAL_VARIABLE(com.android.tools.build.bundletool.commands.CommandUtils.ANDROID_SERIAL_VARIABLE) Collectors.maxBy(java.util.stream.Collectors.maxBy) IOException(java.io.IOException) PackagesParser(com.android.tools.build.bundletool.device.PackagesParser) Streams.stream(com.google.common.collect.Streams.stream) IncompatibleDeviceException(com.android.tools.build.bundletool.model.exceptions.IncompatibleDeviceException) InvalidCommandException(com.android.tools.build.bundletool.model.exceptions.InvalidCommandException) FlagDescription(com.android.tools.build.bundletool.commands.CommandHelp.FlagDescription) Versions(com.android.tools.build.bundletool.model.utils.Versions) CommandDescription(com.android.tools.build.bundletool.commands.CommandHelp.CommandDescription) InputStream(java.io.InputStream) InstalledPackageInfo(com.android.tools.build.bundletool.device.PackagesParser.InstalledPackageInfo) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) IncompatibleDeviceException(com.android.tools.build.bundletool.model.exceptions.IncompatibleDeviceException)

Example 2 with InstalledPackageInfo

use of com.android.tools.build.bundletool.device.PackagesParser.InstalledPackageInfo in project bundletool by google.

the class InstallMultiApksCommand method getPackagesInstalledOnDevice.

private static ImmutableMap<String, InstalledPackageInfo> getPackagesInstalledOnDevice(Device device) {
    // List standard packages (excluding apex)
    ImmutableList<String> listPackagesOutput = new AdbShellCommandTask(device, "pm list packages --show-versioncode").execute();
    // List .apex packages.
    ImmutableList<String> listApexPackagesOutput = new AdbShellCommandTask(device, "pm list packages --apex-only --show-versioncode").execute();
    ImmutableSet<InstalledPackageInfo> installedApks = new PackagesParser(/* isApex= */
    false).parse(listPackagesOutput);
    ImmutableSet<InstalledPackageInfo> installedApexPackages = new PackagesParser(/* isApex= */
    true).parse(listApexPackagesOutput);
    return Streams.concat(installedApks.stream(), installedApexPackages.stream()).collect(toImmutableMap(InstalledPackageInfo::getPackageName, installedPackageInfo -> installedPackageInfo));
}
Also used : TimeoutException(java.util.concurrent.TimeoutException) SYSTEM_PATH_VARIABLE(com.android.tools.build.bundletool.model.utils.SdkToolsLocator.SYSTEM_PATH_VARIABLE) DeviceSpec(com.android.bundle.Devices.DeviceSpec) DeviceAnalyzer(com.android.tools.build.bundletool.device.DeviceAnalyzer) TempDirectory(com.android.tools.build.bundletool.io.TempDirectory) ImmutableListMultimap.toImmutableListMultimap(com.google.common.collect.ImmutableListMultimap.toImmutableListMultimap) Locale(java.util.Locale) Flag(com.android.tools.build.bundletool.flags.Flag) Duration(java.time.Duration) AdbShellCommandTask(com.android.tools.build.bundletool.device.AdbShellCommandTask) ZipFile(java.util.zip.ZipFile) FilePreconditions.checkFileHasExtension(com.android.tools.build.bundletool.model.utils.files.FilePreconditions.checkFileHasExtension) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) InstalledPackageInfo(com.android.tools.build.bundletool.device.PackagesParser.InstalledPackageInfo) ImmutableSet(com.google.common.collect.ImmutableSet) BadgingInfo(com.android.tools.build.bundletool.device.BadgingInfoParser.BadgingInfo) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Aapt2Command(com.android.tools.build.bundletool.androidtools.Aapt2Command) Device(com.android.tools.build.bundletool.device.Device) ParsedFlags(com.android.tools.build.bundletool.flags.ParsedFlags) CanIgnoreReturnValue(com.google.errorprone.annotations.CanIgnoreReturnValue) Streams(com.google.common.collect.Streams) Logger(java.util.logging.Logger) UncheckedIOException(java.io.UncheckedIOException) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) SystemEnvironmentProvider(com.android.tools.build.bundletool.model.utils.SystemEnvironmentProvider) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) AutoValue(com.google.auto.value.AutoValue) ByteStreams(com.google.common.io.ByteStreams) Optional(java.util.Optional) FilePreconditions.checkFileExistsAndReadable(com.android.tools.build.bundletool.model.utils.files.FilePreconditions.checkFileExistsAndReadable) ZipPath(com.android.tools.build.bundletool.model.ZipPath) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Supplier(java.util.function.Supplier) ImmutableList(com.google.common.collect.ImmutableList) DefaultSystemEnvironmentProvider(com.android.tools.build.bundletool.model.utils.DefaultSystemEnvironmentProvider) BadgingInfoParser(com.android.tools.build.bundletool.device.BadgingInfoParser) Suppliers(com.google.common.base.Suppliers) Comparator.comparing(java.util.Comparator.comparing) AdbServer(com.android.tools.build.bundletool.device.AdbServer) OutputStream(java.io.OutputStream) ANDROID_HOME_VARIABLE(com.android.tools.build.bundletool.model.utils.SdkToolsLocator.ANDROID_HOME_VARIABLE) FilePreconditions.checkFileExistsAndExecutable(com.android.tools.build.bundletool.model.utils.files.FilePreconditions.checkFileExistsAndExecutable) AdbCommand(com.android.tools.build.bundletool.androidtools.AdbCommand) Files(java.nio.file.Files) ANDROID_SERIAL_VARIABLE(com.android.tools.build.bundletool.commands.CommandUtils.ANDROID_SERIAL_VARIABLE) Collectors.maxBy(java.util.stream.Collectors.maxBy) IOException(java.io.IOException) PackagesParser(com.android.tools.build.bundletool.device.PackagesParser) Streams.stream(com.google.common.collect.Streams.stream) IncompatibleDeviceException(com.android.tools.build.bundletool.model.exceptions.IncompatibleDeviceException) InvalidCommandException(com.android.tools.build.bundletool.model.exceptions.InvalidCommandException) FlagDescription(com.android.tools.build.bundletool.commands.CommandHelp.FlagDescription) Versions(com.android.tools.build.bundletool.model.utils.Versions) CommandDescription(com.android.tools.build.bundletool.commands.CommandHelp.CommandDescription) InputStream(java.io.InputStream) AdbShellCommandTask(com.android.tools.build.bundletool.device.AdbShellCommandTask) PackagesParser(com.android.tools.build.bundletool.device.PackagesParser) InstalledPackageInfo(com.android.tools.build.bundletool.device.PackagesParser.InstalledPackageInfo)

Example 3 with InstalledPackageInfo

use of com.android.tools.build.bundletool.device.PackagesParser.InstalledPackageInfo in project bundletool by google.

the class InstallMultiApksCommand method shouldInstall.

/**
 * The package should be installed if:
 *
 * <ul>
 *   <li>If it is not already present on the device and --update-only is not set, or
 *   <li>The installable version has a equal or higher version code than the one already
 *       installed.
 * </ul>
 */
private boolean shouldInstall(PackagePathVersion apk, ImmutableMap<String, InstalledPackageInfo> existingPackages) {
    if (getUpdateOnly() && !existingPackages.containsKey(apk.getPackageName())) {
        logger.info(String.format("Package '%s' not present on device, skipping due to --%s.", apk.getPackageName(), UPDATE_ONLY_FLAG.getName()));
        return false;
    }
    if (!existingPackages.containsKey(apk.getPackageName())) {
        return true;
    }
    InstalledPackageInfo existingPackage = existingPackages.get(apk.getPackageName());
    if (existingPackage.getVersionCode() <= apk.getVersionCode()) {
        return true;
    }
    // If the user is attempting to install a mixture of lower and higher version .apks, that
    // likely indicates something is wrong.
    logger.warning(String.format("A higher version of package '%s' (%d vs %d) is already present on device," + " skipping.", apk.getPackageName(), apk.getVersionCode(), existingPackage.getVersionCode()));
    return false;
}
Also used : InstalledPackageInfo(com.android.tools.build.bundletool.device.PackagesParser.InstalledPackageInfo)

Example 4 with InstalledPackageInfo

use of com.android.tools.build.bundletool.device.PackagesParser.InstalledPackageInfo in project bundletool by google.

the class InstallMultiApksCommand method execute.

public void execute() throws TimeoutException, IOException {
    validateInput();
    AdbServer adbServer = getAdbServer();
    adbServer.init(getAdbPath());
    try (TempDirectory tempDirectory = new TempDirectory()) {
        DeviceAnalyzer deviceAnalyzer = new DeviceAnalyzer(adbServer);
        DeviceSpec deviceSpec = deviceAnalyzer.getDeviceSpec(getDeviceId());
        Device device = deviceAnalyzer.getAndValidateDevice(getDeviceId());
        if (getTimeout().isPresent() && !device.getVersion().isGreaterOrEqualThan(Versions.ANDROID_S_API_VERSION)) {
            throw InvalidCommandException.builder().withInternalMessage("'%s' flag is supported for Android 12+ devices.", TIMEOUT_MILLIS_FLAG.getName()).build();
        }
        Path aapt2Dir = tempDirectory.getPath().resolve("aapt2");
        Files.createDirectory(aapt2Dir);
        Supplier<Aapt2Command> aapt2CommandSupplier = Suppliers.memoize(() -> getOrExtractAapt2Command(aapt2Dir));
        ImmutableMap<String, InstalledPackageInfo> existingPackages = getPackagesInstalledOnDevice(device);
        ImmutableList<PackagePathVersion> installableApksFilesWithBadgingInfo = getActualApksPaths(tempDirectory).stream().flatMap(apksArchivePath -> stream(apksWithPackageName(apksArchivePath, deviceSpec, aapt2CommandSupplier))).filter(apks -> shouldInstall(apks, existingPackages)).collect(toImmutableList());
        ImmutableList<PackagePathVersion> apkFilesToInstall = uniqueApksByPackageName(installableApksFilesWithBadgingInfo).stream().flatMap(apks -> extractApkListFromApks(deviceSpec, apks, Optional.ofNullable(existingPackages.get(apks.getPackageName())), tempDirectory).stream()).collect(toImmutableList());
        ImmutableListMultimap<String, String> apkToInstallByPackage = apkFilesToInstall.stream().collect(toImmutableListMultimap(PackagePathVersion::getPackageName, packagePathVersion -> packagePathVersion.getPath().toAbsolutePath().toString()));
        if (apkFilesToInstall.isEmpty()) {
            logger.warning("No packages found to install! Exiting...");
            return;
        }
        AdbCommand adbCommand = getOrCreateAdbCommand();
        ImmutableList<String> commandResults = adbCommand.installMultiPackage(apkToInstallByPackage, getStaged(), getEnableRollback(), getTimeout(), getDeviceId());
        logger.info(String.format("Output:\n%s", String.join("\n", commandResults)));
        logger.info("Please reboot device to complete installation.");
    }
}
Also used : Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) TimeoutException(java.util.concurrent.TimeoutException) SYSTEM_PATH_VARIABLE(com.android.tools.build.bundletool.model.utils.SdkToolsLocator.SYSTEM_PATH_VARIABLE) DeviceSpec(com.android.bundle.Devices.DeviceSpec) DeviceAnalyzer(com.android.tools.build.bundletool.device.DeviceAnalyzer) TempDirectory(com.android.tools.build.bundletool.io.TempDirectory) ImmutableListMultimap.toImmutableListMultimap(com.google.common.collect.ImmutableListMultimap.toImmutableListMultimap) Locale(java.util.Locale) Flag(com.android.tools.build.bundletool.flags.Flag) Duration(java.time.Duration) AdbShellCommandTask(com.android.tools.build.bundletool.device.AdbShellCommandTask) ZipFile(java.util.zip.ZipFile) FilePreconditions.checkFileHasExtension(com.android.tools.build.bundletool.model.utils.files.FilePreconditions.checkFileHasExtension) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) InstalledPackageInfo(com.android.tools.build.bundletool.device.PackagesParser.InstalledPackageInfo) ImmutableSet(com.google.common.collect.ImmutableSet) BadgingInfo(com.android.tools.build.bundletool.device.BadgingInfoParser.BadgingInfo) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Aapt2Command(com.android.tools.build.bundletool.androidtools.Aapt2Command) Device(com.android.tools.build.bundletool.device.Device) ParsedFlags(com.android.tools.build.bundletool.flags.ParsedFlags) CanIgnoreReturnValue(com.google.errorprone.annotations.CanIgnoreReturnValue) Streams(com.google.common.collect.Streams) Logger(java.util.logging.Logger) UncheckedIOException(java.io.UncheckedIOException) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) SystemEnvironmentProvider(com.android.tools.build.bundletool.model.utils.SystemEnvironmentProvider) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) AutoValue(com.google.auto.value.AutoValue) ByteStreams(com.google.common.io.ByteStreams) Optional(java.util.Optional) FilePreconditions.checkFileExistsAndReadable(com.android.tools.build.bundletool.model.utils.files.FilePreconditions.checkFileExistsAndReadable) ZipPath(com.android.tools.build.bundletool.model.ZipPath) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Supplier(java.util.function.Supplier) ImmutableList(com.google.common.collect.ImmutableList) DefaultSystemEnvironmentProvider(com.android.tools.build.bundletool.model.utils.DefaultSystemEnvironmentProvider) BadgingInfoParser(com.android.tools.build.bundletool.device.BadgingInfoParser) Suppliers(com.google.common.base.Suppliers) Comparator.comparing(java.util.Comparator.comparing) AdbServer(com.android.tools.build.bundletool.device.AdbServer) OutputStream(java.io.OutputStream) ANDROID_HOME_VARIABLE(com.android.tools.build.bundletool.model.utils.SdkToolsLocator.ANDROID_HOME_VARIABLE) FilePreconditions.checkFileExistsAndExecutable(com.android.tools.build.bundletool.model.utils.files.FilePreconditions.checkFileExistsAndExecutable) AdbCommand(com.android.tools.build.bundletool.androidtools.AdbCommand) Files(java.nio.file.Files) ANDROID_SERIAL_VARIABLE(com.android.tools.build.bundletool.commands.CommandUtils.ANDROID_SERIAL_VARIABLE) Collectors.maxBy(java.util.stream.Collectors.maxBy) IOException(java.io.IOException) PackagesParser(com.android.tools.build.bundletool.device.PackagesParser) Streams.stream(com.google.common.collect.Streams.stream) IncompatibleDeviceException(com.android.tools.build.bundletool.model.exceptions.IncompatibleDeviceException) InvalidCommandException(com.android.tools.build.bundletool.model.exceptions.InvalidCommandException) FlagDescription(com.android.tools.build.bundletool.commands.CommandHelp.FlagDescription) Versions(com.android.tools.build.bundletool.model.utils.Versions) CommandDescription(com.android.tools.build.bundletool.commands.CommandHelp.CommandDescription) InputStream(java.io.InputStream) Aapt2Command(com.android.tools.build.bundletool.androidtools.Aapt2Command) Device(com.android.tools.build.bundletool.device.Device) DeviceAnalyzer(com.android.tools.build.bundletool.device.DeviceAnalyzer) DeviceSpec(com.android.bundle.Devices.DeviceSpec) TempDirectory(com.android.tools.build.bundletool.io.TempDirectory) InstalledPackageInfo(com.android.tools.build.bundletool.device.PackagesParser.InstalledPackageInfo) AdbServer(com.android.tools.build.bundletool.device.AdbServer) AdbCommand(com.android.tools.build.bundletool.androidtools.AdbCommand)

Aggregations

InstalledPackageInfo (com.android.tools.build.bundletool.device.PackagesParser.InstalledPackageInfo)4 DeviceSpec (com.android.bundle.Devices.DeviceSpec)3 Aapt2Command (com.android.tools.build.bundletool.androidtools.Aapt2Command)3 AdbCommand (com.android.tools.build.bundletool.androidtools.AdbCommand)3 CommandDescription (com.android.tools.build.bundletool.commands.CommandHelp.CommandDescription)3 FlagDescription (com.android.tools.build.bundletool.commands.CommandHelp.FlagDescription)3 ANDROID_SERIAL_VARIABLE (com.android.tools.build.bundletool.commands.CommandUtils.ANDROID_SERIAL_VARIABLE)3 AdbServer (com.android.tools.build.bundletool.device.AdbServer)3 AdbShellCommandTask (com.android.tools.build.bundletool.device.AdbShellCommandTask)3 BadgingInfoParser (com.android.tools.build.bundletool.device.BadgingInfoParser)3 BadgingInfo (com.android.tools.build.bundletool.device.BadgingInfoParser.BadgingInfo)3 Device (com.android.tools.build.bundletool.device.Device)3 DeviceAnalyzer (com.android.tools.build.bundletool.device.DeviceAnalyzer)3 PackagesParser (com.android.tools.build.bundletool.device.PackagesParser)3 Flag (com.android.tools.build.bundletool.flags.Flag)3 ParsedFlags (com.android.tools.build.bundletool.flags.ParsedFlags)3 TempDirectory (com.android.tools.build.bundletool.io.TempDirectory)3 ZipPath (com.android.tools.build.bundletool.model.ZipPath)3 IncompatibleDeviceException (com.android.tools.build.bundletool.model.exceptions.IncompatibleDeviceException)3 InvalidCommandException (com.android.tools.build.bundletool.model.exceptions.InvalidCommandException)3