Search in sources :

Example 21 with AdbServer

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

the class InstallApksCommandTest method badAbisDevice_throws.

@Test
@Theory
public void badAbisDevice_throws(@FromDataPoints("apksInDirectory") boolean apksInDirectory) throws Exception {
    Path apksFile = createApks(createSimpleTableOfContent(ZipPath.create("base-master.apk")), apksInDirectory);
    DeviceSpec deviceSpec = mergeSpecs(sdkVersion(21), density(480), abis(), locales("en-US"));
    FakeDevice fakeDevice = FakeDevice.fromDeviceSpec(DEVICE_ID, DeviceState.ONLINE, deviceSpec);
    AdbServer adbServer = new FakeAdbServer(/* hasInitialDeviceList= */
    true, ImmutableList.of(fakeDevice));
    InstallApksCommand command = InstallApksCommand.builder().setApksArchivePath(apksFile).setAdbPath(adbPath).setAdbServer(adbServer).build();
    Throwable exception = assertThrows(IllegalStateException.class, () -> command.execute());
    assertThat(exception).hasMessageThat().contains("Error retrieving device ABIs");
}
Also used : Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) DeviceSpec(com.android.bundle.Devices.DeviceSpec) FakeDevice(com.android.tools.build.bundletool.testing.FakeDevice) AdbServer(com.android.tools.build.bundletool.device.AdbServer) FakeAdbServer(com.android.tools.build.bundletool.testing.FakeAdbServer) FakeAdbServer(com.android.tools.build.bundletool.testing.FakeAdbServer) Test(org.junit.Test) Theory(org.junit.experimental.theories.Theory)

Example 22 with AdbServer

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

the class InstallApksCommandTest method incompleteApksFile_missingAbiSplitMatchedForDevice_throws.

@Test
public void incompleteApksFile_missingAbiSplitMatchedForDevice_throws() throws Exception {
    // Partial APK Set file where 'x86' split is included and 'x86_64' split is not included because
    // device spec sent to 'build-apks' command doesn't support it.
    // Next, device spec that should be matched to 'x86_64' split is provided to 'install-apks'
    // command and command must throw IncompatibleDeviceException as mathed split 'x86_64' is not
    // available.
    BuildApksResult tableOfContent = BuildApksResult.newBuilder().setBundletool(Bundletool.newBuilder().setVersion(BundleToolVersion.getCurrentVersion().toString())).addVariant(createVariant(VariantTargeting.getDefaultInstance(), createSplitApkSet(/* moduleName= */
    "base", createMasterApkDescription(ApkTargeting.getDefaultInstance(), ZipPath.create("base-master.apk")), splitApkDescription(apkAbiTargeting(X86, ImmutableSet.of(X86_64)), ZipPath.create("base-x86.apk"))))).build();
    Path apksFile = createApks(tableOfContent, /* apksInDirectory= */
    false);
    DeviceSpec deviceSpec = mergeSpecs(sdkVersion(21), density(DensityAlias.MDPI), abis("x86_64", "x86"), locales("en-US"));
    FakeDevice fakeDevice = FakeDevice.fromDeviceSpec(DEVICE_ID, DeviceState.ONLINE, deviceSpec);
    AdbServer adbServer = new FakeAdbServer(/* hasInitialDeviceList= */
    true, ImmutableList.of(fakeDevice));
    InstallApksCommand command = InstallApksCommand.builder().setApksArchivePath(apksFile).setAdbPath(adbPath).setAdbServer(adbServer).build();
    Throwable exception = assertThrows(IncompatibleDeviceException.class, command::execute);
    assertThat(exception).hasMessageThat().contains("Missing APKs for [ABI] dimensions in the module 'base' for the provided device.");
}
Also used : Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) DeviceSpec(com.android.bundle.Devices.DeviceSpec) BuildApksResult(com.android.bundle.Commands.BuildApksResult) FakeDevice(com.android.tools.build.bundletool.testing.FakeDevice) AdbServer(com.android.tools.build.bundletool.device.AdbServer) FakeAdbServer(com.android.tools.build.bundletool.testing.FakeAdbServer) FakeAdbServer(com.android.tools.build.bundletool.testing.FakeAdbServer) Test(org.junit.Test)

Example 23 with AdbServer

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

the class InstallApksCommandTest method moduleDependencies_diamondGraph.

@Test
@Theory
public void moduleDependencies_diamondGraph(@FromDataPoints("apksInDirectory") boolean apksInDirectory) throws Exception {
    BuildApksResult tableOfContent = BuildApksResult.newBuilder().setBundletool(Bundletool.newBuilder().setVersion(BundleToolVersion.getCurrentVersion().toString())).addVariant(createVariant(VariantTargeting.getDefaultInstance(), createSplitApkSet(/* moduleName= */
    "base", createMasterApkDescription(ApkTargeting.getDefaultInstance(), ZipPath.create("base-master.apk"))), createSplitApkSet(/* moduleName= */
    "feature1", DeliveryType.ON_DEMAND, /* moduleDependencies= */
    ImmutableList.of(), createMasterApkDescription(ApkTargeting.getDefaultInstance(), ZipPath.create("feature1-master.apk"))), createSplitApkSet(/* moduleName= */
    "feature2", DeliveryType.ON_DEMAND, /* moduleDependencies= */
    ImmutableList.of("feature1"), createMasterApkDescription(ApkTargeting.getDefaultInstance(), ZipPath.create("feature2-master.apk"))), createSplitApkSet(/* moduleName= */
    "feature3", DeliveryType.ON_DEMAND, /* moduleDependencies= */
    ImmutableList.of("feature1"), createMasterApkDescription(ApkTargeting.getDefaultInstance(), ZipPath.create("feature3-master.apk"))), createSplitApkSet(/* moduleName= */
    "feature4", DeliveryType.ON_DEMAND, /* moduleDependencies= */
    ImmutableList.of("feature2", "feature3"), createMasterApkDescription(ApkTargeting.getDefaultInstance(), ZipPath.create("feature4-master.apk"))))).build();
    Path apksFile = createApks(tableOfContent, apksInDirectory);
    List<Path> installedApks = new ArrayList<>();
    FakeDevice fakeDevice = FakeDevice.fromDeviceSpec(DEVICE_ID, DeviceState.ONLINE, lDeviceWithLocales("en-US"));
    AdbServer adbServer = new FakeAdbServer(/* hasInitialDeviceList= */
    true, ImmutableList.of(fakeDevice));
    fakeDevice.setInstallApksSideEffect((apks, installOptions) -> installedApks.addAll(apks));
    InstallApksCommand.builder().setApksArchivePath(apksFile).setAdbPath(adbPath).setAdbServer(adbServer).setModules(ImmutableSet.of("feature4")).build().execute();
    assertThat(getFileNames(installedApks)).containsExactly("base-master.apk", "feature1-master.apk", "feature2-master.apk", "feature3-master.apk", "feature4-master.apk");
}
Also used : Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) BuildApksResult(com.android.bundle.Commands.BuildApksResult) ArrayList(java.util.ArrayList) FakeDevice(com.android.tools.build.bundletool.testing.FakeDevice) AdbServer(com.android.tools.build.bundletool.device.AdbServer) FakeAdbServer(com.android.tools.build.bundletool.testing.FakeAdbServer) FakeAdbServer(com.android.tools.build.bundletool.testing.FakeAdbServer) Test(org.junit.Test) Theory(org.junit.experimental.theories.Theory)

Example 24 with AdbServer

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

Example 25 with AdbServer

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

the class BundleToolMain method main.

/**
 * Parses the flags and routes to the appropriate command handler.
 */
static void main(String[] args, Runtime runtime) {
    final ParsedFlags flags;
    try {
        flags = new FlagParser().parse(args);
    } catch (FlagParser.FlagParseException e) {
        System.err.println("Error while parsing the flags: " + e.getMessage());
        runtime.exit(1);
        return;
    }
    Optional<String> command = flags.getMainCommand();
    if (!command.isPresent()) {
        System.err.println("Error: You have to specify a command.");
        help();
        runtime.exit(1);
        return;
    }
    try {
        switch(command.get()) {
            case BuildBundleCommand.COMMAND_NAME:
                BuildBundleCommand.fromFlags(flags).execute();
                break;
            case BuildApksCommand.COMMAND_NAME:
                try (AdbServer adbServer = DdmlibAdbServer.getInstance()) {
                    BuildApksCommand.fromFlags(flags, adbServer).execute();
                }
                break;
            case ExtractApksCommand.COMMAND_NAME:
                ExtractApksCommand.fromFlags(flags).execute();
                break;
            case GetDeviceSpecCommand.COMMAND_NAME:
                // We have to destroy ddmlib resources at the end of the command.
                try (AdbServer adbServer = DdmlibAdbServer.getInstance()) {
                    GetDeviceSpecCommand.fromFlags(flags, adbServer).execute();
                }
                break;
            case InstallApksCommand.COMMAND_NAME:
                try (AdbServer adbServer = DdmlibAdbServer.getInstance()) {
                    InstallApksCommand.fromFlags(flags, adbServer).execute();
                }
                break;
            case InstallMultiApksCommand.COMMAND_NAME:
                try (AdbServer adbServer = DdmlibAdbServer.getInstance()) {
                    InstallMultiApksCommand.fromFlags(flags, adbServer).execute();
                }
                break;
            case ValidateBundleCommand.COMMAND_NAME:
                ValidateBundleCommand.fromFlags(flags).execute();
                break;
            case DumpCommand.COMMAND_NAME:
                DumpCommand.fromFlags(flags).execute();
                break;
            case GetSizeCommand.COMMAND_NAME:
                GetSizeCommand.fromFlags(flags).execute();
                break;
            case VersionCommand.COMMAND_NAME:
                VersionCommand.fromFlags(flags, System.out).execute();
                break;
            case AddTransparencyCommand.COMMAND_NAME:
                AddTransparencyCommand.fromFlags(flags).execute();
                break;
            case CheckTransparencyCommand.COMMAND_NAME:
                try (AdbServer adbServer = DdmlibAdbServer.getInstance()) {
                    CheckTransparencyCommand.fromFlags(flags, adbServer).execute();
                }
                break;
            case HELP_CMD:
                if (flags.getSubCommand().isPresent()) {
                    help(flags.getSubCommand().get(), runtime);
                } else {
                    help();
                }
                break;
            default:
                System.err.printf("Error: Unrecognized command '%s'.%n%n%n", command.get());
                help();
                runtime.exit(1);
                return;
        }
    } catch (Exception e) {
        System.err.println("[BT:" + BundleToolVersion.getCurrentVersion() + "] Error: " + e.getMessage());
        e.printStackTrace();
        runtime.exit(1);
        return;
    }
    // Takes care of shutting down non-daemon threads in internal thread pools.
    runtime.exit(0);
}
Also used : ParsedFlags(com.android.tools.build.bundletool.flags.ParsedFlags) FlagParser(com.android.tools.build.bundletool.flags.FlagParser) DdmlibAdbServer(com.android.tools.build.bundletool.device.DdmlibAdbServer) AdbServer(com.android.tools.build.bundletool.device.AdbServer)

Aggregations

AdbServer (com.android.tools.build.bundletool.device.AdbServer)25 FakeAdbServer (com.android.tools.build.bundletool.testing.FakeAdbServer)20 Test (org.junit.Test)20 Path (java.nio.file.Path)19 ZipPath (com.android.tools.build.bundletool.model.ZipPath)17 FakeDevice (com.android.tools.build.bundletool.testing.FakeDevice)17 BuildApksResult (com.android.bundle.Commands.BuildApksResult)13 ArrayList (java.util.ArrayList)10 DeviceSpec (com.android.bundle.Devices.DeviceSpec)9 Theory (org.junit.experimental.theories.Theory)9 DeviceAnalyzer (com.android.tools.build.bundletool.device.DeviceAnalyzer)4 ParsedFlags (com.android.tools.build.bundletool.flags.ParsedFlags)2 TempDirectory (com.android.tools.build.bundletool.io.TempDirectory)2 BundleConfig (com.android.bundle.Config.BundleConfig)1 Aapt2Command (com.android.tools.build.bundletool.androidtools.Aapt2Command)1 AdbCommand (com.android.tools.build.bundletool.androidtools.AdbCommand)1 ApkBuildMode (com.android.tools.build.bundletool.commands.BuildApksCommand.ApkBuildMode)1 CommandDescription (com.android.tools.build.bundletool.commands.CommandHelp.CommandDescription)1 FlagDescription (com.android.tools.build.bundletool.commands.CommandHelp.FlagDescription)1 ANDROID_SERIAL_VARIABLE (com.android.tools.build.bundletool.commands.CommandUtils.ANDROID_SERIAL_VARIABLE)1