use of com.android.tools.build.bundletool.io.AppBundleSerializer in project bundletool by google.
the class DumpManagerTest method dumpManifest_withXPath_singleValue.
@Test
public void dumpManifest_withXPath_singleValue() throws Exception {
AppBundle appBundle = new AppBundleBuilder().addModule("base", module -> module.setManifest(androidManifest("com.app"))).build();
new AppBundleSerializer().writeToDisk(appBundle, bundlePath);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DumpCommand.builder().setBundlePath(bundlePath).setDumpTarget(DumpTarget.MANIFEST).setXPathExpression("/manifest/@package").setOutputStream(new PrintStream(outputStream)).build().execute();
assertThat(new String(outputStream.toByteArray(), UTF_8)).isEqualTo(String.format("com.app%n"));
}
use of com.android.tools.build.bundletool.io.AppBundleSerializer in project bundletool by google.
the class BuildBundleCommand method execute.
public void execute() {
validateInput();
try (Closer closer = Closer.create()) {
ImmutableList.Builder<ZipFile> moduleZipFilesBuilder = ImmutableList.builder();
for (Path modulePath : getModulesPaths()) {
try {
moduleZipFilesBuilder.add(closer.register(new ZipFile(modulePath.toFile())));
} catch (ZipException e) {
throw CommandExecutionException.builder().withCause(e).withInternalMessage("File '%s' does not seem to be a valid ZIP file.", modulePath).build();
} catch (IOException e) {
throw CommandExecutionException.builder().withCause(e).withInternalMessage("Unable to read file '%s'.", modulePath).build();
}
}
ImmutableList<ZipFile> moduleZipFiles = moduleZipFilesBuilder.build();
// Read the Bundle Config file if provided by the developer.
BundleConfig bundleConfig = getBundleConfig().orElse(BundleConfig.getDefaultInstance()).toBuilder().setBundletool(Bundletool.newBuilder().setVersion(BundleToolVersion.getCurrentVersion().toString())).build();
ImmutableList<BundleModule> modules = new BundleModulesValidator().validate(moduleZipFiles, bundleConfig);
checkState(moduleZipFiles.size() == modules.size(), "Incorrect number of modules parsed (%s != %s).", moduleZipFiles.size(), modules.size());
ImmutableList.Builder<BundleModule> modulesWithTargeting = ImmutableList.builder();
for (BundleModule module : modules) {
BundleModule.Builder moduleWithTargeting = module.toBuilder();
Optional<Assets> assetsTargeting = generateAssetsTargeting(module);
assetsTargeting.ifPresent(moduleWithTargeting::setAssetsConfig);
Optional<NativeLibraries> nativeLibrariesTargeting = generateNativeLibrariesTargeting(module);
nativeLibrariesTargeting.ifPresent(moduleWithTargeting::setNativeConfig);
Optional<ApexImages> apexImagesTargeting = generateApexImagesTargeting(module);
apexImagesTargeting.ifPresent(moduleWithTargeting::setApexConfig);
modulesWithTargeting.add(moduleWithTargeting.build());
}
AppBundle appBundle = AppBundle.buildFromModules(modulesWithTargeting.build(), bundleConfig, getBundleMetadata());
Path outputDirectory = getOutputPath().toAbsolutePath().getParent();
if (Files.notExists(outputDirectory)) {
logger.info("Output directory '" + outputDirectory + "' does not exist, creating it.");
FileUtils.createDirectories(outputDirectory);
}
if (getOverwriteOutput()) {
Files.deleteIfExists(getOutputPath());
}
new AppBundleSerializer(getUncompressedBundle()).writeToDisk(appBundle, getOutputPath());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
use of com.android.tools.build.bundletool.io.AppBundleSerializer in project bundletool by google.
the class AddTransparencyCommand method executeDefaultMode.
private void executeDefaultMode(AppBundle inputBundle) throws IOException, JoseException {
validateDefaultModeInputs();
String jsonText = toJsonText(CodeTransparencyFactory.createCodeTransparencyMetadata(inputBundle));
AppBundle.Builder bundleBuilder = inputBundle.toBuilder();
bundleBuilder.setBundleMetadata(inputBundle.getBundleMetadata().toBuilder().addFile(BundleMetadata.BUNDLETOOL_NAMESPACE, BundleMetadata.TRANSPARENCY_SIGNED_FILE_NAME, toBytes(createSignedJwt(jsonText, getSignerConfig().get().getCertificates().get(0)))).build());
new AppBundleSerializer().writeToDisk(bundleBuilder.build(), getOutputPath());
}
use of com.android.tools.build.bundletool.io.AppBundleSerializer in project bundletool by google.
the class AddTransparencyCommand method executeInjectSignatureMode.
private void executeInjectSignatureMode(AppBundle inputBundle) throws IOException {
validateInjectSignatureModeInputs();
String signature = BaseEncoding.base64Url().encode(Files.readAllBytes(getTransparencySignaturePath().get()));
String codeTransparencyMetadata = toJsonText(CodeTransparencyFactory.createCodeTransparencyMetadata(inputBundle));
String transparencyFileWithoutSignature = createJwtWithoutSignature(codeTransparencyMetadata, getTransparencyKeyCertificate().get());
AppBundle bundleWithTransparency = inputBundle.toBuilder().setBundleMetadata(inputBundle.getBundleMetadata().toBuilder().addFile(BundleMetadata.BUNDLETOOL_NAMESPACE, BundleMetadata.TRANSPARENCY_SIGNED_FILE_NAME, toBytes(transparencyFileWithoutSignature + "." + signature)).build()).build();
if (!BundleTransparencyCheckUtils.checkTransparency(bundleWithTransparency).verified()) {
throw CommandExecutionException.builder().withInternalMessage("Code transparency verification failed for the provided public key certificate and" + " signature.").build();
}
new AppBundleSerializer().writeToDisk(bundleWithTransparency, getOutputPath());
}
use of com.android.tools.build.bundletool.io.AppBundleSerializer in project bundletool by google.
the class BuildApksPreprocessingTest method renderscript32Bit_64BitLibsOnly_throws.
@Test
public void renderscript32Bit_64BitLibsOnly_throws() throws Exception {
AppBundle appBundle = new AppBundleBuilder().addModule("base", builder -> builder.addFile("dex/classes.dex").addFile("assets/script.bc").addFile("lib/arm64-v8a/libfoo.so").setManifest(androidManifest("com.test.app", withMinSdkVersion(14))).setNativeConfig(nativeLibraries(targetedNativeDirectory("lib/arm64-v8a", nativeDirectoryTargeting(ARM64_V8A))))).build();
new AppBundleSerializer().writeToDisk(appBundle, bundlePath);
BuildApksCommand command = BuildApksCommand.builder().setBundlePath(bundlePath).setOutputFile(outputFilePath).build();
InvalidBundleException exception = assertThrows(InvalidBundleException.class, command::execute);
assertThat(exception).hasMessageThat().contains("Usage of 64-bit native libraries is disabled, but App Bundle contains only 64-bit" + " native libraries.");
}
Aggregations