Search in sources :

Example 1 with Assets

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

the class AssetsTargetingValidatorTest method validateModule_valid_succeeds.

@Test
public void validateModule_valid_succeeds() throws Exception {
    Assets config = assets(targetedAssetsDirectory("assets/dir_other#tcf_etc1", assetsDirectoryTargeting(textureCompressionTargeting(TextureCompressionFormatAlias.ETC1_RGB8))));
    BundleModule module = new BundleModuleBuilder("testModule").addFile("assets/dir_other#tcf_etc1/raw.dat").setAssetsConfig(config).setManifest(androidManifest("com.test.app")).build();
    new AssetsTargetingValidator().validateModule(module);
}
Also used : Assets(com.android.bundle.Files.Assets) BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder) BundleModule(com.android.tools.build.bundletool.model.BundleModule) Test(org.junit.Test)

Example 2 with Assets

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

the class AssetsTargetingValidatorTest method validateModule_emptyTargetedDirectory_throws.

@Test
public void validateModule_emptyTargetedDirectory_throws() throws Exception {
    Assets config = assets(targetedAssetsDirectory("assets/dir#tcf_etc1", assetsDirectoryTargeting(textureCompressionTargeting(TextureCompressionFormatAlias.ETC1_RGB8))));
    BundleModule module = new BundleModuleBuilder("testModule").setAssetsConfig(config).setManifest(androidManifest("com.test.app")).build();
    InvalidBundleException e = assertThrows(InvalidBundleException.class, () -> new AssetsTargetingValidator().validateModule(module));
    assertThat(e).hasMessageThat().contains("Targeted directory 'assets/dir#tcf_etc1' is empty.");
}
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 3 with Assets

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

the class AssetsTargetingValidatorTest method validateModule_pointDirectlyToAssets_succeeds.

@Test
public void validateModule_pointDirectlyToAssets_succeeds() throws Exception {
    Assets config = assets(targetedAssetsDirectory("assets", AssetsDirectoryTargeting.getDefaultInstance()));
    BundleModule module = new BundleModuleBuilder("testModule").addFile("assets/file.dat").setAssetsConfig(config).setManifest(androidManifest("com.test.app")).build();
    new AssetsTargetingValidator().validateModule(module);
}
Also used : Assets(com.android.bundle.Files.Assets) BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder) BundleModule(com.android.tools.build.bundletool.model.BundleModule) Test(org.junit.Test)

Example 4 with Assets

use of com.android.bundle.Files.Assets 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 5 with Assets

use of com.android.bundle.Files.Assets 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)

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