Search in sources :

Example 1 with Device

use of com.android.tools.build.bundletool.device.Device 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 Device

use of com.android.tools.build.bundletool.device.Device 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 Device

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

the class ConnectedDeviceModeTransparencyChecker method checkTransparency.

public static TransparencyCheckResult checkTransparency(CheckTransparencyCommand command) {
    command.getAdbServer().get().init(command.getAdbPath().get());
    AdbRunner adbRunner = new AdbRunner(command.getAdbServer().get());
    Device adbDevice = getDevice(command.getAdbServer().get(), command.getDeviceId());
    // Execute a shell command to retrieve paths to all APKs for the given package name.
    AdbShellCommandTask adbShellCommandTask = new AdbShellCommandTask(adbDevice, "pm path " + command.getPackageName().get());
    ImmutableList<String> pathsToApksOnDevice = adbShellCommandTask.execute().stream().filter(path -> path.startsWith(APK_PATH_ON_DEVICE_PREFIX)).map(path -> path.substring(APK_PATH_ON_DEVICE_PREFIX.length())).collect(toImmutableList());
    if (pathsToApksOnDevice.isEmpty()) {
        throw InvalidCommandException.builder().withInternalMessage("No files found for package " + command.getPackageName().get()).build();
    }
    // Pull APKs to a temporary directory and verify code transparency.
    try (TempDirectory tempDir = new TempDirectory("connected-device-transparency-check")) {
        Path apksExtractedSubDirectory = tempDir.getPath().resolve("extracted");
        Files.createDirectory(apksExtractedSubDirectory);
        ImmutableList<FilePullParams> pullParams = createPullParams(pathsToApksOnDevice, apksExtractedSubDirectory);
        if (command.getDeviceId().isPresent()) {
            adbRunner.run(device -> device.pull(pullParams), command.getDeviceId().get());
        } else {
            adbRunner.run(device -> device.pull(pullParams));
        }
        return ApkTransparencyCheckUtils.checkTransparency(pullParams.stream().map(FilePullParams::getDestinationPath).collect(toImmutableList()));
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : AdbRunner(com.android.tools.build.bundletool.device.AdbRunner) CheckTransparencyCommand(com.android.tools.build.bundletool.commands.CheckTransparencyCommand) FilePullParams(com.android.tools.build.bundletool.device.Device.FilePullParams) Files(java.nio.file.Files) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Device(com.android.tools.build.bundletool.device.Device) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) DeviceAnalyzer(com.android.tools.build.bundletool.device.DeviceAnalyzer) TempDirectory(com.android.tools.build.bundletool.io.TempDirectory) ImmutableList(com.google.common.collect.ImmutableList) Paths(java.nio.file.Paths) InvalidCommandException(com.android.tools.build.bundletool.model.exceptions.InvalidCommandException) Optional(java.util.Optional) AdbShellCommandTask(com.android.tools.build.bundletool.device.AdbShellCommandTask) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) Path(java.nio.file.Path) AdbServer(com.android.tools.build.bundletool.device.AdbServer) Path(java.nio.file.Path) TempDirectory(com.android.tools.build.bundletool.io.TempDirectory) AdbShellCommandTask(com.android.tools.build.bundletool.device.AdbShellCommandTask) AdbRunner(com.android.tools.build.bundletool.device.AdbRunner) Device(com.android.tools.build.bundletool.device.Device) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) FilePullParams(com.android.tools.build.bundletool.device.Device.FilePullParams)

Example 4 with Device

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

the class ConnectedDeviceModeTransparencyChecker method getDevice.

private static Device getDevice(AdbServer adbServer, Optional<String> deviceId) {
    DeviceAnalyzer deviceAnalyzer = new DeviceAnalyzer(adbServer);
    Device device;
    try {
        device = deviceAnalyzer.getAndValidateDevice(deviceId);
    } catch (TimeoutException e) {
        throw new UncheckedTimeoutException(e);
    }
    return device;
}
Also used : Device(com.android.tools.build.bundletool.device.Device) DeviceAnalyzer(com.android.tools.build.bundletool.device.DeviceAnalyzer) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) TimeoutException(java.util.concurrent.TimeoutException) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException)

Example 5 with Device

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

the class ActivityManagerRunnerTest method preLDevice_noResults.

@Test
public void preLDevice_noResults() {
    Device device = FakeDevice.fromDeviceSpec(SERIAL_NUMBER, DeviceState.ONLINE, mergeSpecs(sdkVersion(20), locales("en-US"), density(240), abis("armeabi")));
    ActivityManagerRunner runner = new ActivityManagerRunner(device);
    assertThat(runner.getDeviceLocales()).isEmpty();
    assertThat(runner.getDeviceAbis()).isEmpty();
}
Also used : Device(com.android.tools.build.bundletool.device.Device) FakeDevice(com.android.tools.build.bundletool.testing.FakeDevice) Test(org.junit.Test)

Aggregations

Device (com.android.tools.build.bundletool.device.Device)6 DeviceAnalyzer (com.android.tools.build.bundletool.device.DeviceAnalyzer)5 AdbServer (com.android.tools.build.bundletool.device.AdbServer)4 AdbShellCommandTask (com.android.tools.build.bundletool.device.AdbShellCommandTask)4 TempDirectory (com.android.tools.build.bundletool.io.TempDirectory)4 InvalidCommandException (com.android.tools.build.bundletool.model.exceptions.InvalidCommandException)4 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)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 BadgingInfoParser (com.android.tools.build.bundletool.device.BadgingInfoParser)3 BadgingInfo (com.android.tools.build.bundletool.device.BadgingInfoParser.BadgingInfo)3 PackagesParser (com.android.tools.build.bundletool.device.PackagesParser)3 InstalledPackageInfo (com.android.tools.build.bundletool.device.PackagesParser.InstalledPackageInfo)3 Flag (com.android.tools.build.bundletool.flags.Flag)3 ParsedFlags (com.android.tools.build.bundletool.flags.ParsedFlags)3