use of com.android.tools.build.bundletool.model.AppBundle in project bundletool by google.
the class BuildApksManagerTest method pinningOfManifestReachableResources_enabledSince_0_8_1.
@Test
public void pinningOfManifestReachableResources_enabledSince_0_8_1() throws Exception {
AppBundle appBundle = new AppBundleBuilder().addModule("base", builder -> builder.addFile("res/drawable-mdpi/manifest_image.jpg").addFile("res/drawable-hdpi/manifest_image.jpg").addFile("res/drawable-mdpi/other_image.jpg").addFile("res/drawable-hdpi/other_image.jpg").setManifest(androidManifest("com.test.app", withAppIcon(/* ID of "drawable/manifest_image" */
0x7f010000))).setResourceTable(new ResourceTableBuilder().addPackage("com.test.app").addDrawableResourceForMultipleDensities("manifest_image", ImmutableMap.of(/* mdpi */
160, "res/drawable-mdpi/manifest_image.jpg", /* hdpi */
240, "res/drawable-hdpi/manifest_image.jpg")).addDrawableResourceForMultipleDensities("other_image", ImmutableMap.of(/* mdpi */
160, "res/drawable-mdpi/other_image.jpg", /* hdpi */
240, "res/drawable-hdpi/other_image.jpg")).build())).setBundleConfig(BundleConfigBuilder.create().setVersion("0.8.1").build()).build();
TestComponent.useTestModule(this, createTestModuleBuilder().withAppBundle(appBundle).withOutputPath(outputFilePath).build());
buildApksManager.execute();
ZipFile apkSetFile = openZipFile(outputFilePath.toFile());
BuildApksResult result = extractTocFromApkSetFile(apkSetFile, outputDir);
ImmutableList<Variant> splitApkVariants = splitApkVariants(result);
ImmutableList<ApkDescription> splitApks = apkDescriptions(splitApkVariants);
// The lowest density (mdpi) of "drawable/manifest_reachable_image" is in the master.
ImmutableList<ApkDescription> masterSplits = splitApks.stream().filter(apkDesc -> apkDesc.getSplitApkMetadata().getIsMasterSplit()).collect(toImmutableList());
assertThat(masterSplits).isNotEmpty();
for (ApkDescription masterSplit : masterSplits) {
assertThat(filesInApk(masterSplit, apkSetFile)).contains("res/drawable-mdpi/manifest_image.jpg");
}
ImmutableList<ApkDescription> densitySplits = splitApks.stream().filter(apkDesc -> apkDesc.getTargeting().hasScreenDensityTargeting()).collect(toImmutableList());
assertThat(densitySplits).isNotEmpty();
for (ApkDescription densitySplit : densitySplits) {
assertThat(filesInApk(densitySplit, apkSetFile)).doesNotContain("res/drawable-mdpi/manifest_image.jpg");
}
}
use of com.android.tools.build.bundletool.model.AppBundle in project bundletool by google.
the class BuildApksManagerTest method transparencyFilePropagatedAsExpected.
@Test
public void transparencyFilePropagatedAsExpected() throws Exception {
String dexFilePath = "dex/classes.dex";
byte[] dexFileInBaseModuleContent = TestData.readBytes("testdata/dex/classes.dex");
byte[] dexFileInFeatureModuleContent = TestData.readBytes("testdata/dex/classes-other.dex");
String libFilePath = "lib/x86_64/libsome.so";
byte[] libFileInBaseModuleContent = new byte[] { 4, 5, 6 };
CodeTransparency codeTransparency = CodeTransparency.newBuilder().addCodeRelatedFile(CodeRelatedFile.newBuilder().setType(CodeRelatedFile.Type.DEX).setPath("base/" + dexFilePath).setSha256(ByteSource.wrap(dexFileInBaseModuleContent).hash(Hashing.sha256()).toString())).addCodeRelatedFile(CodeRelatedFile.newBuilder().setType(CodeRelatedFile.Type.NATIVE_LIBRARY).setPath("base/" + libFilePath).setSha256(ByteSource.wrap(libFileInBaseModuleContent).hash(Hashing.sha256()).toString()).setApkPath(libFilePath)).addCodeRelatedFile(CodeRelatedFile.newBuilder().setType(CodeRelatedFile.Type.DEX).setPath("feature/" + dexFilePath).setSha256(ByteSource.wrap(dexFileInFeatureModuleContent).hash(Hashing.sha256()).toString())).build();
Path bundlePath = tmpDir.resolve("bundle.aab");
AppBundleBuilder appBundle = new AppBundleBuilder().addModule("base", module -> module.setManifest(androidManifest("com.test.app", withMinSdkVersion(20))).setResourceTable(resourceTableWithTestLabel("Test feature")).addFile(dexFilePath, bundlePath, ZipPath.create("base/" + dexFilePath), dexFileInBaseModuleContent).addFile(libFilePath, bundlePath, ZipPath.create("base/" + libFilePath), libFileInBaseModuleContent).setNativeConfig(nativeLibraries(targetedNativeDirectory("lib/x86_64", nativeDirectoryTargeting(AbiAlias.X86_64))))).addModule("feature", module -> module.setManifest(androidManifest("com.test.app", withDelivery(DeliveryType.ON_DEMAND), withFusingAttribute(true), withTitle("@string/test_label", TEST_LABEL_RESOURCE_ID))).addFile(dexFilePath, bundlePath, ZipPath.create("feature/" + dexFilePath), dexFileInFeatureModuleContent)).addMetadataFile(BundleMetadata.BUNDLETOOL_NAMESPACE, BundleMetadata.TRANSPARENCY_SIGNED_FILE_NAME, CharSource.wrap(createJwsToken(codeTransparency, certificate, privateKey)).asByteSource(Charset.defaultCharset()));
TestComponent.useTestModule(this, createTestModuleBuilder().withOutputPath(outputFilePath).withAppBundle(appBundle.build()).build());
buildApksManager.execute();
ZipFile apkSetFile = openZipFile(outputFilePath.toFile());
BuildApksResult result = extractTocFromApkSetFile(apkSetFile, outputDir);
ImmutableList<ApkDescription> splitApks = apkDescriptions(splitApkVariants(result));
// Transparency file should be propagated to main split of the base module.
ImmutableList<ApkDescription> mainSplitsOfBaseModule = splitApks.stream().filter(apk -> apk.getSplitApkMetadata().getSplitId().isEmpty() && apk.getSplitApkMetadata().getIsMasterSplit()).collect(toImmutableList());
assertThat(mainSplitsOfBaseModule).hasSize(2);
for (ApkDescription apk : mainSplitsOfBaseModule) {
ZipFile zipFile = openZipFile(extractFromApkSetFile(apkSetFile, apk.getPath(), outputDir));
assertThat(filesUnderPath(zipFile, ZipPath.create("META-INF"))).contains("META-INF/" + BundleMetadata.TRANSPARENCY_SIGNED_FILE_NAME);
}
// Other splits should not contain transparency file.
ImmutableList<ApkDescription> otherSplits = splitApks.stream().filter(apk -> !apk.getSplitApkMetadata().getSplitId().isEmpty()).collect(toImmutableList());
assertThat(otherSplits).hasSize(4);
for (ApkDescription apk : otherSplits) {
ZipFile zipFile = openZipFile(extractFromApkSetFile(apkSetFile, apk.getPath(), outputDir));
assertThat(filesUnderPath(zipFile, ZipPath.create("META-INF"))).isEmpty();
}
// Because minSdkVersion < 21, bundle has a feature module and merging strategy is
// MERGE_IF_NEEDED (default), transparency file should not be propagated to standalone APK.
assertThat(standaloneApkVariants(result)).hasSize(1);
ImmutableList<ApkDescription> standaloneApks = apkDescriptions(standaloneApkVariants(result).get(0));
File standaloneApkFile = extractFromApkSetFile(apkSetFile, standaloneApks.get(0).getPath(), outputDir);
ZipFile standaloneApkZip = openZipFile(standaloneApkFile);
assertThat(filesUnderPath(standaloneApkZip, ZipPath.create("META-INF"))).isEmpty();
}
use of com.android.tools.build.bundletool.model.AppBundle in project bundletool by google.
the class BuildApksManagerTest method selectsRightModules_universalMode_withModulesFlag_allModulesShortcut.
@Test
public void selectsRightModules_universalMode_withModulesFlag_allModulesShortcut() throws Exception {
AppBundle appBundle = createAppBundleWithBaseAndFeatureModules("ar", "vr");
TestComponent.useTestModule(this, createTestModuleBuilder().withAppBundle(appBundle).withOutputPath(outputFilePath).withApkBuildMode(UNIVERSAL).withModules(ALL_MODULES_SHORTCUT).build());
buildApksManager.execute();
ZipFile apkSetFile = openZipFile(outputFilePath.toFile());
BuildApksResult result = extractTocFromApkSetFile(apkSetFile, outputDir);
assertThat(apkDescriptions(standaloneApkVariants(result))).ignoringRepeatedFieldOrder().containsExactly(ApkDescription.newBuilder().setPath("universal.apk").setTargeting(ApkTargeting.getDefaultInstance()).setStandaloneApkMetadata(StandaloneApkMetadata.newBuilder().addAllFusedModuleName(ImmutableList.of("base", "vr", "ar"))).build());
}
use of com.android.tools.build.bundletool.model.AppBundle in project bundletool by google.
the class BuildApksManagerTest method buildApksCommand_appTargetingLPlus_buildsSplitApksOnly.
@Test
public void buildApksCommand_appTargetingLPlus_buildsSplitApksOnly() throws Exception {
AppBundle appBundle = new AppBundleBuilder().addModule("base", builder -> builder.setManifest(androidManifest("com.test.app", withMinSdkVersion(ANDROID_L_API_VERSION)))).build();
TestComponent.useTestModule(this, createTestModuleBuilder().withAppBundle(appBundle).withOutputPath(outputFilePath).build());
buildApksManager.execute();
ZipFile apkSetFile = openZipFile(outputFilePath.toFile());
BuildApksResult result = extractTocFromApkSetFile(apkSetFile, outputDir);
assertThat(result.getVariantList()).hasSize(1);
assertThat(standaloneApkVariants(result)).isEmpty();
assertThat(splitApkVariants(result)).hasSize(1);
Variant splitApksVariant = splitApkVariants(result).get(0);
assertThat(splitApksVariant.getTargeting()).isEqualTo(variantSdkTargeting(L_SDK_VERSION));
}
use of com.android.tools.build.bundletool.model.AppBundle in project bundletool by google.
the class BuildApksPreprocessingTest method localTestingMode_enabled_addsMetadata.
@Test
public void localTestingMode_enabled_addsMetadata() throws Exception {
AppBundle appBundle = createAppBundleWithBaseAndFeatureModules("feature");
new AppBundleSerializer().writeToDisk(appBundle, bundlePath);
BuildApksCommand command = BuildApksCommand.builder().setBundlePath(bundlePath).setOutputFile(outputFilePath).setLocalTestingMode(true).build();
command.execute();
try (ZipFile apkSet = new ZipFile(outputFilePath.toFile())) {
BuildApksResult result = extractTocFromApkSetFile(apkSet, outputDir);
assertThat(result.hasLocalTestingInfo()).isTrue();
assertThat(result.getLocalTestingInfo().getEnabled()).isTrue();
assertThat(result.getLocalTestingInfo().getLocalTestingPath()).isNotEmpty();
ImmutableList<ApkDescription> apkDescriptions = apkDescriptions(result.getVariantList());
assertThat(apkDescriptions).isNotEmpty();
assertThat(apkDescriptions.stream().map(ApkDescription::getPath)).contains("splits/base-master.apk");
for (ApkDescription apkDescription : apkDescriptions) {
File apk = extractFromApkSetFile(apkSet, apkDescription.getPath(), outputDir);
// The local testing metadata is set if and only if the apk is the base master.
assertThat((apkDescription.hasSplitApkMetadata() && apkDescription.getSplitApkMetadata().getSplitId().isEmpty() && apkDescription.getSplitApkMetadata().getIsMasterSplit()) || apkDescription.hasStandaloneApkMetadata()).isEqualTo(extractAndroidManifest(apk).getMetadataValue(LocalTestingPreprocessor.METADATA_NAME).isPresent());
}
}
}
Aggregations