Search in sources :

Example 26 with ZipBuilder

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

the class BuildBundleCommandTest method validApexModule.

@Test
public void validApexModule() throws Exception {
    XmlNode manifest = androidManifest(PKG_NAME, withHasCode(false));
    ImmutableSet<AbiAlias> targetedAbis = ImmutableSet.of(X86_64, X86, ARM64_V8A, ARMEABI_V7A);
    ApexImages apexConfig = apexImages(targetedImageWithAlternatives("apex/x86_64.img", X86_64, targetedAbis), targetedImageWithAlternatives("apex/x86.img", X86, targetedAbis), targetedImageWithAlternatives("apex/arm64-v8a.img", ARM64_V8A, targetedAbis), targetedImageWithAlternatives("apex/armeabi-v7a.img", ARMEABI_V7A, targetedAbis));
    byte[] apexManifest = "{\"name\": \"com.test.app\"}".getBytes(UTF_8);
    Path module = new ZipBuilder().addFileWithContent(ZipPath.create("apex/x86_64.img"), "x86_64".getBytes(UTF_8)).addFileWithContent(ZipPath.create("apex/x86.img"), "x86".getBytes(UTF_8)).addFileWithContent(ZipPath.create("apex/arm64-v8a.img"), "arm64-v8a".getBytes(UTF_8)).addFileWithContent(ZipPath.create("apex/armeabi-v7a.img"), "armeabi-v7a".getBytes(UTF_8)).addFileWithProtoContent(ZipPath.create("manifest/AndroidManifest.xml"), manifest).addFileWithContent(ZipPath.create("root/apex_manifest.json"), apexManifest).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/manifest/AndroidManifest.xml").withContent(manifest.toByteArray());
        assertThat(bundle).hasFile("base/apex/x86_64.img").withContent("x86_64".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/apex/x86.img").withContent("x86".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/apex/arm64-v8a.img").withContent("arm64-v8a".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/apex/armeabi-v7a.img").withContent("armeabi-v7a".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/root/apex_manifest.json").withContent(apexManifest);
        assertThat(bundle).hasFile("base/apex.pb").withContent(apexConfig.toByteArray());
    }
}
Also used : Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) XmlNode(com.android.aapt.Resources.XmlNode) ApexImages(com.android.bundle.Files.ApexImages) ZipFile(java.util.zip.ZipFile) ZipBuilder(com.android.tools.build.bundletool.io.ZipBuilder) AbiAlias(com.android.bundle.Targeting.Abi.AbiAlias) Test(org.junit.Test)

Example 27 with ZipBuilder

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

the class BuildBundleCommandTest method validApexModuleWithBuildInfo.

@Test
public void validApexModuleWithBuildInfo() throws Exception {
    XmlNode manifest = androidManifest(PKG_NAME, withHasCode(false));
    ImmutableSet<AbiAlias> targetedAbis = ImmutableSet.of(X86_64, X86, ARM64_V8A, ARMEABI_V7A);
    ApexImages apexConfig = apexImages(targetedImageWithBuildInfoAndAlternatives("apex/x86_64.img", "apex/x86_64.build_info.pb", X86_64, targetedAbis), targetedImageWithBuildInfoAndAlternatives("apex/x86.img", "apex/x86.build_info.pb", X86, targetedAbis), targetedImageWithBuildInfoAndAlternatives("apex/arm64-v8a.img", "apex/arm64-v8a.build_info.pb", ARM64_V8A, targetedAbis), targetedImageWithBuildInfoAndAlternatives("apex/armeabi-v7a.img", "apex/armeabi-v7a.build_info.pb", ARMEABI_V7A, targetedAbis));
    byte[] apexManifest = "{\"name\": \"com.test.app\"}".getBytes(UTF_8);
    Path module = new ZipBuilder().addFileWithContent(ZipPath.create("apex/x86_64.img"), "x86_64".getBytes(UTF_8)).addFileWithContent(ZipPath.create("apex/x86_64.build_info.pb"), "x86_64.build_info".getBytes(UTF_8)).addFileWithContent(ZipPath.create("apex/x86.img"), "x86".getBytes(UTF_8)).addFileWithContent(ZipPath.create("apex/x86.build_info.pb"), "x86.build_info".getBytes(UTF_8)).addFileWithContent(ZipPath.create("apex/arm64-v8a.img"), "arm64-v8a".getBytes(UTF_8)).addFileWithContent(ZipPath.create("apex/arm64-v8a.build_info.pb"), "arm64-v8a.build_info".getBytes(UTF_8)).addFileWithContent(ZipPath.create("apex/armeabi-v7a.img"), "armeabi-v7a".getBytes(UTF_8)).addFileWithContent(ZipPath.create("apex/armeabi-v7a.build_info.pb"), "armeabi-v7a.build_info".getBytes(UTF_8)).addFileWithProtoContent(ZipPath.create("manifest/AndroidManifest.xml"), manifest).addFileWithContent(ZipPath.create("root/apex_manifest.json"), apexManifest).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/manifest/AndroidManifest.xml").withContent(manifest.toByteArray());
        assertThat(bundle).hasFile("base/apex/x86_64.img").withContent("x86_64".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/apex/x86_64.build_info.pb").withContent("x86_64.build_info".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/apex/x86.img").withContent("x86".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/apex/x86.build_info.pb").withContent("x86.build_info".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/apex/arm64-v8a.img").withContent("arm64-v8a".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/apex/arm64-v8a.build_info.pb").withContent("arm64-v8a.build_info".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/apex/armeabi-v7a.img").withContent("armeabi-v7a".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/apex/armeabi-v7a.build_info.pb").withContent("armeabi-v7a.build_info".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/root/apex_manifest.json").withContent(apexManifest);
        assertThat(bundle).hasFile("base/apex.pb").withContent(apexConfig.toByteArray());
    }
}
Also used : Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) XmlNode(com.android.aapt.Resources.XmlNode) ApexImages(com.android.bundle.Files.ApexImages) ZipFile(java.util.zip.ZipFile) ZipBuilder(com.android.tools.build.bundletool.io.ZipBuilder) AbiAlias(com.android.bundle.Targeting.Abi.AbiAlias) Test(org.junit.Test)

Example 28 with ZipBuilder

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

the class BuildBundleCommandTest method buildUncompressedBundle.

@Test
public void buildUncompressedBundle() throws Exception {
    Path module = new ZipBuilder().addFileWithProtoContent(ZipPath.create("manifest/AndroidManifest.xml"), androidManifest(PKG_NAME)).addFileWithContent(ZipPath.create("dex/classes.dex"), new byte[8]).addFileWithContent(ZipPath.create("res/raw/hello.png"), new byte[8]).addFileWithProtoContent(ZipPath.create("resources.pb"), new ResourceTableBuilder().addPackage(PKG_NAME).addDrawableResource("hello", "res/raw/hello.png").build()).writeTo(tmpDir.resolve("base.zip"));
    Path mainDexList = Files.createFile(tmp.getRoot().toPath().resolve("mainDexList.txt"));
    BuildBundleCommand.builder().setModulesPaths(ImmutableList.of(module)).setOutputPath(bundlePath).setMainDexListFile(mainDexList).setUncompressedBundle(true).build().execute();
    try (ZipFile bundle = new ZipFile(bundlePath.toFile())) {
        Enumeration<? extends ZipEntry> entries = bundle.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            assertWithMessage("Entry %s is not uncompressed.", entry.getName()).that(entry.getMethod()).isEqualTo(ZipEntry.STORED);
        }
    }
}
Also used : Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) ZipBuilder(com.android.tools.build.bundletool.io.ZipBuilder) ResourceTableBuilder(com.android.tools.build.bundletool.testing.ResourceTableBuilder) Test(org.junit.Test)

Example 29 with ZipBuilder

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

the class BuildBundleCommandTest method validModule.

@Test
public void validModule() throws Exception {
    XmlNode manifest = androidManifest(PKG_NAME, withHasCode(true));
    ResourceTable resourceTable = new ResourceTableBuilder().addPackage(PKG_NAME).addDrawableResource("icon", "res/drawable/icon.png").build();
    Path module = new ZipBuilder().addFileWithContent(ZipPath.create("assets/anything.dat"), "any".getBytes(UTF_8)).addFileWithContent(ZipPath.create("dex/classes.dex"), "dex".getBytes(UTF_8)).addFileWithContent(ZipPath.create("lib/x86/libX.so"), "native".getBytes(UTF_8)).addFileWithProtoContent(ZipPath.create("manifest/AndroidManifest.xml"), manifest).addFileWithContent(ZipPath.create("res/drawable/icon.png"), "image".getBytes(UTF_8)).addFileWithContent(ZipPath.create("root/anything2.dat"), "any2".getBytes(UTF_8)).addFileWithProtoContent(ZipPath.create("resources.pb"), resourceTable).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/dex/classes.dex").withContent("dex".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/lib/x86/libX.so").withContent("native".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/manifest/AndroidManifest.xml").withContent(manifest.toByteArray());
        assertThat(bundle).hasFile("base/res/drawable/icon.png").withContent("image".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/root/anything2.dat").withContent("any2".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/resources.pb").withContent(resourceTable.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) ZipBuilder(com.android.tools.build.bundletool.io.ZipBuilder) ResourceTableBuilder(com.android.tools.build.bundletool.testing.ResourceTableBuilder) ResourceTable(com.android.aapt.Resources.ResourceTable) Test(org.junit.Test)

Example 30 with ZipBuilder

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

the class BuildBundleCommandTest method moduleWithWrongExtension_throws.

@Test
public void moduleWithWrongExtension_throws() throws Exception {
    Path nonZipModule = new ZipBuilder().writeTo(tmpDir.resolve("not_a_zip.txt"));
    IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> BuildBundleCommand.builder().setOutputPath(bundlePath).setModulesPaths(ImmutableList.of(nonZipModule)).build().execute());
    assertThat(exception).hasMessageThat().contains("expected to have '.zip' extension");
}
Also used : Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) 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