Search in sources :

Example 1 with DeviceSpec

use of com.android.bundle.Devices.DeviceSpec in project bundletool by google.

the class BuildApksModule method provideDeviceSpec.

@CommandScoped
@Provides
static Optional<DeviceSpec> provideDeviceSpec(BuildApksCommand command) {
    Optional<DeviceSpec> deviceSpec = command.getDeviceSpec();
    if (command.getGenerateOnlyForConnectedDevice()) {
        AdbServer adbServer = command.getAdbServer().get();
        adbServer.init(command.getAdbPath().get());
        deviceSpec = Optional.of(new DeviceAnalyzer(adbServer).getDeviceSpec(command.getDeviceId()));
    }
    if (command.getDeviceTier().isPresent()) {
        // --device-tier can only be specified along with --device-spec or --connected-device, so
        // deviceSpec should always be present in this case.
        checkState(deviceSpec.isPresent(), "Device tier specified but no device was provided.");
        deviceSpec = deviceSpec.map(spec -> spec.toBuilder().setDeviceTier(Int32Value.of(command.getDeviceTier().get())).build());
    }
    return deviceSpec;
}
Also used : DeviceSpec(com.android.bundle.Devices.DeviceSpec) DefaultSigningConfigurationProvider(com.android.tools.build.bundletool.model.DefaultSigningConfigurationProvider) ApkModifier(com.android.tools.build.bundletool.model.ApkModifier) ApkListener(com.android.tools.build.bundletool.model.ApkListener) DeviceSpec(com.android.bundle.Devices.DeviceSpec) SigningConfiguration(com.android.tools.build.bundletool.model.SigningConfiguration) Retention(java.lang.annotation.Retention) DeviceAnalyzer(com.android.tools.build.bundletool.device.DeviceAnalyzer) Module(dagger.Module) Qualifier(javax.inject.Qualifier) ApkSerializerModule(com.android.tools.build.bundletool.io.ApkSerializerModule) OptimizationsMerger(com.android.tools.build.bundletool.optimizations.OptimizationsMerger) Version(com.android.tools.build.bundletool.model.version.Version) AdbServer(com.android.tools.build.bundletool.device.AdbServer) Provides(dagger.Provides) SourceStamp(com.android.tools.build.bundletool.model.SourceStamp) PrintStream(java.io.PrintStream) Int32Value(com.google.protobuf.Int32Value) SigningConfigurationProvider(com.android.tools.build.bundletool.model.SigningConfigurationProvider) ApkOptimizations(com.android.tools.build.bundletool.optimizations.ApkOptimizations) RUNTIME(java.lang.annotation.RetentionPolicy.RUNTIME) Preconditions.checkState(com.google.common.base.Preconditions.checkState) ApkBuildMode(com.android.tools.build.bundletool.commands.BuildApksCommand.ApkBuildMode) BundleConfig(com.android.bundle.Config.BundleConfig) Optional(java.util.Optional) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) DeviceAnalyzer(com.android.tools.build.bundletool.device.DeviceAnalyzer) AdbServer(com.android.tools.build.bundletool.device.AdbServer) Provides(dagger.Provides)

Example 2 with DeviceSpec

use of com.android.bundle.Devices.DeviceSpec in project bundletool by google.

the class GetDeviceSpecCommand method execute.

public DeviceSpec execute() {
    if (!getOverwriteOutput()) {
        FilePreconditions.checkFileDoesNotExist(getOutputPath());
    }
    Path pathToAdb = getAdbPath();
    FilePreconditions.checkFileExistsAndExecutable(pathToAdb);
    AdbServer adb = getAdbServer();
    adb.init(getAdbPath());
    DeviceSpec deviceSpec = new DeviceAnalyzer(adb).getDeviceSpec(getDeviceId());
    if (getDeviceTier().isPresent()) {
        deviceSpec = deviceSpec.toBuilder().setDeviceTier(Int32Value.of(getDeviceTier().get())).build();
    }
    if (getDeviceGroups().isPresent()) {
        deviceSpec = deviceSpec.toBuilder().addAllDeviceGroups(getDeviceGroups().get()).build();
    }
    writeDeviceSpecToFile(deviceSpec, getOutputPath());
    return deviceSpec;
}
Also used : Path(java.nio.file.Path) DeviceSpec(com.android.bundle.Devices.DeviceSpec) DeviceAnalyzer(com.android.tools.build.bundletool.device.DeviceAnalyzer) AdbServer(com.android.tools.build.bundletool.device.AdbServer)

Example 3 with DeviceSpec

use of com.android.bundle.Devices.DeviceSpec in project bundletool by google.

the class InstallApksCommand method execute.

public void execute() {
    BuildApksResult toc = readBuildApksResult();
    validateInput(toc);
    AdbServer adbServer = getAdbServer();
    adbServer.init(getAdbPath());
    try (TempDirectory tempDirectory = new TempDirectory()) {
        DeviceSpec deviceSpec = new DeviceAnalyzer(adbServer).getDeviceSpec(getDeviceId());
        if (getDeviceTier().isPresent()) {
            deviceSpec = deviceSpec.toBuilder().setDeviceTier(Int32Value.of(getDeviceTier().get())).build();
        }
        if (getDeviceGroups().isPresent()) {
            deviceSpec = deviceSpec.toBuilder().addAllDeviceGroups(getDeviceGroups().get()).build();
        }
        final ImmutableList<Path> apksToInstall = getApksToInstall(toc, deviceSpec, tempDirectory.getPath());
        final ImmutableList<Path> filesToPush = ImmutableList.<Path>builder().addAll(getApksToPushToStorage(toc, deviceSpec, tempDirectory.getPath())).addAll(getAdditionalLocalTestingFiles().orElse(ImmutableList.of())).build();
        AdbRunner adbRunner = new AdbRunner(adbServer);
        InstallOptions installOptions = InstallOptions.builder().setAllowDowngrade(getAllowDowngrade()).setAllowTestOnly(getAllowTestOnly()).setTimeout(getTimeout()).build();
        if (getDeviceId().isPresent()) {
            adbRunner.run(device -> device.installApks(apksToInstall, installOptions), getDeviceId().get());
        } else {
            adbRunner.run(device -> device.installApks(apksToInstall, installOptions));
        }
        if (!filesToPush.isEmpty()) {
            pushFiles(filesToPush, toc, adbRunner);
        }
        if (toc.getLocalTestingInfo().getEnabled()) {
            cleanUpEmulatedSplits(adbRunner, toc);
        }
    }
}
Also used : DeviceSpec(com.android.bundle.Devices.DeviceSpec) Path(java.nio.file.Path) TempDirectory(com.android.tools.build.bundletool.io.TempDirectory) BuildApksResult(com.android.bundle.Commands.BuildApksResult) AdbRunner(com.android.tools.build.bundletool.device.AdbRunner) DeviceAnalyzer(com.android.tools.build.bundletool.device.DeviceAnalyzer) InstallOptions(com.android.tools.build.bundletool.device.Device.InstallOptions) AdbServer(com.android.tools.build.bundletool.device.AdbServer)

Example 4 with DeviceSpec

use of com.android.bundle.Devices.DeviceSpec 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 5 with DeviceSpec

use of com.android.bundle.Devices.DeviceSpec in project bundletool by google.

the class BuildApksDeviceSpecTest method deviceSpecL_bundleTargetsMPlus_throws.

@Test
public void deviceSpecL_bundleTargetsMPlus_throws() throws Exception {
    DeviceSpec deviceSpec = mergeSpecs(sdkVersion(/* Lollipop */
    21), abis("x86"), locales("en-US"), density(XHDPI));
    bundleSerializer.writeToDisk(createMinSdkBundle(/* Marshmallow */
    23), bundlePath);
    BuildApksCommand command = BuildApksCommand.builder().setBundlePath(bundlePath).setOutputFile(outputFilePath).setDeviceSpec(deviceSpec).build();
    Throwable exception = assertThrows(IncompatibleDeviceException.class, command::execute);
    assertThat(exception).hasMessageThat().contains("");
}
Also used : DeviceSpec(com.android.bundle.Devices.DeviceSpec) Test(org.junit.Test)

Aggregations

DeviceSpec (com.android.bundle.Devices.DeviceSpec)124 Test (org.junit.Test)113 Path (java.nio.file.Path)71 BuildApksResult (com.android.bundle.Commands.BuildApksResult)68 ZipPath (com.android.tools.build.bundletool.model.ZipPath)64 FakeAdbServer (com.android.tools.build.bundletool.testing.FakeAdbServer)19 FlagParser (com.android.tools.build.bundletool.flags.FlagParser)18 AdbServer (com.android.tools.build.bundletool.device.AdbServer)15 ImmutableSet (com.google.common.collect.ImmutableSet)14 Theory (org.junit.experimental.theories.Theory)13 IncompatibleDeviceException (com.android.tools.build.bundletool.model.exceptions.IncompatibleDeviceException)12 Variant (com.android.bundle.Commands.Variant)11 InvalidCommandException (com.android.tools.build.bundletool.model.exceptions.InvalidCommandException)11 ZipFile (java.util.zip.ZipFile)11 ApkSet (com.android.bundle.Commands.ApkSet)10 ImmutableList (com.google.common.collect.ImmutableList)9 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)9 ImmutableMap (com.google.common.collect.ImmutableMap)8 SystemEnvironmentProvider (com.android.tools.build.bundletool.model.utils.SystemEnvironmentProvider)7 PrintStream (java.io.PrintStream)7