Search in sources :

Example 1 with ApkBuildMode

use of com.android.tools.build.bundletool.commands.BuildApksCommand.ApkBuildMode 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)

Aggregations

ApkDescription (com.android.bundle.Commands.ApkDescription)1 ApkSet (com.android.bundle.Commands.ApkSet)1 AssetModuleMetadata (com.android.bundle.Commands.AssetModuleMetadata)1 AssetModulesInfo (com.android.bundle.Commands.AssetModulesInfo)1 AssetSliceSet (com.android.bundle.Commands.AssetSliceSet)1 BuildApksResult (com.android.bundle.Commands.BuildApksResult)1 BuildSdkApksResult (com.android.bundle.Commands.BuildSdkApksResult)1 DefaultTargetingValue (com.android.bundle.Commands.DefaultTargetingValue)1 DeliveryType (com.android.bundle.Commands.DeliveryType)1 InstantMetadata (com.android.bundle.Commands.InstantMetadata)1 LocalTestingInfo (com.android.bundle.Commands.LocalTestingInfo)1 PermanentlyFusedModule (com.android.bundle.Commands.PermanentlyFusedModule)1 SdkVersionInformation (com.android.bundle.Commands.SdkVersionInformation)1 Variant (com.android.bundle.Commands.Variant)1 AssetModulesConfig (com.android.bundle.Config.AssetModulesConfig)1 BundleConfig (com.android.bundle.Config.BundleConfig)1 Bundletool (com.android.bundle.Config.Bundletool)1 SplitDimension (com.android.bundle.Config.SplitDimension)1 SuffixStripping (com.android.bundle.Config.SuffixStripping)1 DeviceSpec (com.android.bundle.Devices.DeviceSpec)1