Search in sources :

Example 1 with NativeDirectoryTargeting

use of com.android.bundle.Targeting.NativeDirectoryTargeting in project bundletool by google.

the class AbiNativeLibrariesSplitter method split.

/**
 * Generates {@link ModuleSplit} objects dividing the native libraries by ABI.
 */
@Override
public ImmutableCollection<ModuleSplit> split(ModuleSplit moduleSplit) {
    if (!moduleSplit.getNativeConfig().isPresent()) {
        return ImmutableList.of(moduleSplit);
    }
    ImmutableList.Builder<ModuleSplit> splits = new ImmutableList.Builder<>();
    // Flatten all targeted directories.
    List<TargetedNativeDirectory> allTargetedDirectories = moduleSplit.getNativeConfig().get().getDirectoryList();
    // Currently we only support targeting via ABI, so grouping it by Targeting.equals() should be
    // enough.
    ImmutableMultimap<NativeDirectoryTargeting, TargetedNativeDirectory> targetingMap = Multimaps.index(allTargetedDirectories, TargetedNativeDirectory::getTargeting);
    // We need to know the exact set of ABIs that we will generate, to set alternatives correctly.
    ImmutableSet<Abi> abisToGenerate = targetingMap.keySet().stream().map(NativeDirectoryTargeting::getAbi).collect(toImmutableSet());
    // Any entries not claimed by the ABI splits will be returned in a separate split using the
    // original targeting.
    HashSet<ModuleEntry> leftOverEntries = new HashSet<>(moduleSplit.getEntries());
    for (NativeDirectoryTargeting targeting : targetingMap.keySet()) {
        ImmutableList<ModuleEntry> entriesList = targetingMap.get(targeting).stream().flatMap(directory -> moduleSplit.findEntriesUnderPath(directory.getPath())).collect(toImmutableList());
        ModuleSplit.Builder splitBuilder = moduleSplit.toBuilder().setApkTargeting(moduleSplit.getApkTargeting().toBuilder().setAbiTargeting(AbiTargeting.newBuilder().addValue(targeting.getAbi()).addAllAlternatives(Sets.difference(abisToGenerate, ImmutableSet.of(targeting.getAbi())))).build()).setMasterSplit(false).addMasterManifestMutator(withSplitsRequired(true)).setEntries(entriesList);
        splits.add(splitBuilder.build());
        leftOverEntries.removeAll(entriesList);
    }
    if (!leftOverEntries.isEmpty()) {
        splits.add(moduleSplit.toBuilder().setEntries(ImmutableList.copyOf(leftOverEntries)).build());
    }
    return splits.build();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) AbiTargeting(com.android.bundle.Targeting.AbiTargeting) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableCollection(com.google.common.collect.ImmutableCollection) Sets(com.google.common.collect.Sets) Multimaps(com.google.common.collect.Multimaps) HashSet(java.util.HashSet) ManifestMutator.withSplitsRequired(com.android.tools.build.bundletool.model.ManifestMutator.withSplitsRequired) List(java.util.List) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) ImmutableList(com.google.common.collect.ImmutableList) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) TargetedNativeDirectory(com.android.bundle.Files.TargetedNativeDirectory) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) NativeDirectoryTargeting(com.android.bundle.Targeting.NativeDirectoryTargeting) Abi(com.android.bundle.Targeting.Abi) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) Abi(com.android.bundle.Targeting.Abi) NativeDirectoryTargeting(com.android.bundle.Targeting.NativeDirectoryTargeting) TargetedNativeDirectory(com.android.bundle.Files.TargetedNativeDirectory) HashSet(java.util.HashSet)

Example 2 with NativeDirectoryTargeting

use of com.android.bundle.Targeting.NativeDirectoryTargeting in project bundletool by google.

the class NativeTargetingValidator method validateTargeting.

private static void validateTargeting(BundleModule module, NativeLibraries nativeLibraries) {
    for (TargetedNativeDirectory targetedDirectory : nativeLibraries.getDirectoryList()) {
        ZipPath path = ZipPath.create(targetedDirectory.getPath());
        NativeDirectoryTargeting targeting = targetedDirectory.getTargeting();
        if (!targeting.hasAbi()) {
            throw InvalidBundleException.builder().withUserMessage("Targeted native directory '%s' does not have the ABI dimension set.", targetedDirectory.getPath()).build();
        }
        if (!path.startsWith(LIB_DIRECTORY) || path.getNameCount() != 2) {
            throw InvalidBundleException.builder().withUserMessage("Path of targeted native directory must be in format 'lib/<directory>' but " + "found '%s'.", path).build();
        }
        if (BundleValidationUtils.directoryContainsNoFiles(module, path)) {
            throw InvalidBundleException.builder().withUserMessage("Targeted directory '%s' is empty.", path).build();
        }
    }
    SetView<String> libDirsWithoutTargeting = Sets.difference(module.findEntriesUnderPath(LIB_DIRECTORY).map(libFile -> libFile.getPath().subpath(0, 2).toString()).collect(toImmutableSet()), nativeLibraries.getDirectoryList().stream().map(TargetedNativeDirectory::getPath).collect(toImmutableSet()));
    if (!libDirsWithoutTargeting.isEmpty()) {
        throw InvalidBundleException.builder().withUserMessage("Following native directories are not targeted: %s", libDirsWithoutTargeting).build();
    }
}
Also used : NativeDirectoryTargeting(com.android.bundle.Targeting.NativeDirectoryTargeting) TargetedNativeDirectory(com.android.bundle.Files.TargetedNativeDirectory) ZipPath(com.android.tools.build.bundletool.model.ZipPath)

Aggregations

TargetedNativeDirectory (com.android.bundle.Files.TargetedNativeDirectory)2 NativeDirectoryTargeting (com.android.bundle.Targeting.NativeDirectoryTargeting)2 Abi (com.android.bundle.Targeting.Abi)1 AbiTargeting (com.android.bundle.Targeting.AbiTargeting)1 ManifestMutator.withSplitsRequired (com.android.tools.build.bundletool.model.ManifestMutator.withSplitsRequired)1 ModuleEntry (com.android.tools.build.bundletool.model.ModuleEntry)1 ModuleSplit (com.android.tools.build.bundletool.model.ModuleSplit)1 ZipPath (com.android.tools.build.bundletool.model.ZipPath)1 ImmutableCollection (com.google.common.collect.ImmutableCollection)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 Multimaps (com.google.common.collect.Multimaps)1 Sets (com.google.common.collect.Sets)1 HashSet (java.util.HashSet)1 List (java.util.List)1