Search in sources :

Example 1 with GeneratedApk

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

the class ExtractApksCommand method extractMatchedApksFromApksArchive.

private ImmutableList<Path> extractMatchedApksFromApksArchive(ImmutableList<GeneratedApk> generatedApks, BuildApksResult toc) {
    Path outputDirectoryPath = getOutputDirectory().orElseGet(ExtractApksCommand::createTempDirectory);
    getOutputDirectory().ifPresent(dir -> {
        if (!Files.exists(dir)) {
            logger.info("Output directory '" + dir + "' does not exist, creating it.");
            FileUtils.createDirectories(dir);
        }
    });
    ImmutableList.Builder<Path> builder = ImmutableList.builder();
    try (ZipFile apksArchive = new ZipFile(getApksArchivePath().toFile())) {
        for (GeneratedApk matchedApk : generatedApks) {
            ZipEntry entry = apksArchive.getEntry(matchedApk.getPath().toString());
            checkNotNull(entry);
            Path extractedApkPath = outputDirectoryPath.resolve(matchedApk.getPath().getFileName().toString());
            try (InputStream inputStream = apksArchive.getInputStream(entry);
                OutputStream outputApk = Files.newOutputStream(extractedApkPath)) {
                ByteStreams.copy(inputStream, outputApk);
                builder.add(extractedApkPath);
            } catch (IOException e) {
                throw new UncheckedIOException(String.format("Error while extracting APK '%s' from the APK Set.", matchedApk), e);
            }
        }
        if (getIncludeMetadata()) {
            produceCommandMetadata(generatedApks, toc, outputDirectoryPath);
        }
    } catch (IOException e) {
        throw new UncheckedIOException(String.format("Error while processing the APK Set archive '%s'.", getApksArchivePath()), e);
    }
    System.err.printf("The APKs have been extracted in the directory: %s%n", outputDirectoryPath.toString());
    return builder.build();
}
Also used : Path(java.nio.file.Path) LocalTestingPathResolver.resolveLocalTestingPath(com.android.tools.build.bundletool.device.LocalTestingPathResolver.resolveLocalTestingPath) ZipFile(java.util.zip.ZipFile) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) OutputStream(java.io.OutputStream) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) GeneratedApk(com.android.tools.build.bundletool.device.ApkMatcher.GeneratedApk)

Example 2 with GeneratedApk

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

the class ApkMatcherTest method apkMatch_withModuleNameFiltering_splitApks_permanentlyMergedModule.

@Test
public void apkMatch_withModuleNameFiltering_splitApks_permanentlyMergedModule() {
    DeviceSpec device = deviceWithSdk(21);
    ZipPath apk = ZipPath.create("master-de-fr.apk");
    BuildApksResult buildApksResult = buildApksResult(createVariant(VariantTargeting.getDefaultInstance(), splitApkSet(/* moduleName= */
    "base", splitApkDescription(ApkTargeting.getDefaultInstance(), apk)))).toBuilder().addPermanentlyFusedModules(PermanentlyFusedModule.newBuilder().setName("my-module")).build();
    ImmutableList<GeneratedApk> matchedApks = createMatcher(device, Optional.of(ImmutableSet.of("my-module"))).getMatchingApks(buildApksResult);
    assertThat(matchedApks).containsExactly(baseMatchedApk(apk));
}
Also used : DeviceSpec(com.android.bundle.Devices.DeviceSpec) BuildApksResult(com.android.bundle.Commands.BuildApksResult) ZipPath(com.android.tools.build.bundletool.model.ZipPath) GeneratedApk(com.android.tools.build.bundletool.device.ApkMatcher.GeneratedApk) Test(org.junit.Test)

Example 3 with GeneratedApk

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

the class ExtractApksCommand method execute.

@VisibleForTesting
ImmutableList<Path> execute(PrintStream output) {
    validateInput();
    BuildApksResult toc = ResultUtils.readTableOfContents(getApksArchivePath());
    Optional<ImmutableSet<String>> requestedModuleNames = getModules().map(modules -> resolveRequestedModules(modules, toc));
    DeviceSpec deviceSpec = applyDefaultsToDeviceSpec(getDeviceSpec(), toc);
    ApkMatcher apkMatcher = new ApkMatcher(deviceSpec, requestedModuleNames, getInstant(), /* ensureDensityAndAbiApksMatched= */
    true);
    ImmutableList<GeneratedApk> generatedApks = apkMatcher.getMatchingApks(toc);
    if (generatedApks.isEmpty()) {
        throw IncompatibleDeviceException.builder().withUserMessage("No compatible APKs found for the device.").build();
    }
    if (Files.isDirectory(getApksArchivePath())) {
        return generatedApks.stream().map(matchedApk -> getApksArchivePath().resolve(matchedApk.getPath().toString())).collect(toImmutableList());
    } else {
        return extractMatchedApksFromApksArchive(generatedApks, toc);
    }
}
Also used : DeviceSpec(com.android.bundle.Devices.DeviceSpec) Value(com.android.bundle.Config.SplitDimension.Value) DeviceSpec(com.android.bundle.Devices.DeviceSpec) FilePreconditions.checkDirectoryExists(com.android.tools.build.bundletool.model.utils.files.FilePreconditions.checkDirectoryExists) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ExtractedApk(com.android.bundle.Commands.ExtractedApk) Flag(com.android.tools.build.bundletool.flags.Flag) ZipFile(java.util.zip.ZipFile) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) LocalTestingPathResolver.resolveLocalTestingPath(com.android.tools.build.bundletool.device.LocalTestingPathResolver.resolveLocalTestingPath) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ParsedFlags(com.android.tools.build.bundletool.flags.ParsedFlags) Logger(java.util.logging.Logger) UncheckedIOException(java.io.UncheckedIOException) DefaultTargetingValue(com.android.bundle.Commands.DefaultTargetingValue) Stream(java.util.stream.Stream) JsonFormat(com.google.protobuf.util.JsonFormat) ExtractApksResult(com.android.bundle.Commands.ExtractApksResult) FileNames(com.android.tools.build.bundletool.model.utils.FileNames) AutoValue(com.google.auto.value.AutoValue) ByteStreams(com.google.common.io.ByteStreams) Optional(java.util.Optional) LocalTestingInfoForMetadata(com.android.bundle.Commands.LocalTestingInfoForMetadata) FilePreconditions.checkFileExistsAndReadable(com.android.tools.build.bundletool.model.utils.files.FilePreconditions.checkFileExistsAndReadable) BuildApksResult(com.android.bundle.Commands.BuildApksResult) GeneratedApk(com.android.tools.build.bundletool.device.ApkMatcher.GeneratedApk) ResultUtils(com.android.tools.build.bundletool.model.utils.ResultUtils) ImmutableList(com.google.common.collect.ImmutableList) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ApkMatcher(com.android.tools.build.bundletool.device.ApkMatcher) OutputStream(java.io.OutputStream) PrintStream(java.io.PrintStream) MoreCollectors.toOptional(com.google.common.collect.MoreCollectors.toOptional) Int32Value(com.google.protobuf.Int32Value) DeviceSpecParser(com.android.tools.build.bundletool.device.DeviceSpecParser) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) AssetSliceSet(com.android.bundle.Commands.AssetSliceSet) IOException(java.io.IOException) AssetModuleMetadata(com.android.bundle.Commands.AssetModuleMetadata) 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) VisibleForTesting(com.google.common.annotations.VisibleForTesting) FileUtils(com.android.tools.build.bundletool.model.utils.files.FileUtils) CommandDescription(com.android.tools.build.bundletool.commands.CommandHelp.CommandDescription) InputStream(java.io.InputStream) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) BuildApksResult(com.android.bundle.Commands.BuildApksResult) ApkMatcher(com.android.tools.build.bundletool.device.ApkMatcher) GeneratedApk(com.android.tools.build.bundletool.device.ApkMatcher.GeneratedApk) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with GeneratedApk

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

the class ExtractApksCommand method produceCommandMetadata.

private static void produceCommandMetadata(ImmutableList<GeneratedApk> generatedApks, BuildApksResult toc, Path outputDir) {
    ImmutableList<ExtractedApk> apks = generatedApks.stream().map(apk -> ExtractedApk.newBuilder().setPath(apk.getPath().getFileName().toString()).setModuleName(apk.getModuleName()).setDeliveryType(apk.getDeliveryType()).build()).collect(toImmutableList());
    try {
        JsonFormat.Printer printer = JsonFormat.printer();
        ExtractApksResult.Builder builder = ExtractApksResult.newBuilder();
        if (toc.getLocalTestingInfo().getEnabled()) {
            builder.setLocalTestingInfo(createLocalTestingInfo(toc));
        }
        String metadata = printer.print(builder.addAllApks(apks).build());
        Files.write(outputDir.resolve(METADATA_FILE), metadata.getBytes(UTF_8));
    } catch (IOException e) {
        throw new UncheckedIOException("Error while writing metadata.json.", e);
    }
}
Also used : Value(com.android.bundle.Config.SplitDimension.Value) DeviceSpec(com.android.bundle.Devices.DeviceSpec) FilePreconditions.checkDirectoryExists(com.android.tools.build.bundletool.model.utils.files.FilePreconditions.checkDirectoryExists) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ExtractedApk(com.android.bundle.Commands.ExtractedApk) Flag(com.android.tools.build.bundletool.flags.Flag) ZipFile(java.util.zip.ZipFile) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) LocalTestingPathResolver.resolveLocalTestingPath(com.android.tools.build.bundletool.device.LocalTestingPathResolver.resolveLocalTestingPath) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ParsedFlags(com.android.tools.build.bundletool.flags.ParsedFlags) Logger(java.util.logging.Logger) UncheckedIOException(java.io.UncheckedIOException) DefaultTargetingValue(com.android.bundle.Commands.DefaultTargetingValue) Stream(java.util.stream.Stream) JsonFormat(com.google.protobuf.util.JsonFormat) ExtractApksResult(com.android.bundle.Commands.ExtractApksResult) FileNames(com.android.tools.build.bundletool.model.utils.FileNames) AutoValue(com.google.auto.value.AutoValue) ByteStreams(com.google.common.io.ByteStreams) Optional(java.util.Optional) LocalTestingInfoForMetadata(com.android.bundle.Commands.LocalTestingInfoForMetadata) FilePreconditions.checkFileExistsAndReadable(com.android.tools.build.bundletool.model.utils.files.FilePreconditions.checkFileExistsAndReadable) BuildApksResult(com.android.bundle.Commands.BuildApksResult) GeneratedApk(com.android.tools.build.bundletool.device.ApkMatcher.GeneratedApk) ResultUtils(com.android.tools.build.bundletool.model.utils.ResultUtils) ImmutableList(com.google.common.collect.ImmutableList) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ApkMatcher(com.android.tools.build.bundletool.device.ApkMatcher) OutputStream(java.io.OutputStream) PrintStream(java.io.PrintStream) MoreCollectors.toOptional(com.google.common.collect.MoreCollectors.toOptional) Int32Value(com.google.protobuf.Int32Value) DeviceSpecParser(com.android.tools.build.bundletool.device.DeviceSpecParser) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) AssetSliceSet(com.android.bundle.Commands.AssetSliceSet) IOException(java.io.IOException) AssetModuleMetadata(com.android.bundle.Commands.AssetModuleMetadata) 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) VisibleForTesting(com.google.common.annotations.VisibleForTesting) FileUtils(com.android.tools.build.bundletool.model.utils.files.FileUtils) CommandDescription(com.android.tools.build.bundletool.commands.CommandHelp.CommandDescription) InputStream(java.io.InputStream) ExtractApksResult(com.android.bundle.Commands.ExtractApksResult) JsonFormat(com.google.protobuf.util.JsonFormat) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) ExtractedApk(com.android.bundle.Commands.ExtractedApk)

Aggregations

GeneratedApk (com.android.tools.build.bundletool.device.ApkMatcher.GeneratedApk)4 BuildApksResult (com.android.bundle.Commands.BuildApksResult)3 DeviceSpec (com.android.bundle.Devices.DeviceSpec)3 LocalTestingPathResolver.resolveLocalTestingPath (com.android.tools.build.bundletool.device.LocalTestingPathResolver.resolveLocalTestingPath)3 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 AssetModuleMetadata (com.android.bundle.Commands.AssetModuleMetadata)2 AssetSliceSet (com.android.bundle.Commands.AssetSliceSet)2 DefaultTargetingValue (com.android.bundle.Commands.DefaultTargetingValue)2 ExtractApksResult (com.android.bundle.Commands.ExtractApksResult)2 ExtractedApk (com.android.bundle.Commands.ExtractedApk)2 LocalTestingInfoForMetadata (com.android.bundle.Commands.LocalTestingInfoForMetadata)2 Value (com.android.bundle.Config.SplitDimension.Value)2 CommandDescription (com.android.tools.build.bundletool.commands.CommandHelp.CommandDescription)2 FlagDescription (com.android.tools.build.bundletool.commands.CommandHelp.FlagDescription)2 ApkMatcher (com.android.tools.build.bundletool.device.ApkMatcher)2 DeviceSpecParser (com.android.tools.build.bundletool.device.DeviceSpecParser)2