Search in sources :

Example 6 with Assets

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

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

the class ModuleSplitterTest method pinSpecIsCopied.

@Test
public void pinSpecIsCopied() {
    NativeLibraries nativeConfig = nativeLibraries(targetedNativeDirectory("lib/x86", nativeDirectoryTargeting("x86")));
    BundleModule testModule = new BundleModuleBuilder("testModule").setManifest(androidManifest("com.test.app")).setNativeConfig(nativeConfig).addFile("lib/x86/liba.so").addFile("assets/com.android.hints.pins.txt").build();
    ImmutableList<ModuleSplit> splits = createAbiAndDensitySplitter(testModule).splitModule();
    assertThat(splits).hasSize(2);
    assertThat(splits.stream().map((s) -> s.getEntries().stream().filter((e) -> e.getPath().endsWith(PinSpecInjector.PIN_SPEC_NAME)).count()).distinct()).containsExactly(1L);
}
Also used : ManifestProtoUtils.withSplitNameActivity(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withSplitNameActivity) ApkTargeting(com.android.bundle.Targeting.ApkTargeting) ResourcesTableFactory.locale(com.android.tools.build.bundletool.testing.ResourcesTableFactory.locale) TargetingUtils.textureCompressionTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.textureCompressionTargeting) TargetingUtils.alternativeLanguageTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.alternativeLanguageTargeting) SCREEN_DENSITY(com.android.tools.build.bundletool.model.OptimizationDimension.SCREEN_DENSITY) Predicates.not(com.google.common.base.Predicates.not) ResourceId(com.android.tools.build.bundletool.model.ResourceId) Map(java.util.Map) DEVICE_TIER(com.android.tools.build.bundletool.model.OptimizationDimension.DEVICE_TIER) TargetingUtils.lPlusVariantTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.lPlusVariantTargeting) ManifestMutator.withExtractNativeLibs(com.android.tools.build.bundletool.model.ManifestMutator.withExtractNativeLibs) DensityAlias(com.android.bundle.Targeting.ScreenDensity.DensityAlias) Version(com.android.tools.build.bundletool.model.version.Version) ResourcesTableFactory.type(com.android.tools.build.bundletool.testing.ResourcesTableFactory.type) ResourceTable(com.android.aapt.Resources.ResourceTable) SPLIT_NAME_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.SPLIT_NAME_RESOURCE_ID) ModuleSplitUtils.applyManifestMutators(com.android.tools.build.bundletool.testing.ModuleSplitUtils.applyManifestMutators) BundleToolVersion(com.android.tools.build.bundletool.model.version.BundleToolVersion) TargetingUtils.nativeDirectoryTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.nativeDirectoryTargeting) ConfigValue(com.android.aapt.Resources.ConfigValue) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) StampType(com.android.tools.build.bundletool.model.SourceStamp.StampType) ManifestProtoUtils.withMinSdkVersion(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withMinSdkVersion) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) Assets(com.android.bundle.Files.Assets) TargetingUtils.assetsDirectoryTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.assetsDirectoryTargeting) STAMP_TYPE_METADATA_KEY(com.android.tools.build.bundletool.model.SourceStamp.STAMP_TYPE_METADATA_KEY) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Iterables(com.google.common.collect.Iterables) STAMP_SOURCE_METADATA_KEY(com.android.tools.build.bundletool.model.SourceStamp.STAMP_SOURCE_METADATA_KEY) TargetingUtils.variantMinSdkTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.variantMinSdkTargeting) ZipPath(com.android.tools.build.bundletool.model.ZipPath) OptimizationDimension(com.android.tools.build.bundletool.model.OptimizationDimension) ATC(com.android.bundle.Targeting.TextureCompressionFormat.TextureCompressionFormatAlias.ATC) RunWith(org.junit.runner.RunWith) TargetingUtils.apkMinSdkTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.apkMinSdkTargeting) TEXTURE_COMPRESSION_FORMAT(com.android.tools.build.bundletool.model.OptimizationDimension.TEXTURE_COMPRESSION_FORMAT) TargetingUtils.getSplitsWithTargetingEqualTo(com.android.tools.build.bundletool.testing.TargetingUtils.getSplitsWithTargetingEqualTo) TruthResourceTable.assertThat(com.android.tools.build.bundletool.testing.truth.resources.TruthResourceTable.assertThat) ANDROID_R_API_VERSION(com.android.tools.build.bundletool.model.utils.Versions.ANDROID_R_API_VERSION) LANGUAGE(com.android.tools.build.bundletool.model.OptimizationDimension.LANGUAGE) ManifestProtoUtils.withMetadataResource(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withMetadataResource) ResourcesTableFactory.mergeConfigs(com.android.tools.build.bundletool.testing.ResourcesTableFactory.mergeConfigs) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) XmlProtoElement(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElement) ResourcesTableFactory.pkg(com.android.tools.build.bundletool.testing.ResourcesTableFactory.pkg) ANDROID_Q_API_VERSION(com.android.tools.build.bundletool.model.utils.Versions.ANDROID_Q_API_VERSION) VariantTargeting(com.android.bundle.Targeting.VariantTargeting) SuffixStripping(com.android.bundle.Config.SuffixStripping) ManifestProtoUtils.clearApplication(com.android.tools.build.bundletool.testing.ManifestProtoUtils.clearApplication) USER_PACKAGE_OFFSET(com.android.tools.build.bundletool.testing.ResourcesTableFactory.USER_PACKAGE_OFFSET) HDPI(com.android.tools.build.bundletool.testing.ResourcesTableFactory.HDPI) TargetingUtils.targetedNativeDirectory(com.android.tools.build.bundletool.testing.TargetingUtils.targetedNativeDirectory) TestCase.fail(junit.framework.TestCase.fail) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) Test(org.junit.Test) ANDROID_L_API_VERSION(com.android.tools.build.bundletool.model.utils.Versions.ANDROID_L_API_VERSION) ANDROID_M_API_VERSION(com.android.tools.build.bundletool.model.utils.Versions.ANDROID_M_API_VERSION) TargetingUtils.assets(com.android.tools.build.bundletool.testing.TargetingUtils.assets) TargetingUtils.apkDensityTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.apkDensityTargeting) XmlNode(com.android.aapt.Resources.XmlNode) TargetingUtils.mergeApkTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.mergeApkTargeting) ManifestProtoUtils.withMainActivity(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withMainActivity) ModuleSplitUtils.createModuleSplitBuilder(com.android.tools.build.bundletool.testing.ModuleSplitUtils.createModuleSplitBuilder) AppBundle(com.android.tools.build.bundletool.model.AppBundle) BundleModule(com.android.tools.build.bundletool.model.BundleModule) TargetingUtils.apkDeviceTierTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.apkDeviceTierTargeting) ManifestProtoUtils.androidManifest(com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifest) AndroidManifest(com.android.tools.build.bundletool.model.AndroidManifest) ManifestProtoUtils.xmlAttribute(com.android.tools.build.bundletool.testing.ManifestProtoUtils.xmlAttribute) MoreCollectors.onlyElement(com.google.common.collect.MoreCollectors.onlyElement) ANDROID_NAMESPACE_URI(com.android.tools.build.bundletool.model.AndroidManifest.ANDROID_NAMESPACE_URI) TargetingUtils.deviceTierTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.deviceTierTargeting) TargetingUtils.targetedAssetsDirectory(com.android.tools.build.bundletool.testing.TargetingUtils.targetedAssetsDirectory) NAME_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.NAME_RESOURCE_ID) ResourcesTableFactory.entry(com.android.tools.build.bundletool.testing.ResourcesTableFactory.entry) TargetingUtils.languageTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.languageTargeting) ImmutableSet(com.google.common.collect.ImmutableSet) ResourcesTableFactory.value(com.android.tools.build.bundletool.testing.ResourcesTableFactory.value) TargetingUtils.apkTextureTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.apkTextureTargeting) ImmutableMap(com.google.common.collect.ImmutableMap) CommandExecutionException(com.android.tools.build.bundletool.model.exceptions.CommandExecutionException) SplitType(com.android.tools.build.bundletool.model.ModuleSplit.SplitType) Sets(com.google.common.collect.Sets) Preconditions.checkState(com.google.common.base.Preconditions.checkState) NativeLibraries(com.android.bundle.Files.NativeLibraries) List(java.util.List) AppBundleBuilder(com.android.tools.build.bundletool.testing.AppBundleBuilder) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) Optional(java.util.Optional) ABI(com.android.tools.build.bundletool.model.OptimizationDimension.ABI) TargetingUtils.apkLanguageTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.apkLanguageTargeting) ManifestProtoUtils.withMaxSdkVersion(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withMaxSdkVersion) TargetingUtils.apkAbiTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.apkAbiTargeting) BundleModuleName(com.android.tools.build.bundletool.model.BundleModuleName) TestUtils.extractPaths(com.android.tools.build.bundletool.testing.TestUtils.extractPaths) ResourcesTableFactory.resourceTable(com.android.tools.build.bundletool.testing.ResourcesTableFactory.resourceTable) TargetingUtils.toAbi(com.android.tools.build.bundletool.testing.TargetingUtils.toAbi) BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder) ProtoTruth.assertThat(com.google.common.truth.extensions.proto.ProtoTruth.assertThat) ImmutableList(com.google.common.collect.ImmutableList) ACTIVITY_ELEMENT_NAME(com.android.tools.build.bundletool.model.AndroidManifest.ACTIVITY_ELEMENT_NAME) S3TC(com.android.bundle.Targeting.TextureCompressionFormat.TextureCompressionFormatAlias.S3TC) Truth8.assertThat(com.google.common.truth.Truth8.assertThat) ManifestProtoUtils.withInstant(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withInstant) APPLICATION_ELEMENT_NAME(com.android.tools.build.bundletool.model.AndroidManifest.APPLICATION_ELEMENT_NAME) JUnit4(org.junit.runners.JUnit4) XHDPI(com.android.tools.build.bundletool.testing.ResourcesTableFactory.XHDPI) Truth.assertThat(com.google.common.truth.Truth.assertThat) ETC1_RGB8(com.android.bundle.Targeting.TextureCompressionFormat.TextureCompressionFormatAlias.ETC1_RGB8) Maps(com.google.common.collect.Maps) ResourcesTableFactory.fileReference(com.android.tools.build.bundletool.testing.ResourcesTableFactory.fileReference) TargetingUtils.nativeLibraries(com.android.tools.build.bundletool.testing.TargetingUtils.nativeLibraries) AbiAlias(com.android.bundle.Targeting.Abi.AbiAlias) Ignore(org.junit.Ignore) TargetingUtils.apkAlternativeLanguageTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.apkAlternativeLanguageTargeting) XmlElement(com.android.aapt.Resources.XmlElement) Versions(com.android.tools.build.bundletool.model.utils.Versions) Configuration(com.android.aapt.ConfigurationOuterClass.Configuration) NativeLibraries(com.android.bundle.Files.NativeLibraries) BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) BundleModule(com.android.tools.build.bundletool.model.BundleModule) Test(org.junit.Test)

Example 8 with Assets

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

the class BundleModuleTest method correctAssetsProtoFile_parsedAndReturned.

@Test
public void correctAssetsProtoFile_parsedAndReturned() throws Exception {
    Assets assetsConfig = Assets.newBuilder().addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/data-armv6")).build();
    BundleModule bundleModule = createMinimalModuleBuilder().addEntry(createModuleEntryForFile("assets.pb", assetsConfig.toByteArray())).build();
    assertThat(bundleModule.getAssetsConfig()).hasValue(assetsConfig);
}
Also used : Assets(com.android.bundle.Files.Assets) Test(org.junit.Test)

Example 9 with Assets

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

the class TargetingGenerator method generateTargetingForAssets.

/**
 * Processes given asset directories, generating targeting based on their names.
 *
 * @param assetDirectories Names of directories under assets/, including the "assets/" prefix.
 * @return Targeting for the given asset directories.
 */
public Assets generateTargetingForAssets(Collection<ZipPath> assetDirectories) {
    for (ZipPath directory : assetDirectories) {
        // Extra '/' to handle special case when the directory is just "assets".
        checkRootDirectoryName(ASSETS_DIR, directory + "/");
    }
    // Stores all different targeting values for a given set of sibling targeted directories.
    // Key: targeted directory base name {@link TargetedDirectory#getPathBaseName}
    // Values: {@link AssetsDirectoryTargeting} targeting expressed as value for each sibling.
    HashMultimap<String, AssetsDirectoryTargeting> targetingByBaseName = HashMultimap.create();
    for (ZipPath assetDirectory : FileUtils.toPathWalkingOrder(assetDirectories)) {
        TargetedDirectory targetedDirectory = TargetedDirectory.parse(assetDirectory);
        targetingByBaseName.put(targetedDirectory.getPathBaseName(), targetedDirectory.getLastSegment().getTargeting());
    }
    validateDimensions(targetingByBaseName);
    // Pass 2: Building the directory targeting proto using the targetingByBaseName map.
    Assets.Builder assetsBuilder = Assets.newBuilder();
    for (ZipPath assetDirectory : assetDirectories) {
        AssetsDirectoryTargeting.Builder targeting = AssetsDirectoryTargeting.newBuilder();
        TargetedDirectory targetedDirectory = TargetedDirectory.parse(assetDirectory);
        // We will calculate targeting of each path segment and merge them together.
        for (int i = 0; i < targetedDirectory.getPathSegments().size(); i++) {
            TargetedDirectorySegment segment = targetedDirectory.getPathSegments().get(i);
            // Set targeting values.
            targeting.mergeFrom(segment.getTargeting());
            // Set targeting alternatives.
            if (segment.getTargeting().hasLanguage()) {
                // identical language across resources and assets.
                continue;
            }
            targeting.mergeFrom(// Remove oneself from the alternatives and merge them together.
            Sets.difference(targetingByBaseName.get(targetedDirectory.getSubPathBaseName(i)), ImmutableSet.of(segment.getTargeting())).stream().map(TargetingProtoUtils::toAlternativeTargeting).reduce(AssetsDirectoryTargeting.newBuilder(), (builder, targetingValue) -> builder.mergeFrom(targetingValue), (builderA, builderB) -> builderA.mergeFrom(builderB.build())).build());
        }
        assetsBuilder.addDirectory(TargetedAssetsDirectory.newBuilder().setPath(assetDirectory.toString()).setTargeting(targeting));
    }
    return assetsBuilder.build();
}
Also used : AssetsDirectoryTargeting(com.android.bundle.Targeting.AssetsDirectoryTargeting) Assets(com.android.bundle.Files.Assets) ZipPath(com.android.tools.build.bundletool.model.ZipPath)

Example 10 with Assets

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

the class TargetingGeneratorTest method generateTargetingForAssets_basicScenario.

@Test
public void generateTargetingForAssets_basicScenario() 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"), ZipPath.create("assets/world/texture#tcf_atc/i18n#lang_en"), ZipPath.create("assets/world/texture#tcf_atc/i18n#lang_ru")));
    assertThat(assetsConfig).ignoringRepeatedFieldOrder().isEqualTo(Assets.newBuilder().addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/world/texture#tcf_etc1/i18n#lang_en").setTargeting(mergeAssetsTargeting(assetsDirectoryTargeting(languageTargeting("en")), assetsDirectoryTargeting(textureCompressionTargeting(TextureCompressionFormatAlias.ETC1_RGB8, ImmutableSet.of(TextureCompressionFormatAlias.ATC)))))).addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/world/texture#tcf_etc1/i18n").setTargeting(mergeAssetsTargeting(assetsDirectoryTargeting(alternativeLanguageTargeting("en")), assetsDirectoryTargeting(textureCompressionTargeting(TextureCompressionFormatAlias.ETC1_RGB8, ImmutableSet.of(TextureCompressionFormatAlias.ATC)))))).addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/world/texture#tcf_atc/i18n#lang_en").setTargeting(mergeAssetsTargeting(assetsDirectoryTargeting(languageTargeting("en")), assetsDirectoryTargeting(textureCompressionTargeting(TextureCompressionFormatAlias.ATC, ImmutableSet.of(TextureCompressionFormatAlias.ETC1_RGB8)))))).addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/world/texture#tcf_atc/i18n#lang_ru").setTargeting(mergeAssetsTargeting(assetsDirectoryTargeting(languageTargeting("ru")), assetsDirectoryTargeting(textureCompressionTargeting(TextureCompressionFormatAlias.ATC, ImmutableSet.of(TextureCompressionFormatAlias.ETC1_RGB8)))))).build());
}
Also used : Assets(com.android.bundle.Files.Assets) 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