Search in sources :

Example 41 with ZipBuilder

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

the class SdkBundleHasOneModuleValidatorTest method sdkBundleZipFile_multipleModules_throws.

@Test
public void sdkBundleZipFile_multipleModules_throws() throws Exception {
    Path bundlePath = new ZipBuilder().addFileWithContent(ZipPath.create("BundleConfig.pb"), DUMMY_CONTENT).addFileWithContent(ZipPath.create("base/manifest/AndroidManifest.xml"), DUMMY_CONTENT).addFileWithContent(ZipPath.create("feature/manifest/AndroidManifest.xml"), DUMMY_CONTENT).writeTo(tempFolder.resolve("bundle.asb"));
    try (ZipFile bundleZip = new ZipFile(bundlePath.toFile())) {
        InvalidBundleException exception = assertThrows(InvalidBundleException.class, () -> new SdkBundleHasOneModuleValidator().validateBundleZipFile(bundleZip));
        assertThat(exception).hasMessageThat().contains("SDK bundles need exactly one module");
    }
}
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 42 with ZipBuilder

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

the class ValidatorRunnerTest method validateBundle_invokesRightSubValidatorMethods.

@Test
public void validateBundle_invokesRightSubValidatorMethods() throws Exception {
    Path bundlePath = new ZipBuilder().addFileWithContent(ZipPath.create("BundleConfig.pb"), BUNDLE_CONFIG.toByteArray()).addFileWithProtoContent(ZipPath.create("moduleX/manifest/AndroidManifest.xml"), androidManifest("com.test.app", withSplitId("moduleX"))).addFileWithProtoContent(ZipPath.create("moduleX/assets.pb"), Assets.getDefaultInstance()).addFileWithProtoContent(ZipPath.create("moduleX/native.pb"), NativeLibraries.getDefaultInstance()).addFileWithProtoContent(ZipPath.create("moduleX/resources.pb"), ResourceTable.getDefaultInstance()).addFileWithContent(ZipPath.create("moduleX/res/drawable/icon.png"), DUMMY_CONTENT).addFileWithContent(ZipPath.create("moduleX/lib/x86/libX.so"), DUMMY_CONTENT).addFileWithProtoContent(ZipPath.create("moduleY/manifest/AndroidManifest.xml"), androidManifest("com.test.app", withSplitId("moduleY"))).addFileWithContent(ZipPath.create("moduleY/assets/file.txt"), DUMMY_CONTENT).writeTo(tempFolder.resolve("bundle.aab"));
    try (ZipFile bundleZip = new ZipFile(bundlePath.toFile())) {
        AppBundle bundle = AppBundle.buildFromZip(bundleZip);
        ImmutableList<BundleModule> bundleFeatureModules = ImmutableList.copyOf(bundle.getFeatureModules().values());
        new ValidatorRunner(ImmutableList.of(validator)).validateBundle(bundle);
        ArgumentCaptor<BundleModule> moduleArgs = ArgumentCaptor.forClass(BundleModule.class);
        ArgumentCaptor<ZipPath> fileArgs = ArgumentCaptor.forClass(ZipPath.class);
        verify(validator).validateBundle(eq(bundle));
        verify(validator).validateAllModules(eq(bundleFeatureModules));
        verify(validator, times(2)).validateModule(moduleArgs.capture());
        verify(validator, atLeastOnce()).validateModuleFile(fileArgs.capture());
        verifyNoMoreInteractions(validator);
        assertThat(moduleArgs.getAllValues()).containsExactlyElementsIn(bundle.getFeatureModules().values());
        assertThat(fileArgs.getAllValues().stream().map(ZipPath::toString)).containsExactly("assets/file.txt", "lib/x86/libX.so", "res/drawable/icon.png");
    }
}
Also used : ZipPath(com.android.tools.build.bundletool.model.ZipPath) Path(java.nio.file.Path) AppBundle(com.android.tools.build.bundletool.model.AppBundle) ZipFile(java.util.zip.ZipFile) ZipBuilder(com.android.tools.build.bundletool.io.ZipBuilder) ZipPath(com.android.tools.build.bundletool.model.ZipPath) BundleModule(com.android.tools.build.bundletool.model.BundleModule) Test(org.junit.Test)

Example 43 with ZipBuilder

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

the class ZipUtilsTest method createZipFileWithFiles.

private ZipFile createZipFileWithFiles(String... fileNames) throws IOException {
    ZipBuilder zipBuilder = new ZipBuilder();
    for (String fileName : fileNames) {
        zipBuilder.addFileWithContent(ZipPath.create(fileName), fileName.getBytes(UTF_8));
    }
    Path zipPath = zipBuilder.writeTo(tmp.getRoot().toPath().resolve("output.jar"));
    return new ZipFile(zipPath.toFile());
}
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)

Example 44 with ZipBuilder

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

the class BuildApksCommandTest method sdkBundleZipMissingManifest_sdkBundleZipFileValidationFails.

@Test
public void sdkBundleZipMissingManifest_sdkBundleZipFileValidationFails() throws Exception {
    new ZipBuilder().addFileWithContent(ZipPath.create("base/dex/classes.dex"), new byte[1]).addFileWithContent(ZipPath.create("BundleConfig.pb"), new byte[1]).writeTo(sdkBundlePath1);
    createAppBundle(bundlePath);
    BuildApksCommand command = BuildApksCommand.fromFlags(new FlagParser().parse("--bundle=" + bundlePath, "--output=" + outputFilePath, "--sdk-bundles=" + sdkBundlePath1), fakeAdbServer);
    Exception e = assertThrows(InvalidBundleException.class, command::execute);
    assertThat(e).hasMessageThat().contains("Module 'base' is missing mandatory file 'manifest/AndroidManifest.xml'.");
}
Also used : ZipBuilder(com.android.tools.build.bundletool.io.ZipBuilder) FlagParser(com.android.tools.build.bundletool.flags.FlagParser) FlagParseException(com.android.tools.build.bundletool.flags.FlagParser.FlagParseException) IOException(java.io.IOException) CommandExecutionException(com.android.tools.build.bundletool.model.exceptions.CommandExecutionException) JoseException(org.jose4j.lang.JoseException) TestUtils.expectMissingRequiredBuilderPropertyException(com.android.tools.build.bundletool.testing.TestUtils.expectMissingRequiredBuilderPropertyException) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) TestUtils.expectMissingRequiredFlagException(com.android.tools.build.bundletool.testing.TestUtils.expectMissingRequiredFlagException) InvalidCommandException(com.android.tools.build.bundletool.model.exceptions.InvalidCommandException) 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