Search in sources :

Example 21 with ZipBuilder

use of com.android.tools.build.bundletool.io.ZipBuilder in project bundletool by google.

the class MandatoryFilesPresenceValidatorTest method bundleZipFile_withoutAndroidManifestInModule_throws.

@Test
public void bundleZipFile_withoutAndroidManifestInModule_throws() throws Exception {
    Path bundlePath = new ZipBuilder().addFileWithContent(ZipPath.create("base/assets/file.txt"), DUMMY_CONTENT).addFileWithContent(ZipPath.create("BundleConfig.pb"), DUMMY_CONTENT).writeTo(tempFolder.resolve("bundle.aab"));
    try (ZipFile bundleZip = new ZipFile(bundlePath.toFile())) {
        InvalidBundleException exception = assertThrows(InvalidBundleException.class, () -> new MandatoryFilesPresenceValidator(AppBundle.NON_MODULE_DIRECTORIES).validateBundleZipFile(bundleZip));
        assertThat(exception).hasMessageThat().contains("Module 'base' is missing mandatory file 'manifest/AndroidManifest.xml'");
    }
}
Also used : ZipPath(com.android.tools.build.bundletool.model.ZipPath) Path(java.nio.file.Path) ZipFile(java.util.zip.ZipFile) ZipBuilder(com.android.tools.build.bundletool.io.ZipBuilder) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) Test(org.junit.Test)

Example 22 with ZipBuilder

use of com.android.tools.build.bundletool.io.ZipBuilder in project bundletool by google.

the class MandatoryFilesPresenceValidatorTest method moduleZipFile_withoutAndroidManifest_throws.

@Test
public void moduleZipFile_withoutAndroidManifest_throws() throws Exception {
    Path modulePath = new ZipBuilder().addFileWithContent(ZipPath.create("assets/file.txt"), DUMMY_CONTENT).writeTo(tempFolder.resolve("base.zip"));
    try (ZipFile moduleZip = new ZipFile(modulePath.toFile())) {
        InvalidBundleException exception = assertThrows(InvalidBundleException.class, () -> new MandatoryFilesPresenceValidator(AppBundle.NON_MODULE_DIRECTORIES).validateModuleZipFile(moduleZip));
        assertThat(exception).hasMessageThat().contains("Module 'base' is missing mandatory file 'manifest/AndroidManifest.xml'");
    }
}
Also used : ZipPath(com.android.tools.build.bundletool.model.ZipPath) Path(java.nio.file.Path) ZipFile(java.util.zip.ZipFile) ZipBuilder(com.android.tools.build.bundletool.io.ZipBuilder) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) Test(org.junit.Test)

Example 23 with ZipBuilder

use of com.android.tools.build.bundletool.io.ZipBuilder in project bundletool by google.

the class AppBundleTest method createBasicZipBuilder.

private static ZipBuilder createBasicZipBuilder(BundleConfig config) {
    ZipBuilder zipBuilder = new ZipBuilder();
    zipBuilder.addFileWithContent(ZipPath.create("BundleConfig.pb"), config.toByteArray());
    return zipBuilder;
}
Also used : ZipBuilder(com.android.tools.build.bundletool.io.ZipBuilder)

Example 24 with ZipBuilder

use of com.android.tools.build.bundletool.io.ZipBuilder in project bundletool by google.

the class ApkSizeUtilsTest method multipleVariants_withUncompressedEntries.

@Test
public void multipleVariants_withUncompressedEntries() throws Exception {
    ZipPath apkOne = ZipPath.create("apk_one.apk");
    ZipPath apkTwo = ZipPath.create("apk_two.apk");
    ImmutableList<Variant> variants = ImmutableList.of(createVariantForSingleSplitApk(variantSdkTargeting(sdkVersionFrom(21), ImmutableSet.of(sdkVersionFrom(23))), ApkTargeting.getDefaultInstance(), apkOne), createVariantForSingleSplitApk(variantSdkTargeting(sdkVersionFrom(23), ImmutableSet.of(sdkVersionFrom(21))), ApkTargeting.getDefaultInstance(), apkTwo));
    ZipBuilder archiveBuilder = new ZipBuilder();
    archiveBuilder.addFileWithContent(ZipPath.create(apkOne.toString()), DUMMY_BYTES);
    archiveBuilder.addFileWithContent(ZipPath.create(apkTwo.toString()), DUMMY_BYTES, // APK stored uncompressed in the APKs zip.
    EntryOption.UNCOMPRESSED);
    archiveBuilder.addFileWithProtoContent(ZipPath.create("toc.pb"), BuildApksResult.newBuilder().addAllVariant(variants).build());
    Path apksArchiveFile = archiveBuilder.writeTo(tmpDir.resolve("bundle.apks"));
    ImmutableMap<String, Long> sizeByApkPaths = getVariantCompressedSizeByApkPaths(variants, apksArchiveFile);
    assertThat(sizeByApkPaths.keySet()).containsExactly("apk_one.apk", "apk_two.apk");
    long apkOneSize = sizeByApkPaths.get("apk_one.apk");
    long apkTwoSize = sizeByApkPaths.get("apk_two.apk");
    // Apks should have size at least 20. Gzip Header(10 bytes), deflated data (Min 2 Bytes for
    // empty file), 8 bytes footer.
    assertThat(apkOneSize).isGreaterThan(20L);
    assertThat(apkTwoSize).isGreaterThan(20L);
    assertThat(apkOneSize).isEqualTo(apkTwoSize);
}
Also used : ApksArchiveHelpers.createVariant(com.android.tools.build.bundletool.testing.ApksArchiveHelpers.createVariant) Variant(com.android.bundle.Commands.Variant) ZipPath(com.android.tools.build.bundletool.model.ZipPath) Path(java.nio.file.Path) ZipBuilder(com.android.tools.build.bundletool.io.ZipBuilder) ZipPath(com.android.tools.build.bundletool.model.ZipPath) Test(org.junit.Test)

Example 25 with ZipBuilder

use of com.android.tools.build.bundletool.io.ZipBuilder in project bundletool by google.

the class BuildBundleCommandTest method assetsTargeting_generated.

@Test
public void assetsTargeting_generated() throws Exception {
    XmlNode manifest = androidManifest(PKG_NAME, withHasCode(true));
    Assets assetsConfig = Assets.newBuilder().addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets").setTargeting(AssetsDirectoryTargeting.getDefaultInstance())).addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/texture#tcf_atc/device#tier_0").setTargeting(mergeAssetsTargeting(assetsDirectoryTargeting(textureCompressionTargeting(TextureCompressionFormatAlias.ATC)), assetsDirectoryTargeting(deviceTierTargeting(0))))).build();
    Path module = new ZipBuilder().addFileWithContent(ZipPath.create("assets/anything.dat"), "any".getBytes(UTF_8)).addFileWithContent(ZipPath.create("assets/texture#tcf_atc/device#tier_0/file.dat"), "any2".getBytes(UTF_8)).addFileWithContent(ZipPath.create("dex/classes.dex"), "dex".getBytes(UTF_8)).addFileWithProtoContent(ZipPath.create("manifest/AndroidManifest.xml"), manifest).writeTo(tmpDir.resolve("base.zip"));
    BuildBundleCommand.builder().setOutputPath(bundlePath).setModulesPaths(ImmutableList.of(module)).build().execute();
    try (ZipFile bundle = new ZipFile(bundlePath.toFile())) {
        assertThat(bundle).hasFile("base/assets/anything.dat").withContent("any".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/assets/texture#tcf_atc/device#tier_0/file.dat").withContent("any2".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/dex/classes.dex").withContent("dex".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/manifest/AndroidManifest.xml").withContent(manifest.toByteArray());
        assertThat(bundle).hasFile("base/assets.pb").withContent(assetsConfig.toByteArray());
    }
}
Also used : Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) XmlNode(com.android.aapt.Resources.XmlNode) ZipFile(java.util.zip.ZipFile) Assets(com.android.bundle.Files.Assets) ZipBuilder(com.android.tools.build.bundletool.io.ZipBuilder) Test(org.junit.Test)

Aggregations

ZipBuilder (com.android.tools.build.bundletool.io.ZipBuilder)44 Test (org.junit.Test)39 ZipPath (com.android.tools.build.bundletool.model.ZipPath)37 Path (java.nio.file.Path)37 ZipFile (java.util.zip.ZipFile)19 InvalidBundleException (com.android.tools.build.bundletool.model.exceptions.InvalidBundleException)11 BuildApksResult (com.android.bundle.Commands.BuildApksResult)5 ModuleSplit (com.android.tools.build.bundletool.model.ModuleSplit)5 ByteString (com.google.protobuf.ByteString)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 PrintStream (java.io.PrintStream)5 ZipEntry (java.util.zip.ZipEntry)5 XmlNode (com.android.aapt.Resources.XmlNode)4 Variant (com.android.bundle.Commands.Variant)4 ApksArchiveHelpers.createVariant (com.android.tools.build.bundletool.testing.ApksArchiveHelpers.createVariant)4 FlagParseException (com.android.tools.build.bundletool.flags.FlagParser.FlagParseException)3 ConfigurationSizes (com.android.tools.build.bundletool.model.ConfigurationSizes)3 InvalidCommandException (com.android.tools.build.bundletool.model.exceptions.InvalidCommandException)3 ApksArchiveHelpers.standaloneVariant (com.android.tools.build.bundletool.testing.ApksArchiveHelpers.standaloneVariant)3 ResourceTableBuilder (com.android.tools.build.bundletool.testing.ResourceTableBuilder)3