Search in sources :

Example 56 with BundleModule

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

the class SplitApksGenerator method generateSplitApks.

private ImmutableList<ModuleSplit> generateSplitApks(ImmutableList<BundleModule> modules, ApkGenerationConfiguration apkGenerationConfiguration, VariantTargeting variantTargeting) {
    ImmutableSet<String> allModuleNames = modules.stream().map(module -> module.getName().getName()).collect(toImmutableSet());
    ImmutableList.Builder<ModuleSplit> splits = ImmutableList.builder();
    for (BundleModule module : modules) {
        ModuleSplitter moduleSplitter = ModuleSplitter.create(module, bundletoolVersion, appBundle, apkGenerationConfiguration, variantTargeting, allModuleNames, stampSource.map(SourceStamp::getSource), StampType.STAMP_TYPE_DISTRIBUTION_APK);
        splits.addAll(moduleSplitter.splitModule());
    }
    return splits.build();
}
Also used : SourceStamp(com.android.tools.build.bundletool.model.SourceStamp) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) StampType(com.android.tools.build.bundletool.model.SourceStamp.StampType) Inject(javax.inject.Inject) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) ImmutableList(com.google.common.collect.ImmutableList) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Optional(java.util.Optional) TargetingUtils.generateAllVariantTargetings(com.android.tools.build.bundletool.model.targeting.TargetingUtils.generateAllVariantTargetings) Version(com.android.tools.build.bundletool.model.version.Version) AppBundle(com.android.tools.build.bundletool.model.AppBundle) BundleModule(com.android.tools.build.bundletool.model.BundleModule) VariantTargeting(com.android.bundle.Targeting.VariantTargeting) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) BundleModule(com.android.tools.build.bundletool.model.BundleModule)

Example 57 with BundleModule

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

the class EntryClashValidator method validateAllModules.

@Override
public void validateAllModules(ImmutableList<BundleModule> modules) {
    if (!BundleValidationUtils.isAssetOnlyBundle(modules)) {
        BundleModule baseModule = BundleValidationUtils.expectBaseModule(modules);
        boolean isolatedSplits = baseModule.getAndroidManifest().getIsolatedSplits().orElse(false);
        if (isolatedSplits) {
            // manager instances.
            return;
        }
    }
    checkEntryClashes(modules);
}
Also used : BundleModule(com.android.tools.build.bundletool.model.BundleModule)

Example 58 with BundleModule

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

the class ModuleNamesValidator method validateAllModules.

@Override
public void validateAllModules(ImmutableList<BundleModule> modules) {
    Set<BundleModuleName> moduleNames = new HashSet<>();
    for (BundleModule module : modules) {
        Optional<String> splitId = module.getAndroidManifest().getSplitId();
        BundleModuleName moduleName = module.getName();
        boolean isFeatureModule = module.getAndroidManifest().getModuleType().isFeatureModule();
        if (moduleName.equals(BundleModuleName.BASE_MODULE_NAME)) {
            if (splitId.isPresent()) {
                throw InvalidBundleException.builder().withUserMessage("The base module should not have the 'split' attribute set in the " + "AndroidManifest.xml").build();
            }
        } else {
            if (splitId.isPresent()) {
                if (!splitId.get().equals(moduleName.getName())) {
                    throw InvalidBundleException.builder().withUserMessage("The 'split' attribute in the AndroidManifest.xml of modules must be the name " + "of the module, but has the value '%s' in module '%s'", splitId.get(), moduleName).build();
                }
            } else {
                throw InvalidBundleException.builder().withUserMessage("No 'split' attribute found in the AndroidManifest.xml of module '%s'.", moduleName).build();
            }
        }
        boolean alreadyPresent = !moduleNames.add(moduleName);
        if (alreadyPresent) {
            if (splitId.isPresent()) {
                throw InvalidBundleException.builder().withUserMessage("More than one module have the 'split' attribute set to '%s' in the " + "AndroidManifest.xml.", splitId.get()).build();
            } else if (isFeatureModule) {
                throw InvalidBundleException.builder().withUserMessage("More than one module was found without the 'split' attribute set in the" + " AndroidManifest.xml. Ensure that all the dynamic features have the" + " 'split' attribute correctly set in the AndroidManifest.xml.").build();
            }
        }
    }
    if (!BundleValidationUtils.isAssetOnlyBundle(modules) && !moduleNames.contains(BundleModuleName.BASE_MODULE_NAME)) {
        throw InvalidBundleException.builder().withUserMessage("No base module found. At least one module must not have a 'split' attribute set in" + " the AndroidManifest.xml.").build();
    }
}
Also used : BundleModuleName(com.android.tools.build.bundletool.model.BundleModuleName) HashSet(java.util.HashSet) BundleModule(com.android.tools.build.bundletool.model.BundleModule)

Example 59 with BundleModule

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

the class ModuleTitleValidator method checkModuleTitles.

private static void checkModuleTitles(ImmutableList<BundleModule> modules) {
    if (BundleValidationUtils.isAssetOnlyBundle(modules)) {
        return;
    }
    BundleModule baseModule = BundleValidationUtils.expectBaseModule(modules);
    boolean isolatedSplits = baseModule.getAndroidManifest().getIsolatedSplits().orElse(false);
    // For bundles built using older versions we haven't strictly enforced module Title Validation.
    Version bundleVersion = BundleToolVersion.getVersionFromBundleConfig(baseModule.getBundleConfig());
    if (!MODULE_TITLE_VALIDATION_ENFORCED.enabledForVersion(bundleVersion)) {
        return;
    }
    ResourceTable table = baseModule.getResourceTable().orElse(ResourceTable.getDefaultInstance());
    ImmutableSet<Integer> stringResourceIds = entries(table).filter(entry -> entry.getType().getName().equals("string")).map(entry -> entry.getResourceId().getFullResourceId()).collect(toImmutableSet());
    for (BundleModule module : modules) {
        if (module.getModuleType().equals(ModuleType.ASSET_MODULE)) {
            if (module.getAndroidManifest().getTitleRefId().isPresent()) {
                throw InvalidBundleException.builder().withUserMessage("Module titles not supported in asset packs, but found in '%s'.", module.getName()).build();
            }
        } else if (!module.getDeliveryType().equals(ModuleDeliveryType.ALWAYS_INITIAL_INSTALL)) {
            Optional<Integer> titleRefId = module.getAndroidManifest().getTitleRefId();
            if (isolatedSplits) {
                // references into the base split resource table.
                return;
            }
            if (!titleRefId.isPresent()) {
                throw InvalidBundleException.builder().withUserMessage("Mandatory title is missing in manifest for module '%s'.", module.getName()).build();
            }
            if (!stringResourceIds.contains(titleRefId.get())) {
                throw InvalidBundleException.builder().withUserMessage("Title for module '%s' is missing in the base resource table.", module.getName()).build();
            }
        }
    }
}
Also used : ModuleDeliveryType(com.android.tools.build.bundletool.model.ModuleDeliveryType) BundleToolVersion(com.android.tools.build.bundletool.model.version.BundleToolVersion) ImmutableSet(com.google.common.collect.ImmutableSet) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) MODULE_TITLE_VALIDATION_ENFORCED(com.android.tools.build.bundletool.model.version.VersionGuardedFeature.MODULE_TITLE_VALIDATION_ENFORCED) ResourcesUtils.entries(com.android.tools.build.bundletool.model.utils.ResourcesUtils.entries) ImmutableList(com.google.common.collect.ImmutableList) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Optional(java.util.Optional) ModuleType(com.android.tools.build.bundletool.model.BundleModule.ModuleType) Version(com.android.tools.build.bundletool.model.version.Version) BundleModule(com.android.tools.build.bundletool.model.BundleModule) ResourceTable(com.android.aapt.Resources.ResourceTable) Optional(java.util.Optional) BundleToolVersion(com.android.tools.build.bundletool.model.version.BundleToolVersion) Version(com.android.tools.build.bundletool.model.version.Version) BundleModule(com.android.tools.build.bundletool.model.BundleModule) ResourceTable(com.android.aapt.Resources.ResourceTable)

Example 60 with BundleModule

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

the class ResourceTableValidator method checkResourceIdsAreUnique.

private static void checkResourceIdsAreUnique(ImmutableList<BundleModule> modules) {
    HashSet<ResourceId> usedResourceIds = newHashSet();
    for (BundleModule module : modules) {
        ResourceTable resourceTable = module.getResourceTable().orElse(ResourceTable.getDefaultInstance());
        ResourcesUtils.entries(resourceTable).forEach(resourceTableEntry -> {
            boolean foundDuplicate = !usedResourceIds.add(resourceTableEntry.getResourceId());
            if (foundDuplicate) {
                throw InvalidBundleException.builder().withUserMessage("Duplicate resource id (%s).", resourceTableEntry.getResourceId().toString()).build();
            }
        });
    }
}
Also used : ResourceId(com.android.tools.build.bundletool.model.ResourceId) BundleModule(com.android.tools.build.bundletool.model.BundleModule) ResourceTable(com.android.aapt.Resources.ResourceTable)

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