Search in sources :

Example 51 with BundleModule

use of com.android.tools.build.bundletool.model.BundleModule in project bundletool by google.

the class AndroidManifestValidator method validateMinMaxSdk.

private static void validateMinMaxSdk(BundleModule module) {
    AndroidManifest manifest = module.getAndroidManifest();
    Optional<Integer> maxSdk = manifest.getMaxSdkVersion();
    Optional<Integer> minSdk = manifest.getMinSdkVersion();
    maxSdk.filter(sdk -> sdk < 0).ifPresent(sdk -> {
        throw InvalidBundleException.builder().withUserMessage("maxSdkVersion must be nonnegative, found: (%d).", sdk).build();
    });
    minSdk.filter(sdk -> sdk < 0).ifPresent(sdk -> {
        throw InvalidBundleException.builder().withUserMessage("minSdkVersion must be nonnegative, found: (%d).", sdk).build();
    });
    if (maxSdk.isPresent() && minSdk.isPresent() && maxSdk.get() < minSdk.get()) {
        throw InvalidBundleException.builder().withUserMessage("minSdkVersion (%d) is greater than maxSdkVersion (%d).", minSdk.get(), maxSdk.get()).build();
    }
}
Also used : Iterables(com.google.common.collect.Iterables) NO_INITIAL_INSTALL(com.android.tools.build.bundletool.model.ModuleDeliveryType.NO_INITIAL_INSTALL) VERSION_CODE_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.VERSION_CODE_RESOURCE_ID) AndroidManifest(com.android.tools.build.bundletool.model.AndroidManifest) DISTRIBUTION_NAMESPACE_URI(com.android.tools.build.bundletool.model.AndroidManifest.DISTRIBUTION_NAMESPACE_URI) CONDITIONAL_INITIAL_INSTALL(com.android.tools.build.bundletool.model.ModuleDeliveryType.CONDITIONAL_INITIAL_INSTALL) XmlProtoAttribute(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoAttribute) ImmutableList(com.google.common.collect.ImmutableList) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ModuleType(com.android.tools.build.bundletool.model.BundleModule.ModuleType) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) ModuleDeliveryType(com.android.tools.build.bundletool.model.ModuleDeliveryType) ImmutableSet(com.google.common.collect.ImmutableSet) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ManifestDeliveryElement(com.android.tools.build.bundletool.model.ManifestDeliveryElement) NO_NAMESPACE_URI(com.android.tools.build.bundletool.model.AndroidManifest.NO_NAMESPACE_URI) Collectors.joining(java.util.stream.Collectors.joining) InvalidVersionCodeException(com.android.tools.build.bundletool.model.exceptions.InvalidVersionCodeException) ModuleTargeting(com.android.bundle.Targeting.ModuleTargeting) Optional(java.util.Optional) TargetingUtils.extractTextureCompressionFormats(com.android.tools.build.bundletool.model.targeting.TargetingUtils.extractTextureCompressionFormats) ModuleConditions(com.android.tools.build.bundletool.model.ModuleConditions) TextureCompressionFormatAlias(com.android.bundle.Targeting.TextureCompressionFormat.TextureCompressionFormatAlias) ALWAYS_INITIAL_INSTALL(com.android.tools.build.bundletool.model.ModuleDeliveryType.ALWAYS_INITIAL_INSTALL) TargetingUtils.extractAssetsTargetedDirectories(com.android.tools.build.bundletool.model.targeting.TargetingUtils.extractAssetsTargetedDirectories) BundleModule(com.android.tools.build.bundletool.model.BundleModule) Joiner(com.google.common.base.Joiner) AndroidManifest(com.android.tools.build.bundletool.model.AndroidManifest)

Example 52 with BundleModule

use of com.android.tools.build.bundletool.model.BundleModule in project bundletool by google.

the class ApexBundleValidator method validateModule.

@Override
public void validateModule(BundleModule module) {
    if (module.findEntriesUnderPath(APEX_DIRECTORY).count() == 0) {
        return;
    }
    Optional<ModuleEntry> apexManifest = module.getEntry(APEX_MANIFEST_PATH);
    if (apexManifest.isPresent()) {
        validateApexManifest(apexManifest.get());
    } else {
        apexManifest = module.getEntry(APEX_MANIFEST_JSON_PATH);
        if (!apexManifest.isPresent()) {
            throw InvalidBundleException.builder().withUserMessage("Missing expected file in APEX bundle: '%s' or '%s'.", APEX_MANIFEST_PATH, APEX_MANIFEST_JSON_PATH).build();
        }
        validateApexManifestJson(apexManifest.get());
    }
    ImmutableSet.Builder<String> apexImagesBuilder = ImmutableSet.builder();
    ImmutableSet.Builder<String> apexBuildInfosBuilder = ImmutableSet.builder();
    ImmutableSet.Builder<String> apexFileNamesBuilder = ImmutableSet.builder();
    for (ModuleEntry entry : module.getEntries()) {
        ZipPath path = entry.getPath();
        if (path.startsWith(APEX_DIRECTORY)) {
            if (path.getFileName().toString().endsWith(BundleModule.APEX_IMAGE_SUFFIX)) {
                apexImagesBuilder.add(path.toString());
                apexFileNamesBuilder.add(path.getFileName().toString());
            } else if (path.getFileName().toString().endsWith(BundleModule.BUILD_INFO_SUFFIX)) {
                apexBuildInfosBuilder.add(path.toString());
            } else {
                throw InvalidBundleException.builder().withUserMessage("Unexpected file in apex directory of bundle: '%s'.", entry.getPath()).build();
            }
        } else if (!ALLOWED_APEX_FILES_OUTSIDE_APEX_DIRECTORY.contains(path)) {
            throw InvalidBundleException.builder().withUserMessage("Unexpected file in APEX bundle: '%s'.", entry.getPath()).build();
        }
    }
    ImmutableSet<String> apexBuildInfos = apexBuildInfosBuilder.build();
    ImmutableSet<String> apexImages = apexImagesBuilder.build();
    ImmutableSet<ImmutableSet<AbiName>> allAbiNameSets = apexFileNamesBuilder.build().stream().map(ApexBundleValidator::abiNamesFromFile).collect(toImmutableSet());
    if (allAbiNameSets.size() != apexImages.size()) {
        throw InvalidBundleException.builder().withUserMessage("Every APEX image file must target a unique set of architectures, " + "but found multiple files that target the same set of architectures.").build();
    }
    ImmutableSet<String> expectedImages = apexBuildInfos.stream().map(f -> f.replace(BundleModule.BUILD_INFO_SUFFIX, BundleModule.APEX_IMAGE_SUFFIX)).collect(toImmutableSet());
    if (!apexBuildInfos.isEmpty() && !expectedImages.equals(apexImages)) {
        throw InvalidBundleException.builder().withUserMessage("If APEX build info is provided then one must be provided for each APEX image file:\n" + " Expected %s\n" + " Found %s.", expectedImages, apexImages).build();
    }
    if (REQUIRED_ONE_OF_ABI_SETS.stream().anyMatch(one_of -> one_of.stream().noneMatch(allAbiNameSets::contains))) {
        throw InvalidBundleException.builder().withUserMessage("APEX bundle must contain one of %s.", Joiner.on(" and one of ").join(REQUIRED_ONE_OF_ABI_SETS)).build();
    }
    module.getApexConfig().ifPresent(targeting -> validateTargeting(apexImages, targeting));
}
Also used : AbiName(com.android.tools.build.bundletool.model.AbiName) JsonObject(com.google.gson.JsonObject) ApexManifest(com.android.apex.ApexManifestProto.ApexManifest) ZipPath(com.android.tools.build.bundletool.model.ZipPath) JsonParser(com.google.gson.JsonParser) ARMEABI_V7A(com.android.tools.build.bundletool.model.AbiName.ARMEABI_V7A) JsonElement(com.google.gson.JsonElement) TargetedApexImage(com.android.bundle.Files.TargetedApexImage) ImmutableList(com.google.common.collect.ImmutableList) ARM64_V8A(com.android.tools.build.bundletool.model.AbiName.ARM64_V8A) X86(com.android.tools.build.bundletool.model.AbiName.X86) X86_64(com.android.tools.build.bundletool.model.AbiName.X86_64) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) APEX_MANIFEST_JSON_PATH(com.android.tools.build.bundletool.model.BundleModule.APEX_MANIFEST_JSON_PATH) APEX_PUBKEY_PATH(com.android.tools.build.bundletool.model.BundleModule.APEX_PUBKEY_PATH) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ABI_SPLITTER(com.android.tools.build.bundletool.model.BundleModule.ABI_SPLITTER) ImmutableSet(com.google.common.collect.ImmutableSet) UTF_8(java.nio.charset.StandardCharsets.UTF_8) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) IOException(java.io.IOException) Sets(com.google.common.collect.Sets) UncheckedIOException(java.io.UncheckedIOException) ApexImages(com.android.bundle.Files.ApexImages) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) Optional(java.util.Optional) BufferedReader(java.io.BufferedReader) APEX_DIRECTORY(com.android.tools.build.bundletool.model.BundleModule.APEX_DIRECTORY) APEX_NOTICE_PATH(com.android.tools.build.bundletool.model.BundleModule.APEX_NOTICE_PATH) BundleModule(com.android.tools.build.bundletool.model.BundleModule) APEX_MANIFEST_PATH(com.android.tools.build.bundletool.model.BundleModule.APEX_MANIFEST_PATH) Joiner(com.google.common.base.Joiner) InputStream(java.io.InputStream) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ImmutableSet(com.google.common.collect.ImmutableSet) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ZipPath(com.android.tools.build.bundletool.model.ZipPath)

Example 53 with BundleModule

use of com.android.tools.build.bundletool.model.BundleModule 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 54 with BundleModule

use of com.android.tools.build.bundletool.model.BundleModule in project bundletool by google.

the class BundleModulesValidator method validate.

public ImmutableList<BundleModule> validate(ImmutableList<ZipFile> moduleZips, BundleConfig bundleConfig) {
    for (ZipFile moduleZip : moduleZips) {
        new ValidatorRunner(MODULE_FILE_SUB_VALIDATORS).validateModuleZipFile(moduleZip);
    }
    ImmutableList<BundleModule> modules = moduleZips.stream().map(module -> toBundleModule(module, bundleConfig)).collect(toImmutableList());
    new ValidatorRunner(MODULES_SUB_VALIDATORS).validateBundleModules(modules);
    return modules;
}
Also used : BundleModuleName(com.android.tools.build.bundletool.model.BundleModuleName) ZipPath(com.android.tools.build.bundletool.model.ZipPath) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Predicates.not(com.google.common.base.Predicates.not) ImmutableList(com.google.common.collect.ImmutableList) BundleConfig(com.android.bundle.Config.BundleConfig) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ZipFile(java.util.zip.ZipFile) VisibleForTesting(com.google.common.annotations.VisibleForTesting) AppBundle(com.android.tools.build.bundletool.model.AppBundle) BundleModule(com.android.tools.build.bundletool.model.BundleModule) ZipUtils(com.android.tools.build.bundletool.model.utils.ZipUtils) ZipEntry(java.util.zip.ZipEntry) ZipFile(java.util.zip.ZipFile) BundleModule(com.android.tools.build.bundletool.model.BundleModule)

Example 55 with BundleModule

use of com.android.tools.build.bundletool.model.BundleModule in project bundletool by google.

the class BundleModulesValidator method toBundleModule.

private BundleModule toBundleModule(ZipFile moduleZipFile, BundleConfig bundleConfig) {
    BundleModule bundleModule = BundleModule.builder().setName(BundleModuleName.create("TEMPORARY_MODULE_NAME")).setBundleConfig(bundleConfig).addEntries(moduleZipFile.stream().filter(not(ZipEntry::isDirectory)).map(zipEntry -> ModuleEntry.builder().setPath(ZipPath.create(zipEntry.getName())).setContent(ZipUtils.asByteSource(moduleZipFile, zipEntry)).build()).collect(toImmutableList())).build();
    BundleModuleName actualModuleName = bundleModule.getAndroidManifest().getSplitId().map(BundleModuleName::create).orElse(BundleModuleName.BASE_MODULE_NAME);
    return bundleModule.toBuilder().setName(actualModuleName).build();
}
Also used : BundleModuleName(com.android.tools.build.bundletool.model.BundleModuleName) ZipPath(com.android.tools.build.bundletool.model.ZipPath) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Predicates.not(com.google.common.base.Predicates.not) ImmutableList(com.google.common.collect.ImmutableList) BundleConfig(com.android.bundle.Config.BundleConfig) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ZipFile(java.util.zip.ZipFile) VisibleForTesting(com.google.common.annotations.VisibleForTesting) AppBundle(com.android.tools.build.bundletool.model.AppBundle) BundleModule(com.android.tools.build.bundletool.model.BundleModule) ZipUtils(com.android.tools.build.bundletool.model.utils.ZipUtils) ZipEntry(java.util.zip.ZipEntry) BundleModuleName(com.android.tools.build.bundletool.model.BundleModuleName) BundleModule(com.android.tools.build.bundletool.model.BundleModule)

Aggregations

BundleModule (com.android.tools.build.bundletool.model.BundleModule)404 Test (org.junit.Test)370 BundleModuleBuilder (com.android.tools.build.bundletool.testing.BundleModuleBuilder)321 ModuleSplit (com.android.tools.build.bundletool.model.ModuleSplit)158 InvalidBundleException (com.android.tools.build.bundletool.model.exceptions.InvalidBundleException)112 ResourceTableBuilder (com.android.tools.build.bundletool.testing.ResourceTableBuilder)42 ResourceTable (com.android.aapt.Resources.ResourceTable)40 ApkTargeting (com.android.bundle.Targeting.ApkTargeting)29 ImmutableList (com.google.common.collect.ImmutableList)27 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)24 ImmutableSet (com.google.common.collect.ImmutableSet)22 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)21 DensityAlias (com.android.bundle.Targeting.ScreenDensity.DensityAlias)19 AppBundle (com.android.tools.build.bundletool.model.AppBundle)15 ManifestProtoUtils.androidManifest (com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifest)15 TestUtils.extractPaths (com.android.tools.build.bundletool.testing.TestUtils.extractPaths)15 Truth.assertThat (com.google.common.truth.Truth.assertThat)15 RunWith (org.junit.runner.RunWith)15 Assets (com.android.bundle.Files.Assets)14 VariantTargeting (com.android.bundle.Targeting.VariantTargeting)14