com.android.tools.build.bundletool.io.ZipBuilder.<init>()方法的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(12.0k)|赞(0)|评价(0)|浏览(149)

本文整理了Java中com.android.tools.build.bundletool.io.ZipBuilder.<init>()方法的一些代码示例,展示了ZipBuilder.<init>()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZipBuilder.<init>()方法的具体详情如下:
包路径:com.android.tools.build.bundletool.io.ZipBuilder
类名称:ZipBuilder
方法名:<init>

ZipBuilder.<init>介绍

暂无

代码示例

代码示例来源:origin: google/bundletool

public ApkSetArchiveBuilder(
  SplitApkSerializer splitApkSerializer,
  StandaloneApkSerializer standaloneApkSerializer,
  Path tempDirectory) {
 this.splitApkSerializer = splitApkSerializer;
 this.standaloneApkSerializer = standaloneApkSerializer;
 this.tempDirectory = tempDirectory;
 this.apkSetZipBuilder = new ZipBuilder();
}

代码示例来源:origin: google/bundletool

private ZipFile createZipFileWithFiles(String... fileNames) throws IOException {
  ZipBuilder zipBuilder = new ZipBuilder();
  for (String fileName : fileNames) {
   zipBuilder.addFileWithContent(ZipPath.create(fileName), new byte[1]);
  }
  Path zipPath = zipBuilder.writeTo(tmp.getRoot().toPath().resolve("output.jar"));
  return new ZipFile(zipPath.toFile());
 }
}

代码示例来源:origin: google/bundletool

@Test
 public void validateBundleZipEntry_file_ok() throws Exception {
  Path bundlePath =
    new ZipBuilder()
      .addFileWithContent(ZipPath.create("file.txt"), DUMMY_CONTENT)
      .writeTo(tempFolder.resolve("bundle.aab"));

  try (ZipFile bundleZip = new ZipFile(bundlePath.toFile())) {
   ArrayList<? extends ZipEntry> entries = Collections.list(bundleZip.entries());
   // Sanity check.
   assertThat(entries).hasSize(1);

   new BundleZipValidator().validateBundleZipEntry(bundleZip, entries.get(0));
  }
 }
}

代码示例来源:origin: google/bundletool

public static Path createApksArchiveFile(BuildApksResult result, Path location) throws Exception {
 ZipBuilder archiveBuilder = new ZipBuilder();
 result.getVariantList().stream()
   .flatMap(variant -> variant.getApkSetList().stream())
   .flatMap(apkSet -> apkSet.getApkDescriptionList().stream())
   .forEach(
     apkDesc ->
       archiveBuilder.addFileWithContent(ZipPath.create(apkDesc.getPath()), DUMMY_BYTES));
 archiveBuilder.addFileWithProtoContent(ZipPath.create("toc.pb"), result);
 return archiveBuilder.writeTo(location);
}

代码示例来源:origin: google/bundletool

private Path createSimpleBaseModule() throws IOException {
 return new ZipBuilder()
   .addFileWithProtoContent(
     ZipPath.create("manifest/AndroidManifest.xml"), androidManifest(PKG_NAME))
   .writeTo(tmpDir.resolve("base.zip"));
}

代码示例来源:origin: google/bundletool

@Test
public void moduleZipFile_withAllMandatoryFiles_ok() throws Exception {
 Path modulePath =
   new ZipBuilder()
     .addFileWithContent(ZipPath.create("manifest/AndroidManifest.xml"), DUMMY_CONTENT)
     .writeTo(tempFolder.resolve("base.zip"));
 try (ZipFile moduleZip = new ZipFile(modulePath.toFile())) {
  new MandatoryFilesPresenceValidator().validateModuleZipFile(moduleZip);
 }
}

代码示例来源:origin: google/bundletool

private static ZipBuilder createBasicZipBuilder(BundleConfig config) {
 ZipBuilder zipBuilder = new ZipBuilder();
 zipBuilder.addFileWithContent(ZipPath.create("BundleConfig.pb"), config.toByteArray());
 return zipBuilder;
}

代码示例来源:origin: google/bundletool

@Test
public void validateBundleZipFile_invokesRightSubValidatorMethods() throws Exception {
 Path bundlePath =
   new ZipBuilder()
     .addDirectory(ZipPath.create("directory"))
     .addFileWithContent(ZipPath.create("file.txt"), DUMMY_CONTENT)
     .writeTo(tempFolder.resolve("bundle.aab"));
 try (ZipFile bundleZip = new ZipFile(bundlePath.toFile())) {
  new ValidatorRunner(ImmutableList.of(validator)).validateBundleZipFile(bundleZip);
  ArgumentCaptor<ZipEntry> zipEntryArgs = ArgumentCaptor.forClass(ZipEntry.class);
  verify(validator).validateBundleZipFile(eq(bundleZip));
  verify(validator, atLeastOnce())
    .validateBundleZipEntry(eq(bundleZip), zipEntryArgs.capture());
  verifyNoMoreInteractions(validator);
  assertThat(zipEntryArgs.getAllValues().stream().map(ZipEntry::getName))
    .containsExactly("directory/", "file.txt");
 }
}

代码示例来源:origin: google/bundletool

@Test
 public void validateModuleZipFile_invokesRightSubValidatorMethods() throws Exception {
  Path modulePath =
    new ZipBuilder()
      .addDirectory(ZipPath.create("module"))
      .addFileWithContent(ZipPath.create("module/file.txt"), DUMMY_CONTENT)
      .writeTo(tempFolder.resolve("module.zip"));

  try (ZipFile moduleZip = new ZipFile(modulePath.toFile())) {
   new ValidatorRunner(ImmutableList.of(validator)).validateModuleZipFile(moduleZip);

   verify(validator).validateModuleZipFile(eq(moduleZip));
   verifyNoMoreInteractions(validator);
  }
 }
}

代码示例来源:origin: google/bundletool

@Test
public void validateBundleZipEntry_directory_throws() throws Exception {
 Path bundlePath =
   new ZipBuilder()
     .addDirectory(ZipPath.create("directory"))
     .writeTo(tempFolder.resolve("bundle.aab"));
 try (ZipFile bundleZip = new ZipFile(bundlePath.toFile())) {
  ArrayList<? extends ZipEntry> entries = Collections.list(bundleZip.entries());
  // Sanity check.
  assertThat(entries).hasSize(1);
  ValidationException exception =
    assertThrows(
      ValidationException.class,
      () -> new BundleZipValidator().validateBundleZipEntry(bundleZip, entries.get(0)));
  assertThat(exception).hasMessageThat().contains("zip file contains directory zip entry");
 }
}

代码示例来源:origin: google/bundletool

@Test
 public void bundleZipFile_withAllMandatoryFiles_ok() throws Exception {
  Path bundlePath =
    new ZipBuilder()
      .addFileWithContent(ZipPath.create("base/manifest/AndroidManifest.xml"), DUMMY_CONTENT)
      .addFileWithContent(ZipPath.create("BundleConfig.pb"), DUMMY_CONTENT)
      .writeTo(tempFolder.resolve("bundle.aab"));

  try (ZipFile bundleZip = new ZipFile(bundlePath.toFile())) {
   new MandatoryFilesPresenceValidator().validateBundleZipFile(bundleZip);
  }
 }
}

代码示例来源:origin: google/bundletool

@Test
public void bundleZipFile_withoutBundleConfig_throws() throws Exception {
 Path bundlePath =
   new ZipBuilder()
     .addFileWithContent(ZipPath.create("base/manifest/AndroidManifest.xml"), DUMMY_CONTENT)
     .writeTo(tempFolder.resolve("bundle.aab"));
 try (ZipFile bundleZip = new ZipFile(bundlePath.toFile())) {
  MandatoryBundleFileMissingException exception =
    assertThrows(
      MandatoryBundleFileMissingException.class,
      () -> new MandatoryFilesPresenceValidator().validateBundleZipFile(bundleZip));
  assertThat(exception).hasMessageThat().contains("missing required file 'BundleConfig.pb'");
 }
}

代码示例来源:origin: google/bundletool

private Path buildSimpleModule(String moduleName) throws IOException {
 ManifestMutator[] manifestMutators =
   moduleName.equals("base")
     ? new ManifestMutator[0]
     : new ManifestMutator[] {withSplitId(moduleName)};
 return new ZipBuilder()
   .addFileWithProtoContent(
     ZipPath.create("manifest/AndroidManifest.xml"),
     androidManifest(PKG_NAME, manifestMutators))
   .writeTo(FileUtils.getRandomFilePath(tmp, moduleName, ".zip"));
}

代码示例来源:origin: google/bundletool

@Test
public void directoryZipEntriesInModuleFiles_notIncludedInBundle() throws Exception {
 Path tmpBaseModulePath = Files.move(buildSimpleModule("base"), tmpDir.resolve("base.zip.tmp"));
 Path baseModulePath;
 // Copy the valid bundle, only add a directory zip entry.
 try (ZipFile tmpBaseModuleZip = new ZipFile(tmpBaseModulePath.toFile())) {
  baseModulePath =
    new ZipBuilder()
      .copyAllContentsFromZip(ZipPath.create(""), tmpBaseModuleZip)
      .addDirectory(ZipPath.create("directory-entry"))
      .writeTo(tmpDir.resolve("base.zip"));
 }
 BuildBundleCommand.builder()
   .setOutputPath(bundlePath)
   .setModulesPaths(ImmutableList.of(baseModulePath))
   .build()
   .execute();
 try (ZipFile bundleZip = new ZipFile(bundlePath.toFile())) {
  assertThat(Collections.list(bundleZip.entries()).stream().filter(ZipEntry::isDirectory))
    .isEmpty();
 }
}

代码示例来源:origin: google/bundletool

@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())) {
  MandatoryModuleFileMissingException exception =
    assertThrows(
      MandatoryModuleFileMissingException.class,
      () -> new MandatoryFilesPresenceValidator().validateModuleZipFile(moduleZip));
  assertThat(exception)
    .hasMessageThat()
    .contains("Module 'base' is missing mandatory file 'manifest/AndroidManifest.xml'");
 }
}

代码示例来源:origin: google/bundletool

@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");
}

代码示例来源:origin: google/bundletool

@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())) {
  MandatoryModuleFileMissingException exception =
    assertThrows(
      MandatoryModuleFileMissingException.class,
      () -> new MandatoryFilesPresenceValidator().validateBundleZipFile(bundleZip));
  assertThat(exception)
    .hasMessageThat()
    .contains("Module 'base' is missing mandatory file 'manifest/AndroidManifest.xml'");
 }
}

代码示例来源:origin: google/bundletool

@Test
public void runsBundleFilesValidator_rogueFileInModuleRoot_throws() throws Exception {
 Path module =
   new ZipBuilder()
     .addFileWithContent(ZipPath.create("rogue.txt"), "".getBytes(UTF_8))
     .addFileWithProtoContent(
       ZipPath.create("manifest/AndroidManifest.xml"), androidManifest(PKG_NAME))
     .writeTo(tmpDir.resolve("base.zip"));
 BuildBundleCommand command =
   BuildBundleCommand.builder()
     .setOutputPath(bundlePath)
     .setModulesPaths(ImmutableList.of(module))
     .build();
 ValidationException exception =
   assertThrows(ValidationException.class, () -> command.execute());
 assertThat(exception)
   .hasMessageThat()
   .contains("Module files can be only in pre-defined directories");
}

代码示例来源:origin: google/bundletool

@Test
public void runsResourceTableValidator_resourceTableReferencingNonExistingFile_throws()
  throws Exception {
 ResourceTable resourceTable =
   new ResourceTableBuilder()
     .addPackage(PKG_NAME)
     .addDrawableResource("icon", "res/drawable/icon.png")
     .build();
 Path module =
   new ZipBuilder()
     .addFileWithProtoContent(ZipPath.create("resources.pb"), resourceTable)
     .addFileWithProtoContent(
       ZipPath.create("manifest/AndroidManifest.xml"), androidManifest(PKG_NAME))
     .writeTo(tmpDir.resolve("base.zip"));
 BuildBundleCommand command =
   BuildBundleCommand.builder()
     .setOutputPath(bundlePath)
     .setModulesPaths(ImmutableList.of(module))
     .build();
 ValidationException exception =
   assertThrows(ValidationException.class, () -> command.execute());
 assertThat(exception).hasMessageThat().contains("contains references to non-existing files");
}

代码示例来源:origin: google/bundletool

@Test
public void bundleWithDirectoryZipEntries_throws() throws Exception {
 Path tmpBundlePath = FileUtils.getRandomFilePath(tmp, "bundle-", ".aab");
 AppBundle tmpBundle =
   new AppBundleBuilder()
     .addModule("base", module -> module.setManifest(androidManifest("com.app")))
     .build();
 bundleSerializer.writeToDisk(tmpBundle, tmpBundlePath);
 // Copy the valid bundle, only add a directory zip entry.
 try (ZipFile tmpBundleZip = new ZipFile(tmpBundlePath.toFile())) {
  bundlePath =
    new ZipBuilder()
      .copyAllContentsFromZip(ZipPath.create(""), tmpBundleZip)
      .addDirectory(ZipPath.create("directory-entries-are-forbidden"))
      .writeTo(FileUtils.getRandomFilePath(tmp, "bundle-", ".aab"));
 }
 BuildApksCommand command =
   BuildApksCommand.builder()
     .setBundlePath(bundlePath)
     .setOutputFile(outputFilePath)
     .setAapt2Command(aapt2Command)
     .build();
 ValidationException exception = assertThrows(ValidationException.class, () -> execute(command));
 assertThat(exception)
   .hasMessageThat()
   .contains("zip file contains directory zip entry 'directory-entries-are-forbidden/'");
}

相关文章