Search in sources :

Example 11 with BundleModuleName

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

the class FusingAndroidManifestMergerTest method onlyBaseManifest.

@Test
@Theory
public void onlyBaseManifest(FusingAndroidManifestMerger.Mode mode) {
    SetMultimap<BundleModuleName, AndroidManifest> manifests = createManifests(androidManifest("com.testapp", withDebuggableAttribute(true), withCustomThemeActivity("activity", BASE_THEME_REF_ID)));
    AndroidManifest merged = createMerger(mode).merge(manifests);
    assertThat(merged).isEqualTo(getOnlyElement(manifests.get(BASE_MODULE_NAME)));
}
Also used : BundleModuleName(com.android.tools.build.bundletool.model.BundleModuleName) AndroidManifest(com.android.tools.build.bundletool.model.AndroidManifest) Test(org.junit.Test) Theory(org.junit.experimental.theories.Theory)

Example 12 with BundleModuleName

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

the class FusingAndroidManifestMergerTest method noBaseManifest_throws.

@Test
public void noBaseManifest_throws() {
    SetMultimap<BundleModuleName, AndroidManifest> manifests = ImmutableSetMultimap.of(BundleModuleName.create("feature"), AndroidManifest.create(androidManifest("com.testapp")));
    CommandExecutionException exception = assertThrows(CommandExecutionException.class, () -> createMerger(Mode.REPLACE).merge(manifests));
    assertThat(exception).hasMessageThat().isEqualTo("Expected to have base module.");
}
Also used : BundleModuleName(com.android.tools.build.bundletool.model.BundleModuleName) AndroidManifest(com.android.tools.build.bundletool.model.AndroidManifest) CommandExecutionException(com.android.tools.build.bundletool.model.exceptions.CommandExecutionException) Test(org.junit.Test)

Example 13 with BundleModuleName

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

the class BundleParser method extractModules.

/**
 * Extracts all modules from bundle zip file and returns them in a list
 */
public static ImmutableList<BundleModule> extractModules(ZipFile bundleFile, BundleConfig bundleConfig, ImmutableSet<ZipPath> nonModuleDirectories) {
    Map<BundleModuleName, BundleModule.Builder> moduleBuilders = new HashMap<>();
    Enumeration<? extends ZipEntry> entries = bundleFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            continue;
        }
        Optional<BundleModuleName> moduleName = extractModuleName(entry, nonModuleDirectories);
        if (!moduleName.isPresent()) {
            continue;
        }
        BundleModule.Builder moduleBuilder = moduleBuilders.computeIfAbsent(moduleName.get(), name -> BundleModule.builder().setName(name).setBundleConfig(bundleConfig));
        moduleBuilder.addEntry(ModuleEntry.builder().setBundleLocation(ModuleEntryBundleLocation.create(Paths.get(bundleFile.getName()), ZipPath.create(entry.getName()))).setPath(ZipUtils.convertBundleToModulePath(ZipPath.create(entry.getName()))).setContent(ZipUtils.asByteSource(bundleFile, entry)).build());
    }
    // We verify the presence of the manifest before building the BundleModule objects because the
    // manifest is a required field of the BundleModule class.
    checkModulesHaveManifest(moduleBuilders.values());
    return moduleBuilders.values().stream().map(BundleModule.Builder::build).collect(toImmutableList());
}
Also used : BundleModuleName(com.android.tools.build.bundletool.model.BundleModuleName) HashMap(java.util.HashMap) ZipEntry(java.util.zip.ZipEntry) BundleModule(com.android.tools.build.bundletool.model.BundleModule)

Example 14 with BundleModuleName

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

the class FusingAndroidManifestMerger method ensureOneManifestPerModule.

private static ImmutableMap<BundleModuleName, AndroidManifest> ensureOneManifestPerModule(SetMultimap<BundleModuleName, AndroidManifest> manifests) {
    ImmutableMap.Builder<BundleModuleName, AndroidManifest> builder = ImmutableMap.builder();
    for (BundleModuleName moduleName : manifests.keys()) {
        Set<AndroidManifest> moduleManifests = manifests.get(moduleName);
        if (moduleManifests.size() != 1) {
            throw CommandExecutionException.builder().withInternalMessage("Expected exactly one %s module manifest, but found %d.", moduleName.getName(), moduleManifests.size()).build();
        }
        builder.put(moduleName, Iterables.getOnlyElement(moduleManifests));
    }
    return builder.build();
}
Also used : BundleModuleName(com.android.tools.build.bundletool.model.BundleModuleName) AndroidManifest(com.android.tools.build.bundletool.model.AndroidManifest) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap)

Example 15 with BundleModuleName

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

the class FusingAndroidManifestMerger method merge.

private AndroidManifest merge(Map<BundleModuleName, AndroidManifest> manifests) {
    AndroidManifest baseManifest = manifests.get(BASE_MODULE_NAME);
    List<AndroidManifest> featureManifests = manifests.entrySet().stream().filter(entry -> !BASE_MODULE_NAME.equals(entry.getKey())).sorted(Comparator.comparing(entry -> entry.getKey().getName())).map(Map.Entry::getValue).collect(toImmutableList());
    if (featureManifests.isEmpty()) {
        return baseManifest;
    }
    return mergeManifests(baseManifest, featureManifests);
}
Also used : Iterables(com.google.common.collect.Iterables) BundleModuleName(com.android.tools.build.bundletool.model.BundleModuleName) AndroidManifest(com.android.tools.build.bundletool.model.AndroidManifest) XmlProtoAttributeBuilder(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoAttributeBuilder) BASE_MODULE_NAME(com.android.tools.build.bundletool.model.BundleModuleName.BASE_MODULE_NAME) Function(java.util.function.Function) Predicates.not(com.google.common.base.Predicates.not) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) XmlProtoNodeBuilder(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNodeBuilder) XmlProtoElement(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElement) MoreCollectors.toOptional(com.google.common.collect.MoreCollectors.toOptional) XmlProtoElementBuilder(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElementBuilder) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) CommandExecutionException(com.android.tools.build.bundletool.model.exceptions.CommandExecutionException) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) SetMultimap(com.google.common.collect.SetMultimap) Sets(com.google.common.collect.Sets) Streams.stream(com.google.common.collect.Streams.stream) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) List(java.util.List) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) AutoValue(com.google.auto.value.AutoValue) Optional(java.util.Optional) ManifestEditor(com.android.tools.build.bundletool.model.ManifestEditor) Comparator(java.util.Comparator) AndroidManifest(com.android.tools.build.bundletool.model.AndroidManifest) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap)

Aggregations

BundleModuleName (com.android.tools.build.bundletool.model.BundleModuleName)26 AndroidManifest (com.android.tools.build.bundletool.model.AndroidManifest)19 Test (org.junit.Test)14 ZipPath (com.android.tools.build.bundletool.model.ZipPath)6 BundleModule (com.android.tools.build.bundletool.model.BundleModule)5 ModuleSplit (com.android.tools.build.bundletool.model.ModuleSplit)5 ImmutableList (com.google.common.collect.ImmutableList)5 AppBundle (com.android.tools.build.bundletool.model.AppBundle)4 ModuleEntry (com.android.tools.build.bundletool.model.ModuleEntry)4 HashMap (java.util.HashMap)4 BundleConfig (com.android.bundle.Config.BundleConfig)3 VariantTargeting (com.android.bundle.Targeting.VariantTargeting)3 CommandExecutionException (com.android.tools.build.bundletool.model.exceptions.CommandExecutionException)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 ResourceTable (com.android.aapt.Resources.ResourceTable)2 DeviceSpec (com.android.bundle.Devices.DeviceSpec)2 TargetedAssetsDirectory (com.android.bundle.Files.TargetedAssetsDirectory)2 Predicates.not (com.google.common.base.Predicates.not)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)2