Search in sources :

Example 11 with Assets

use of com.android.bundle.Files.Assets in project bundletool by google.

the class TargetingGeneratorTest method generateTargetingForAssets_nameOverloading_works.

@Test
public void generateTargetingForAssets_nameOverloading_works() throws Exception {
    Assets assetsConfig = new TargetingGenerator().generateTargetingForAssets(ImmutableList.of(ZipPath.create("assets/world/texture#tcf_etc1/i18n#lang_en"), ZipPath.create("assets/world/texture#tcf_etc1/i18n#lang_ru")));
    assertThat(assetsConfig).ignoringRepeatedFieldOrder().isEqualTo(Assets.newBuilder().addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/world/texture#tcf_etc1/i18n#lang_en").setTargeting(mergeAssetsTargeting(assetsDirectoryTargeting(textureCompressionTargeting(TextureCompressionFormatAlias.ETC1_RGB8)), assetsDirectoryTargeting(languageTargeting("en"))))).addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/world/texture#tcf_etc1/i18n#lang_ru").setTargeting(mergeAssetsTargeting(assetsDirectoryTargeting(textureCompressionTargeting(TextureCompressionFormatAlias.ETC1_RGB8)), assetsDirectoryTargeting(languageTargeting("ru"))))).build());
}
Also used : Assets(com.android.bundle.Files.Assets) Test(org.junit.Test)

Example 12 with Assets

use of com.android.bundle.Files.Assets in project bundletool by google.

the class AssetsDimensionSplitterFactory method createSplitter.

/**
 * Creates a {@link ModuleSplitSplitter} capable of splitting on a given Asset targeting
 * dimension, with, optionally, a dimension to be removed from asset paths.
 *
 * @param <T> the proto buffer message class representing the splitting targeting dimension.
 * @param dimensionGetter function that extracts the sub-message representing a targeting
 *     dimension.
 * @param targetingSetter function that creates a split targeting that will be merged with the
 *     targeting of the input {@link ModuleSplit}.
 * @param hasTargeting predicate to test if the input {@link ModuleSplit} is already targeting on
 *     the dimension of this splitter.
 * @param targetingDimensionToRemove If not empty, the targeting for this dimension will be
 * removed from asset paths (i.e: suffixes like #tcf_xxx will be removed from paths).
 * @return {@link ModuleSplitSplitter} for a given dimension functions.
 */
public static <T extends Message> ModuleSplitSplitter createSplitter(Function<AssetsDirectoryTargeting, T> dimensionGetter, Function<T, ApkTargeting> targetingSetter, Predicate<ApkTargeting> hasTargeting, Optional<TargetingDimension> targetingDimensionToRemove) {
    return new ModuleSplitSplitter() {

        @Override
        public ImmutableCollection<ModuleSplit> split(ModuleSplit split) {
            checkArgument(!hasTargeting.test(split.getApkTargeting()), "Split is already targeting the splitting dimension.");
            return split.getAssetsConfig().map(assetsConfig -> splitAssetsDirectories(assetsConfig, split)).orElse(ImmutableList.of(split)).stream().map(moduleSplit -> moduleSplit.isMasterSplit() ? moduleSplit : removeAssetsTargeting(moduleSplit)).collect(toImmutableList());
        }

        private ModuleSplit removeAssetsTargeting(ModuleSplit split) {
            return targetingDimensionToRemove.isPresent() ? SuffixStripper.createForDimension(targetingDimensionToRemove.get()).removeAssetsTargeting(split) : split;
        }

        private ImmutableList<ModuleSplit> splitAssetsDirectories(Assets assets, ModuleSplit split) {
            Multimap<T, TargetedAssetsDirectory> directoriesMap = Multimaps.filterKeys(Multimaps.index(assets.getDirectoryList(), targetedDirectory -> dimensionGetter.apply(targetedDirectory.getTargeting())), not(this::isDefaultTargeting));
            ImmutableList.Builder<ModuleSplit> splitsBuilder = new ImmutableList.Builder<>();
            // Generate config splits.
            directoriesMap.asMap().entrySet().forEach(entry -> {
                ImmutableList<ModuleEntry> entries = listEntriesFromDirectories(entry.getValue(), split);
                if (entries.isEmpty()) {
                    return;
                }
                ModuleSplit.Builder modifiedSplit = split.toBuilder();
                modifiedSplit.setEntries(entries).setApkTargeting(generateTargeting(split.getApkTargeting(), entry.getKey())).setMasterSplit(false).addMasterManifestMutator(withSplitsRequired(true));
                splitsBuilder.add(modifiedSplit.build());
            });
            // Ensure that master split (even an empty one) always exists.
            ModuleSplit defaultSplit = getDefaultAssetsSplit(split, splitsBuilder.build());
            if (defaultSplit.isMasterSplit() || !defaultSplit.getEntries().isEmpty()) {
                splitsBuilder.add(defaultSplit);
            }
            return splitsBuilder.build();
        }

        private ModuleSplit getDefaultAssetsSplit(ModuleSplit inputSplit, ImmutableList<ModuleSplit> configSplits) {
            ImmutableSet<ModuleEntry> claimedEntries = configSplits.stream().map(ModuleSplit::getEntries).flatMap(Collection::stream).collect(toImmutableSet());
            return inputSplit.toBuilder().setEntries(inputSplit.getEntries().stream().filter(not(claimedEntries::contains)).collect(toImmutableList())).build();
        }

        private boolean isDefaultTargeting(T splittingDimensionTargeting) {
            return splittingDimensionTargeting.equals(splittingDimensionTargeting.getDefaultInstanceForType());
        }

        private ApkTargeting generateTargeting(ApkTargeting splitTargeting, T extraTargeting) {
            if (isDefaultTargeting(extraTargeting)) {
                return splitTargeting;
            }
            return splitTargeting.toBuilder().mergeFrom(targetingSetter.apply(extraTargeting)).build();
        }

        private ImmutableList<ModuleEntry> listEntriesFromDirectories(Collection<TargetedAssetsDirectory> directories, ModuleSplit moduleSplit) {
            return directories.stream().map(targetedAssetsDirectory -> ZipPath.create(targetedAssetsDirectory.getPath())).flatMap(moduleSplit::getEntriesInDirectory).collect(toImmutableList());
        }
    };
}
Also used : ApkTargeting(com.android.bundle.Targeting.ApkTargeting) ZipPath(com.android.tools.build.bundletool.model.ZipPath) ImmutableCollection(com.google.common.collect.ImmutableCollection) Multimap(com.google.common.collect.Multimap) TargetingDimension(com.android.tools.build.bundletool.model.targeting.TargetingDimension) Multimaps(com.google.common.collect.Multimaps) ManifestMutator.withSplitsRequired(com.android.tools.build.bundletool.model.ManifestMutator.withSplitsRequired) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) SameTargetingMerger(com.android.tools.build.bundletool.mergers.SameTargetingMerger) Predicates.not(com.google.common.base.Predicates.not) ImmutableList(com.google.common.collect.ImmutableList) AssetsDirectoryTargeting(com.android.bundle.Targeting.AssetsDirectoryTargeting) Function(com.google.common.base.Function) ImmutableSet(com.google.common.collect.ImmutableSet) Predicate(java.util.function.Predicate) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Collection(java.util.Collection) ImmutableCollectors.toImmutableSet(com.android.utils.ImmutableCollectors.toImmutableSet) SuffixStripper(com.android.tools.build.bundletool.shards.SuffixStripper) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) Message(com.google.protobuf.Message) Optional(java.util.Optional) TargetedAssetsDirectory(com.android.bundle.Files.TargetedAssetsDirectory) Assets(com.android.bundle.Files.Assets) TargetedAssetsDirectory(com.android.bundle.Files.TargetedAssetsDirectory) ApkTargeting(com.android.bundle.Targeting.ApkTargeting) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) Assets(com.android.bundle.Files.Assets) ImmutableCollection(com.google.common.collect.ImmutableCollection) Collection(java.util.Collection)

Example 13 with Assets

use of com.android.bundle.Files.Assets in project bundletool by google.

the class AssetsTargetingValidatorTest method validateModule_assetModuleLanguageTargeting_throws.

@Test
public void validateModule_assetModuleLanguageTargeting_throws() throws Exception {
    Assets config = assets(targetedAssetsDirectory("assets/dir#lang_en", assetsDirectoryTargeting(languageTargeting("en"))));
    BundleModule module = new BundleModuleBuilder("testModule").addFile("assets/dir#lang_en/raw.dat").setAssetsConfig(config).setManifest(androidManifestForAssetModule("com.test.app")).build();
    InvalidBundleException e = assertThrows(InvalidBundleException.class, () -> new AssetsTargetingValidator().validateModule(module));
    assertThat(e).hasMessageThat().contains("Language targeting for asset packs is not supported, but found in module testModule.");
}
Also used : Assets(com.android.bundle.Files.Assets) BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) BundleModule(com.android.tools.build.bundletool.model.BundleModule) Test(org.junit.Test)

Example 14 with Assets

use of com.android.bundle.Files.Assets in project bundletool by google.

the class AssetsTargetingValidatorTest method validateModule_pathOutsideAssets_throws.

@Test
public void validateModule_pathOutsideAssets_throws() throws Exception {
    Assets config = assets(targetedAssetsDirectory("lib/x86", assetsDirectoryTargeting(abiTargeting(X86))), targetedAssetsDirectory("assets/dir#tcf_etc1", assetsDirectoryTargeting(textureCompressionTargeting(TextureCompressionFormatAlias.ETC1_RGB8))));
    BundleModule module = new BundleModuleBuilder("testModule").setAssetsConfig(config).addFile("assets/dir#tcf_etc1/file.txt").setManifest(androidManifest("com.test.app")).build();
    InvalidBundleException e = assertThrows(InvalidBundleException.class, () -> new AssetsTargetingValidator().validateModule(module));
    assertThat(e).hasMessageThat().contains("directory must start with 'assets/'");
}
Also used : Assets(com.android.bundle.Files.Assets) BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) BundleModule(com.android.tools.build.bundletool.model.BundleModule) Test(org.junit.Test)

Example 15 with Assets

use of com.android.bundle.Files.Assets in project bundletool by google.

the class AssetsTargetingValidatorTest method validateModule_defaultInstanceOfLanguageTargeting_throws.

@Test
public void validateModule_defaultInstanceOfLanguageTargeting_throws() throws Exception {
    Assets config = assets(targetedAssetsDirectory("assets/dir", AssetsDirectoryTargeting.newBuilder().setLanguage(LanguageTargeting.getDefaultInstance()).build()));
    BundleModule module = new BundleModuleBuilder("testModule").addFile("assets/dir/raw.dat").setAssetsConfig(config).setManifest(androidManifestForAssetModule("com.test.app")).build();
    InvalidBundleException e = assertThrows(InvalidBundleException.class, () -> new AssetsTargetingValidator().validateModule(module));
    assertThat(e).hasMessageThat().contains("set but empty language targeting");
}
Also used : Assets(com.android.bundle.Files.Assets) BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) BundleModule(com.android.tools.build.bundletool.model.BundleModule) Test(org.junit.Test)

Aggregations

Assets (com.android.bundle.Files.Assets)27 Test (org.junit.Test)21 BundleModule (com.android.tools.build.bundletool.model.BundleModule)11 BundleModuleBuilder (com.android.tools.build.bundletool.testing.BundleModuleBuilder)9 ZipPath (com.android.tools.build.bundletool.model.ZipPath)8 InvalidBundleException (com.android.tools.build.bundletool.model.exceptions.InvalidBundleException)7 TargetedAssetsDirectory (com.android.bundle.Files.TargetedAssetsDirectory)4 ModuleEntry (com.android.tools.build.bundletool.model.ModuleEntry)4 AssetsDirectoryTargeting (com.android.bundle.Targeting.AssetsDirectoryTargeting)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 XmlNode (com.android.aapt.Resources.XmlNode)2 NativeLibraries (com.android.bundle.Files.NativeLibraries)2 ApkTargeting (com.android.bundle.Targeting.ApkTargeting)2 AppBundle (com.android.tools.build.bundletool.model.AppBundle)2 ModuleSplit (com.android.tools.build.bundletool.model.ModuleSplit)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)2 Path (java.nio.file.Path)2 ZipFile (java.util.zip.ZipFile)2