Search in sources :

Example 1 with ApkTargeting

use of com.android.bundle.Targeting.ApkTargeting in project bundletool by google.

the class TargetingUtilsTest method apkMultiAbiTargeting_byMultipleAbisAndAlternatives.

@Test
public void apkMultiAbiTargeting_byMultipleAbisAndAlternatives() {
    ApkTargeting expectedTargeting = ApkTargeting.newBuilder().setMultiAbiTargeting(MULTI_ABI_WITH_ALTERNATIVES).build();
    assertThat(TargetingUtils.apkMultiAbiTargeting(ImmutableSet.of(ImmutableSet.of(X86), ImmutableSet.of(ARMEABI_V7A, ARM64_V8A)), ImmutableSet.of(ImmutableSet.of(X86_64)))).ignoringRepeatedFieldOrder().isEqualTo(expectedTargeting);
}
Also used : ApkTargeting(com.android.bundle.Targeting.ApkTargeting) Test(org.junit.Test)

Example 2 with ApkTargeting

use of com.android.bundle.Targeting.ApkTargeting in project bundletool by google.

the class LanguageResourcesSplitterTest method resourcesPinnedToMasterSplit_noSplitting_fileReferences.

@Test
public void resourcesPinnedToMasterSplit_noSplitting_fileReferences() throws Exception {
    ResourceTable resourceTable = new ResourceTableBuilder().addPackage("com.test.app").addResource("drawable", "image", fileReference("res/drawable-en-hdpi/image.jpg", mergeConfigs(locale("en"), HDPI)), fileReference("res/drawable-fr-hdpi/image.jpg", mergeConfigs(locale("fr"), HDPI))).addResource("drawable", "image2", fileReference("res/drawable-en-hdpi/image2.jpg", mergeConfigs(locale("en"), HDPI)), fileReference("res/drawable-fr-hdpi/image2.jpg", mergeConfigs(locale("fr"), HDPI))).build();
    BundleModule module = new BundleModuleBuilder("testModule").setResourceTable(resourceTable).setManifest(androidManifest("com.test.app")).addFile("res/drawable-en-hdpi/image.jpg").addFile("res/drawable-fr-hdpi/image.jpg").addFile("res/drawable-en-hdpi/image2.jpg").addFile("res/drawable-fr-hdpi/image2.jpg").build();
    ModuleSplit baseSplit = ModuleSplit.forResources(module);
    LanguageResourcesSplitter languageSplitter = new LanguageResourcesSplitter(resource -> resource.getResourceId().getFullResourceId() == 0x7f010000);
    Collection<ModuleSplit> languageSplits = languageSplitter.split(baseSplit);
    ModuleSplit masterSplit = languageSplits.stream().filter(split -> split.isMasterSplit()).collect(onlyElement());
    assertThat(masterSplit.getResourceTable()).hasValue(new ResourceTableBuilder().addPackage("com.test.app").addResource("drawable", "image", fileReference("res/drawable-en-hdpi/image.jpg", mergeConfigs(locale("en"), HDPI)), fileReference("res/drawable-fr-hdpi/image.jpg", mergeConfigs(locale("fr"), HDPI))).build());
    assertThat(extractPaths(masterSplit.getEntries())).containsExactly("res/drawable-en-hdpi/image.jpg", "res/drawable-fr-hdpi/image.jpg");
    ImmutableList<ModuleSplit> configSplits = languageSplits.stream().filter(split -> !split.isMasterSplit()).collect(toImmutableList());
    ImmutableMap<ApkTargeting, ModuleSplit> configSplitMap = Maps.uniqueIndex(configSplits, ModuleSplit::getApkTargeting);
    assertThat(configSplitMap.keySet()).containsExactly(apkLanguageTargeting("en"), apkLanguageTargeting("fr"));
    ModuleSplit enSplit = configSplitMap.get(apkLanguageTargeting("en"));
    assertThat(enSplit.getResourceTable()).hasValue(ResourcesTableFactory.createResourceTable(/* entryId= */
    0x01, "image2", fileReference("res/drawable-en-hdpi/image2.jpg", mergeConfigs(locale("en"), HDPI))));
    assertThat(extractPaths(enSplit.getEntries())).containsExactly("res/drawable-en-hdpi/image2.jpg");
    ModuleSplit frSplit = configSplitMap.get(apkLanguageTargeting("fr"));
    assertThat(frSplit.getResourceTable()).hasValue(ResourcesTableFactory.createResourceTable(/* entryId= */
    0x01, "image2", fileReference("res/drawable-fr-hdpi/image2.jpg", mergeConfigs(locale("fr"), HDPI))));
    assertThat(extractPaths(frSplit.getEntries())).containsExactly("res/drawable-fr-hdpi/image2.jpg");
}
Also used : Iterables(com.google.common.collect.Iterables) MDPI(com.android.tools.build.bundletool.testing.ResourcesTableFactory.MDPI) ApkTargeting(com.android.bundle.Targeting.ApkTargeting) ManifestProtoUtils.androidManifest(com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifest) ResourcesTableFactory.locale(com.android.tools.build.bundletool.testing.ResourcesTableFactory.locale) RunWith(org.junit.runner.RunWith) TestUtils.extractPaths(com.android.tools.build.bundletool.testing.TestUtils.extractPaths) ResourcesTableFactory.resourceTable(com.android.tools.build.bundletool.testing.ResourcesTableFactory.resourceTable) MoreCollectors.onlyElement(com.google.common.collect.MoreCollectors.onlyElement) BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder) ProtoTruth.assertThat(com.google.common.truth.extensions.proto.ProtoTruth.assertThat) ImmutableList(com.google.common.collect.ImmutableList) ResourceTableBuilder(com.android.tools.build.bundletool.testing.ResourceTableBuilder) ResourcesTableFactory.mergeConfigs(com.android.tools.build.bundletool.testing.ResourcesTableFactory.mergeConfigs) Map(java.util.Map) ResourcesTableFactory.entry(com.android.tools.build.bundletool.testing.ResourcesTableFactory.entry) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Predicates(com.google.common.base.Predicates) DensityAlias(com.android.bundle.Targeting.ScreenDensity.DensityAlias) ResourcesTableFactory.pkg(com.android.tools.build.bundletool.testing.ResourcesTableFactory.pkg) ResourcesTableFactory.type(com.android.tools.build.bundletool.testing.ResourcesTableFactory.type) Truth8.assertThat(com.google.common.truth.Truth8.assertThat) ResourceTable(com.android.aapt.Resources.ResourceTable) ResourcesTableFactory.value(com.android.tools.build.bundletool.testing.ResourcesTableFactory.value) ConfigValue(com.android.aapt.Resources.ConfigValue) ImmutableMap(com.google.common.collect.ImmutableMap) USER_PACKAGE_OFFSET(com.android.tools.build.bundletool.testing.ResourcesTableFactory.USER_PACKAGE_OFFSET) HDPI(com.android.tools.build.bundletool.testing.ResourcesTableFactory.HDPI) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Collection(java.util.Collection) TestCase.fail(junit.framework.TestCase.fail) ResourcesTableFactory(com.android.tools.build.bundletool.testing.ResourcesTableFactory) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) SplitType(com.android.tools.build.bundletool.model.ModuleSplit.SplitType) Maps(com.google.common.collect.Maps) ResourcesTableFactory.fileReference(com.android.tools.build.bundletool.testing.ResourcesTableFactory.fileReference) TargetingUtils.apkDensityTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.apkDensityTargeting) ByteString(com.google.protobuf.ByteString) StringPool(com.android.aapt.Resources.StringPool) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) Ignore(org.junit.Ignore) TargetingUtils.mergeApkTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.mergeApkTargeting) BundleModule(com.android.tools.build.bundletool.model.BundleModule) Configuration(com.android.aapt.ConfigurationOuterClass.Configuration) TargetingUtils.apkLanguageTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.apkLanguageTargeting) ApkTargeting(com.android.bundle.Targeting.ApkTargeting) TargetingUtils.mergeApkTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.mergeApkTargeting) BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) ResourceTableBuilder(com.android.tools.build.bundletool.testing.ResourceTableBuilder) ResourceTable(com.android.aapt.Resources.ResourceTable) BundleModule(com.android.tools.build.bundletool.model.BundleModule) Test(org.junit.Test)

Example 3 with ApkTargeting

use of com.android.bundle.Targeting.ApkTargeting in project bundletool by google.

the class SplitApksGeneratorTest method multipleModules_multipleVariants_withTransparency.

@Test
public void multipleModules_multipleVariants_withTransparency() throws Exception {
    TestComponent.useTestModule(this, TestModule.builder().withBundleMetadata(BUNDLE_METADATA_WITH_TRANSPARENCY).build());
    ImmutableList<BundleModule> bundleModule = ImmutableList.of(new BundleModuleBuilder("base").addFile("assets/leftover.txt").addFile("lib/x86_64/libsome.so").setManifest(androidManifest("com.test.app")).setNativeConfig(nativeLibraries(targetedNativeDirectory("lib/x86_64", nativeDirectoryTargeting(AbiAlias.X86_64)))).build(), new BundleModuleBuilder("test").addFile("assets/test.txt").setManifest(androidManifest("com.test.app")).build());
    ImmutableList<ModuleSplit> moduleSplits = splitApksGenerator.generateSplits(bundleModule, ApkGenerationConfiguration.builder().setOptimizationDimensions(ImmutableSet.of(OptimizationDimension.ABI)).build());
    ApkTargeting minSdkLTargeting = apkMinSdkTargeting(/* minSdkVersion= */
    ANDROID_L_API_VERSION);
    ApkTargeting minSdkLWithAbiTargeting = mergeApkTargeting(apkAbiTargeting(AbiAlias.X86_64), minSdkLTargeting);
    // 1 main and 1 ABI split for base module + test module.
    assertThat(moduleSplits).hasSize(3);
    assertThat(moduleSplits.stream().map(ModuleSplit::getApkTargeting).collect(toImmutableSet())).containsExactly(minSdkLTargeting, minSdkLWithAbiTargeting);
    ModuleSplit mainSplitOfBaseModule = getModuleSplit(moduleSplits, minSdkLTargeting, /* moduleName= */
    "base");
    assertThat(extractPaths(mainSplitOfBaseModule.getEntries())).containsExactly("assets/leftover.txt", "META-INF/code_transparency_signed.jwt");
    ModuleSplit abiSplitOfBaseModule = getModuleSplit(moduleSplits, minSdkLWithAbiTargeting, /* moduleName= */
    "base");
    assertThat(extractPaths(abiSplitOfBaseModule.getEntries())).containsExactly("lib/x86_64/libsome.so");
    ModuleSplit testModule = getModuleSplit(moduleSplits, minSdkLTargeting, /* moduleName= */
    "test");
    assertThat(extractPaths(testModule.getEntries())).containsExactly("assets/test.txt");
}
Also used : ApkTargeting(com.android.bundle.Targeting.ApkTargeting) TargetingUtils.mergeApkTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.mergeApkTargeting) 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 4 with ApkTargeting

use of com.android.bundle.Targeting.ApkTargeting in project bundletool by google.

the class StandaloneApksGeneratorTest method manyModulesShardByAbiAndDensity_havingManyAbisAndSomeResource_producesManyApks.

@Test
public void manyModulesShardByAbiAndDensity_havingManyAbisAndSomeResource_producesManyApks() throws Exception {
    BundleModule baseModule = new BundleModuleBuilder("base").addFile("assets/file.txt").addFile("dex/classes.dex").addFile("res/drawable/image.jpg").addFile("res/drawable-hdpi/image.jpg").addFile("root/license.dat").setManifest(androidManifest("com.test.app")).setResourceTable(new ResourceTableBuilder().addPackage("com.test.app").addDrawableResourceForMultipleDensities("image", ImmutableMap.of(DEFAULT_DENSITY_VALUE, "res/drawable/image.jpg", HDPI_VALUE, "res/drawable-hdpi/image.jpg")).build()).build();
    BundleModule featureModule = new BundleModuleBuilder("feature").addFile("lib/armeabi/libtest.so").addFile("lib/x86/libtest.so").setManifest(androidManifest("com.test.app")).setNativeConfig(nativeLibraries(targetedNativeDirectory("lib/armeabi", nativeDirectoryTargeting(ARMEABI)), targetedNativeDirectory("lib/x86", nativeDirectoryTargeting(X86)))).build();
    ImmutableList<ModuleSplit> shards = standaloneApksGenerator.generateStandaloneApks(ImmutableList.of(baseModule, featureModule), standaloneApkOptimizations(OptimizationDimension.ABI, OptimizationDimension.SCREEN_DENSITY));
    assertThat(shards).hasSize(2 * 7);
    assertThat(shards.stream().map(ModuleSplit::getSplitType).distinct().collect(toImmutableSet())).containsExactly(SplitType.STANDALONE);
    ApkTargeting armTargeting = apkAbiTargeting(ARMEABI, ImmutableSet.of(X86));
    ApkTargeting x86Targeting = apkAbiTargeting(X86, ImmutableSet.of(ARMEABI));
    assertThat(shards.stream().map(ModuleSplit::getApkTargeting).collect(toImmutableList())).ignoringRepeatedFieldOrder().containsExactly(mergeApkTargeting(armTargeting, LDPI_TARGETING), mergeApkTargeting(armTargeting, MDPI_TARGETING), mergeApkTargeting(armTargeting, HDPI_TARGETING), mergeApkTargeting(armTargeting, XHDPI_TARGETING), mergeApkTargeting(armTargeting, XXHDPI_TARGETING), mergeApkTargeting(armTargeting, XXXHDPI_TARGETING), mergeApkTargeting(armTargeting, TVDPI_TARGETING), mergeApkTargeting(x86Targeting, LDPI_TARGETING), mergeApkTargeting(x86Targeting, MDPI_TARGETING), mergeApkTargeting(x86Targeting, HDPI_TARGETING), mergeApkTargeting(x86Targeting, XHDPI_TARGETING), mergeApkTargeting(x86Targeting, XXHDPI_TARGETING), mergeApkTargeting(x86Targeting, XXXHDPI_TARGETING), mergeApkTargeting(x86Targeting, TVDPI_TARGETING));
    // Check files not specific to ABI nor screen density.
    for (ModuleSplit shard : shards) {
        assertThat(extractPaths(shard.getEntries())).containsAtLeast("assets/file.txt", "dex/classes.dex", "root/license.dat");
    }
    // Check resources.
    for (ModuleSplit shard : shards) {
        DensityAlias density = shard.getApkTargeting().getScreenDensityTargeting().getValueList().get(0).getDensityAlias();
        switch(density) {
            case LDPI:
            case MDPI:
                assertThat(extractPaths(shard.findEntriesUnderPath("res/"))).containsExactly("res/drawable/image.jpg");
                assertThat(shard.getResourceTable().get()).containsResource("com.test.app:drawable/image").onlyWithConfigs(Configuration.getDefaultInstance());
                break;
            case TVDPI:
                // TVDPI is a special case because the bucket encompasses devices that could serve either
                // the MDPI or the HDPI resource, so both resources are present.
                assertThat(extractPaths(shard.findEntriesUnderPath("res/"))).containsExactly("res/drawable/image.jpg", "res/drawable-hdpi/image.jpg");
                assertThat(shard.getResourceTable().get()).containsResource("com.test.app:drawable/image").onlyWithConfigs(Configuration.getDefaultInstance(), HDPI);
                break;
            case HDPI:
            case XHDPI:
            case XXHDPI:
            case XXXHDPI:
                assertThat(extractPaths(shard.findEntriesUnderPath("res/"))).containsExactly("res/drawable-hdpi/image.jpg");
                assertThat(shard.getResourceTable().get()).containsResource("com.test.app:drawable/image").onlyWithConfigs(HDPI);
                break;
            default:
                fail("Unexpected screen density targeting: " + density);
        }
    }
    // Check native libraries.
    for (ModuleSplit shard : shards) {
        switch(shard.getApkTargeting().getAbiTargeting().getValue(0).getAlias()) {
            case ARMEABI:
                assertThat(extractPaths(shard.getEntries())).contains("lib/armeabi/libtest.so");
                assertThat(extractPaths(shard.getEntries())).containsNoneOf("lib/x86/libtest.so", "lib/x86_64/libtest.so");
                break;
            case X86:
                assertThat(extractPaths(shard.getEntries())).contains("lib/x86/libtest.so");
                assertThat(extractPaths(shard.getEntries())).containsNoneOf("lib/armeabi/libtest.so", "lib/x86_64/libtest.so");
                break;
            default:
                fail("Unexpected ABI targeting in: " + shard.getApkTargeting());
        }
    }
}
Also used : ApkTargeting(com.android.bundle.Targeting.ApkTargeting) TargetingUtils.mergeApkTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.mergeApkTargeting) BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) ResourceTableBuilder(com.android.tools.build.bundletool.testing.ResourceTableBuilder) DensityAlias(com.android.bundle.Targeting.ScreenDensity.DensityAlias) BundleModule(com.android.tools.build.bundletool.model.BundleModule) Test(org.junit.Test)

Example 5 with ApkTargeting

use of com.android.bundle.Targeting.ApkTargeting in project bundletool by google.

the class ModuleSplitterTest method nativeSplits_areGenerated.

@Test
public void nativeSplits_areGenerated() throws Exception {
    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").build();
    ImmutableList<ModuleSplit> splits = createAbiAndDensitySplitter(testModule).splitModule();
    assertThat(splits.stream().map(ModuleSplit::getSplitType).distinct().collect(toImmutableSet())).containsExactly(SplitType.SPLIT);
    assertThat(splits.stream().map(ModuleSplit::getVariantTargeting).distinct().collect(toImmutableSet())).containsExactly(lPlusVariantTargeting());
    assertThat(splits).hasSize(2);
    boolean hasMasterSplit = false;
    boolean hasX86Split = false;
    ApkTargeting x86SplitTargeting = mergeApkTargeting(DEFAULT_MASTER_SPLIT_SDK_TARGETING, apkAbiTargeting(AbiAlias.X86));
    for (ModuleSplit split : splits) {
        if (split.getApkTargeting().equals(DEFAULT_MASTER_SPLIT_SDK_TARGETING)) {
            assertThat(split.isMasterSplit()).isTrue();
            hasMasterSplit = true;
        } else if (split.getApkTargeting().equals(x86SplitTargeting)) {
            assertThat(split.isMasterSplit()).isFalse();
            hasX86Split = true;
        } else {
            fail(String.format("Unexpected split targeting: %s", split.getApkTargeting()));
        }
    }
    assertThat(hasMasterSplit).isTrue();
    assertThat(hasX86Split).isTrue();
}
Also used : ApkTargeting(com.android.bundle.Targeting.ApkTargeting) TargetingUtils.mergeApkTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.mergeApkTargeting) 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)

Aggregations

ApkTargeting (com.android.bundle.Targeting.ApkTargeting)49 Test (org.junit.Test)44 TargetingUtils.mergeApkTargeting (com.android.tools.build.bundletool.testing.TargetingUtils.mergeApkTargeting)28 ModuleSplit (com.android.tools.build.bundletool.model.ModuleSplit)22 BundleModule (com.android.tools.build.bundletool.model.BundleModule)18 BundleModuleBuilder (com.android.tools.build.bundletool.testing.BundleModuleBuilder)18 ResourceTableBuilder (com.android.tools.build.bundletool.testing.ResourceTableBuilder)10 ResourceTable (com.android.aapt.Resources.ResourceTable)8 DensityAlias (com.android.bundle.Targeting.ScreenDensity.DensityAlias)7 ImmutableList (com.google.common.collect.ImmutableList)7 ImmutableSet (com.google.common.collect.ImmutableSet)7 ApexImages (com.android.bundle.Files.ApexImages)6 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)6 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)6 Configuration (com.android.aapt.ConfigurationOuterClass.Configuration)5 ZipPath (com.android.tools.build.bundletool.model.ZipPath)5 ManifestProtoUtils.androidManifest (com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifest)5 MDPI (com.android.tools.build.bundletool.testing.ResourcesTableFactory.MDPI)5 USER_PACKAGE_OFFSET (com.android.tools.build.bundletool.testing.ResourcesTableFactory.USER_PACKAGE_OFFSET)5 ImmutableMap (com.google.common.collect.ImmutableMap)5