Search in sources :

Example 1 with TargetedAssetsDirectory

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

the class AssetsTargetingValidator method validateTargeting.

private void validateTargeting(BundleModule module, Assets assets) {
    ImmutableSet<ZipPath> assetDirsWithFiles = getDirectoriesWithFiles(module);
    for (TargetedAssetsDirectory targetedDirectory : assets.getDirectoryList()) {
        ZipPath path = ZipPath.create(targetedDirectory.getPath());
        if (!path.startsWith(ASSETS_DIRECTORY)) {
            throw InvalidBundleException.builder().withUserMessage("Path of targeted assets directory must start with 'assets/' but found '%s'.", path).build();
        }
        if (!assetDirsWithFiles.contains(path)) {
            throw InvalidBundleException.builder().withUserMessage("Targeted directory '%s' is empty.", path).build();
        }
        checkNoDimensionWithoutValuesAndAlternatives(targetedDirectory);
    }
    if (module.getModuleType().equals(ModuleType.ASSET_MODULE) && assets.getDirectoryList().stream().anyMatch(dir -> dir.getTargeting().hasLanguage())) {
        throw InvalidBundleException.builder().withUserMessage("Language targeting for asset packs is not supported, but found in module %s.", module.getName().getName()).build();
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) ZipPath(com.android.tools.build.bundletool.model.ZipPath) AssetsDirectoryTargeting(com.android.bundle.Targeting.AssetsDirectoryTargeting) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) TargetedAssetsDirectory(com.android.bundle.Files.TargetedAssetsDirectory) ASSETS_DIRECTORY(com.android.tools.build.bundletool.model.BundleModule.ASSETS_DIRECTORY) Assets(com.android.bundle.Files.Assets) ModuleType(com.android.tools.build.bundletool.model.BundleModule.ModuleType) BundleValidationUtils.checkHasValuesOrAlternatives(com.android.tools.build.bundletool.validation.BundleValidationUtils.checkHasValuesOrAlternatives) BundleModule(com.android.tools.build.bundletool.model.BundleModule) TargetedAssetsDirectory(com.android.bundle.Files.TargetedAssetsDirectory) ZipPath(com.android.tools.build.bundletool.model.ZipPath)

Example 2 with TargetedAssetsDirectory

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

the class SuffixStripper method removeAssetsTargeting.

/**
 * Updates the module to remove the specified targeting from the assets - both the directories in
 * assets config and the associated module entries having the specified targeting will be updated.
 */
public ModuleSplit removeAssetsTargeting(ModuleSplit moduleSplit) {
    if (!moduleSplit.getAssetsConfig().isPresent()) {
        return moduleSplit;
    }
    // Update the targeted assets directory and their associated entries.
    Assets assetsConfig = moduleSplit.getAssetsConfig().get();
    Assets.Builder updatedAssetsConfig = assetsConfig.toBuilder().clearDirectory();
    ImmutableList<ModuleEntry> updatedEntries = moduleSplit.getEntries();
    for (TargetedAssetsDirectory targetedAssetsDirectory : assetsConfig.getDirectoryList()) {
        TargetedAssetsDirectory updatedTargetedAssetsDirectory = removeAssetsTargetingFromDirectory(targetedAssetsDirectory);
        // Remove the targeting from the entries path.
        if (!updatedTargetedAssetsDirectory.equals(targetedAssetsDirectory)) {
            // Update the associated entries
            ZipPath directoryPath = ZipPath.create(targetedAssetsDirectory.getPath());
            updatedEntries = updatedEntries.stream().map(entry -> {
                if (entry.getPath().startsWith(directoryPath)) {
                    return removeTargetingFromEntry(entry);
                }
                return entry;
            }).collect(toImmutableList());
        }
        updatedAssetsConfig.addDirectory(updatedTargetedAssetsDirectory);
    }
    return moduleSplit.toBuilder().setEntries(updatedEntries).setAssetsConfig(updatedAssetsConfig.build()).build();
}
Also used : TargetedAssetsDirectory(com.android.bundle.Files.TargetedAssetsDirectory) Assets(com.android.bundle.Files.Assets) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ZipPath(com.android.tools.build.bundletool.model.ZipPath)

Example 3 with TargetedAssetsDirectory

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

the class SuffixStripper method excludeAssetsTargetingOtherValue.

/**
 * Updates the module to remove the specified targeting from the assets: both the assets in assets
 * config and the associated entries having the specified targeting will be updated.
 */
private ModuleSplit excludeAssetsTargetingOtherValue(ModuleSplit moduleSplit, String value) {
    if (!moduleSplit.getAssetsConfig().isPresent()) {
        return moduleSplit;
    }
    // Update the targeted assets directory and their associated entries.
    Assets assetsConfig = moduleSplit.getAssetsConfig().get();
    Assets.Builder updatedAssetsConfig = assetsConfig.toBuilder().clearDirectory();
    ImmutableList<ModuleEntry> updatedEntries = moduleSplit.getEntries();
    for (TargetedAssetsDirectory targetedAssetsDirectory : assetsConfig.getDirectoryList()) {
        ZipPath directoryPath = ZipPath.create(targetedAssetsDirectory.getPath());
        // Check if the directory is targeted at this dimension, but for another value.
        if (dimensionHandler.isDirectoryTargetingOtherValue(targetedAssetsDirectory, value)) {
            // Removed the associated entries if so.
            updatedEntries = updatedEntries.stream().filter(entry -> !entry.getPath().startsWith(directoryPath)).collect(toImmutableList());
        } else {
            // Keep the directory otherwise.
            updatedAssetsConfig.addDirectory(targetedAssetsDirectory);
        }
    }
    return moduleSplit.toBuilder().setEntries(updatedEntries).setAssetsConfig(updatedAssetsConfig.build()).build();
}
Also used : TargetedAssetsDirectory(com.android.bundle.Files.TargetedAssetsDirectory) Assets(com.android.bundle.Files.Assets) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ZipPath(com.android.tools.build.bundletool.model.ZipPath)

Example 4 with TargetedAssetsDirectory

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

the class SameTargetingMergerTest method assetsConfigMerging_mergeDisjointAssets.

@Test
public void assetsConfigMerging_mergeDisjointAssets() throws Exception {
    ModuleSplit moduleSplit = createModuleSplitBuilder().setEntries(ImmutableList.of(createModuleEntryForFile("assets/some_assets/file.txt", DUMMY_CONTENT))).setApkTargeting(ApkTargeting.getDefaultInstance()).setAssetsConfig(Assets.newBuilder().addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/some_assets").build()).build()).build();
    ModuleSplit moduleSplit2 = createModuleSplitBuilder().setEntries(ImmutableList.of(createModuleEntryForFile("assets/some_other_assets/file.txt", DUMMY_CONTENT))).setApkTargeting(ApkTargeting.getDefaultInstance()).setAssetsConfig(Assets.newBuilder().addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/some_other_assets").build()).build()).build();
    ImmutableCollection<ModuleSplit> splits = new SameTargetingMerger().merge(ImmutableList.of(moduleSplit, moduleSplit2));
    assertThat(splits).hasSize(1);
    ModuleSplit masterSplit = splits.iterator().next();
    assertThat(extractPaths(masterSplit.getEntries())).containsExactly("assets/some_assets/file.txt", "assets/some_other_assets/file.txt");
    assertThat(masterSplit.getAssetsConfig()).isPresent();
    assertThat(masterSplit.getAssetsConfig().get().getDirectoryList().stream().map(TargetedAssetsDirectory::getPath)).containsExactly("assets/some_assets", "assets/some_other_assets");
}
Also used : TargetedAssetsDirectory(com.android.bundle.Files.TargetedAssetsDirectory) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) Test(org.junit.Test)

Example 5 with TargetedAssetsDirectory

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

the class ModuleSplitsToShardMergerTest method mergeSingleShard_mergeDisjointAssets.

@Test
public void mergeSingleShard_mergeDisjointAssets() throws Exception {
    ModuleSplit baseModuleSplit = createModuleSplitBuilder().setEntries(ImmutableList.of(createModuleEntryForFile("assets/some_assets/file.txt", DUMMY_CONTENT))).setAssetsConfig(Assets.newBuilder().addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/some_assets").build()).build()).build();
    ModuleSplit featureModuleSplit = createModuleSplitBuilder().setModuleName(BundleModuleName.create(FEATURE_MODULE_NAME)).setEntries(ImmutableList.of(createModuleEntryForFile("assets/some_other_assets/file.txt", DUMMY_CONTENT))).setAssetsConfig(Assets.newBuilder().addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/some_other_assets").build()).build()).build();
    ModuleSplit merged = splitsToShardMerger.mergeSingleShard(ImmutableList.of(baseModuleSplit, featureModuleSplit), createCache());
    assertThat(extractPaths(merged.getEntries())).containsExactly("assets/some_assets/file.txt", "assets/some_other_assets/file.txt");
    assertThat(merged.getAssetsConfig()).isPresent();
    assertThat(merged.getAssetsConfig().get().getDirectoryList().stream().map(TargetedAssetsDirectory::getPath)).containsExactly("assets/some_assets", "assets/some_other_assets");
}
Also used : TargetedAssetsDirectory(com.android.bundle.Files.TargetedAssetsDirectory) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) Test(org.junit.Test)

Aggregations

TargetedAssetsDirectory (com.android.bundle.Files.TargetedAssetsDirectory)10 ModuleSplit (com.android.tools.build.bundletool.model.ModuleSplit)7 ModuleEntry (com.android.tools.build.bundletool.model.ModuleEntry)5 ZipPath (com.android.tools.build.bundletool.model.ZipPath)5 Assets (com.android.bundle.Files.Assets)4 Test (org.junit.Test)4 ResourceTable (com.android.aapt.Resources.ResourceTable)2 ApkTargeting (com.android.bundle.Targeting.ApkTargeting)2 AssetsDirectoryTargeting (com.android.bundle.Targeting.AssetsDirectoryTargeting)2 AndroidManifest (com.android.tools.build.bundletool.model.AndroidManifest)2 BundleModuleName (com.android.tools.build.bundletool.model.BundleModuleName)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 HashMap (java.util.HashMap)2 ApexEmbeddedApkConfig (com.android.bundle.Config.ApexEmbeddedApkConfig)1 ApexImages (com.android.bundle.Files.ApexImages)1 NativeLibraries (com.android.bundle.Files.NativeLibraries)1 VariantTargeting (com.android.bundle.Targeting.VariantTargeting)1 SameTargetingMerger (com.android.tools.build.bundletool.mergers.SameTargetingMerger)1 BundleModule (com.android.tools.build.bundletool.model.BundleModule)1