Search in sources :

Example 1 with ApkMatcher

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

the class BuildApksManager method checkDeviceCompatibilityWithBundle.

private static void checkDeviceCompatibilityWithBundle(GeneratedApks generatedApks, DeviceSpec deviceSpec) {
    ApkMatcher apkMatcher = new ApkMatcher(deviceSpec);
    generatedApks.getAllApksStream().forEach(apkMatcher::checkCompatibleWithApkTargeting);
}
Also used : ApkMatcher(com.android.tools.build.bundletool.device.ApkMatcher)

Example 2 with ApkMatcher

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

the class ApkSerializerManager method serializeApks.

@VisibleForTesting
ImmutableList<Variant> serializeApks(Path outputDirectory, GeneratedApks generatedApks, Optional<DeviceSpec> deviceSpec) {
    validateInput(generatedApks, apkBuildMode);
    // Running with system APK mode generates a fused APK and additional unmatched language splits.
    // To avoid filtering of unmatched language splits we skip device filtering for system mode.
    Predicate<ModuleSplit> deviceFilter = deviceSpec.isPresent() && !apkBuildMode.equals(SYSTEM) ? new ApkMatcher(addDefaultDeviceTierIfNecessary(deviceSpec.get()))::matchesModuleSplitByTargeting : alwaysTrue();
    ImmutableListMultimap<VariantKey, ModuleSplit> splitsByVariant = generatedApks.getAllApksGroupedByOrderedVariants();
    // Assign the variant numbers to each variant present.
    AtomicInteger variantNumberCounter = new AtomicInteger(firstVariantNumber);
    ImmutableMap<VariantKey, Integer> variantNumberByVariantKey = splitsByVariant.keySet().stream().collect(toImmutableMap(identity(), unused -> variantNumberCounter.getAndIncrement()));
    // 1. Remove APKs not matching the device spec.
    // 2. Modify the APKs based on the ApkModifier.
    // 3. Serialize all APKs in parallel.
    // Modifies the APK using APK modifier, then returns a map by extracting the variant
    // of APK first and later clearing out its variant targeting.
    ImmutableListMultimap<VariantKey, ModuleSplit> finalSplitsByVariant = splitsByVariant.entries().stream().filter(keyModuleSplitEntry -> deviceFilter.test(keyModuleSplitEntry.getValue())).collect(groupingBySortedKeys(Entry::getKey, entry -> clearVariantTargeting(modifyApk(entry.getValue(), variantNumberByVariantKey.get(entry.getKey())))));
    // After variant targeting of APKs are cleared, there might be duplicate APKs
    // which are removed and the distinct APKs are then serialized in parallel.
    ImmutableBiMap<ZipPath, ModuleSplit> splitsByRelativePath = finalSplitsByVariant.values().stream().distinct().collect(toImmutableBiMap(apkPathManager::getApkPath, identity()));
    ImmutableMap<ZipPath, ApkDescription> apkDescriptionsByRelativePath = apkSerializer.serialize(outputDirectory, splitsByRelativePath);
    // Build the result proto.
    ImmutableList.Builder<Variant> variants = ImmutableList.builder();
    for (VariantKey variantKey : finalSplitsByVariant.keySet()) {
        Variant.Builder variant = Variant.newBuilder().setVariantNumber(variantNumberByVariantKey.get(variantKey)).setTargeting(variantKey.getVariantTargeting());
        Multimap<BundleModuleName, ModuleSplit> splitsByModuleName = finalSplitsByVariant.get(variantKey).stream().collect(groupingBySortedKeys(ModuleSplit::getModuleName));
        for (BundleModuleName moduleName : splitsByModuleName.keySet()) {
            variant.addApkSet(ApkSet.newBuilder().setModuleMetadata(bundle.getModule(moduleName).getModuleMetadata()).addAllApkDescription(splitsByModuleName.get(moduleName).stream().map(split -> splitsByRelativePath.inverse().get(split)).map(apkDescriptionsByRelativePath::get).collect(toImmutableList())));
        }
        variants.add(variant.build());
    }
    return variants.build();
}
Also used : SYSTEM(com.android.tools.build.bundletool.commands.BuildApksCommand.ApkBuildMode.SYSTEM) FirstVariantNumber(com.android.tools.build.bundletool.commands.BuildApksModule.FirstVariantNumber) Variant(com.android.bundle.Commands.Variant) AndroidManifest(com.android.tools.build.bundletool.model.AndroidManifest) DeviceSpec(com.android.bundle.Devices.DeviceSpec) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Predicates.alwaysTrue(com.google.common.base.Predicates.alwaysTrue) DeliveryType(com.android.bundle.Commands.DeliveryType) Bundletool(com.android.bundle.Config.Bundletool) SdkBundle(com.android.tools.build.bundletool.model.SdkBundle) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssetModulesConfig(com.android.bundle.Config.AssetModulesConfig) Path(java.nio.file.Path) BundleToolVersion(com.android.tools.build.bundletool.model.version.BundleToolVersion) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ManifestDeliveryElement(com.android.tools.build.bundletool.model.ManifestDeliveryElement) ApkOptimizations(com.android.tools.build.bundletool.optimizations.ApkOptimizations) ApkType(com.android.tools.build.bundletool.model.ApkModifier.ApkDescription.ApkType) InstantMetadata(com.android.bundle.Commands.InstantMetadata) SplitType(com.android.tools.build.bundletool.model.ModuleSplit.SplitType) GeneratedApks(com.android.tools.build.bundletool.model.GeneratedApks) UncheckedIOException(java.io.UncheckedIOException) DefaultTargetingValue(com.android.bundle.Commands.DefaultTargetingValue) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ApkSet(com.android.bundle.Commands.ApkSet) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) BundleConfig(com.android.bundle.Config.BundleConfig) PermanentlyFusedModule(com.android.bundle.Commands.PermanentlyFusedModule) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) Function.identity(java.util.function.Function.identity) Entry(java.util.Map.Entry) Optional(java.util.Optional) Bundle(com.android.tools.build.bundletool.model.Bundle) BundleModuleName(com.android.tools.build.bundletool.model.BundleModuleName) ZipPath(com.android.tools.build.bundletool.model.ZipPath) OptimizationDimension(com.android.tools.build.bundletool.model.OptimizationDimension) VariantKey(com.android.tools.build.bundletool.model.VariantKey) CollectorUtils.groupingBySortedKeys(com.android.tools.build.bundletool.model.utils.CollectorUtils.groupingBySortedKeys) ApkModifier(com.android.tools.build.bundletool.model.ApkModifier) BuildApksResult(com.android.bundle.Commands.BuildApksResult) Multimap(com.google.common.collect.Multimap) CollectorUtils.groupingByDeterministic(com.android.tools.build.bundletool.model.utils.CollectorUtils.groupingByDeterministic) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) Collectors.mapping(java.util.stream.Collectors.mapping) SdkVersionInformation(com.android.bundle.Commands.SdkVersionInformation) ApkDescription(com.android.bundle.Commands.ApkDescription) BuildSdkApksResult(com.android.bundle.Commands.BuildSdkApksResult) ApkMatcher(com.android.tools.build.bundletool.device.ApkMatcher) VariantTargeting(com.android.bundle.Targeting.VariantTargeting) SuffixStripping(com.android.bundle.Config.SuffixStripping) Int32Value(com.google.protobuf.Int32Value) AssetSliceSet(com.android.bundle.Commands.AssetSliceSet) IOException(java.io.IOException) AssetModuleMetadata(com.android.bundle.Commands.AssetModuleMetadata) VerboseLogs(com.android.tools.build.bundletool.commands.BuildApksModule.VerboseLogs) AssetModulesInfo(com.android.bundle.Commands.AssetModulesInfo) LocalTestingInfo(com.android.bundle.Commands.LocalTestingInfo) ApkBuildMode(com.android.tools.build.bundletool.commands.BuildApksCommand.ApkBuildMode) GeneratedAssetSlices(com.android.tools.build.bundletool.model.GeneratedAssetSlices) SplitDimension(com.android.bundle.Config.SplitDimension) ImmutableBiMap.toImmutableBiMap(com.google.common.collect.ImmutableBiMap.toImmutableBiMap) VisibleForTesting(com.google.common.annotations.VisibleForTesting) BundleModule(com.android.tools.build.bundletool.model.BundleModule) VariantKey(com.android.tools.build.bundletool.model.VariantKey) ApkDescription(com.android.bundle.Commands.ApkDescription) BundleModuleName(com.android.tools.build.bundletool.model.BundleModuleName) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) ZipPath(com.android.tools.build.bundletool.model.ZipPath) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Variant(com.android.bundle.Commands.Variant) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ApkMatcher(com.android.tools.build.bundletool.device.ApkMatcher) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with ApkMatcher

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

Aggregations

ApkMatcher (com.android.tools.build.bundletool.device.ApkMatcher)3 AssetModuleMetadata (com.android.bundle.Commands.AssetModuleMetadata)2 AssetSliceSet (com.android.bundle.Commands.AssetSliceSet)2 BuildApksResult (com.android.bundle.Commands.BuildApksResult)2 DefaultTargetingValue (com.android.bundle.Commands.DefaultTargetingValue)2 DeviceSpec (com.android.bundle.Devices.DeviceSpec)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Int32Value (com.google.protobuf.Int32Value)2 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 Path (java.nio.file.Path)2 Optional (java.util.Optional)2 ApkDescription (com.android.bundle.Commands.ApkDescription)1 ApkSet (com.android.bundle.Commands.ApkSet)1 AssetModulesInfo (com.android.bundle.Commands.AssetModulesInfo)1 BuildSdkApksResult (com.android.bundle.Commands.BuildSdkApksResult)1