Search in sources :

Example 26 with AppBundleSerializer

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"));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ManifestProtoUtils.withMetadataValue(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withMetadataValue) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ManifestProtoUtils.androidManifest(com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifest) RunWith(org.junit.runner.RunWith) XmlAttribute(com.android.aapt.Resources.XmlAttribute) DumpTarget(com.android.tools.build.bundletool.commands.DumpCommand.DumpTarget) Bundletool(com.android.bundle.Config.Bundletool) ResourceTableBuilder(com.android.tools.build.bundletool.testing.ResourceTableBuilder) AppBundleSerializer(com.android.tools.build.bundletool.io.AppBundleSerializer) Path(java.nio.file.Path) ResourceTable(com.android.aapt.Resources.ResourceTable) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Before(org.junit.Before) PrintStream(java.io.PrintStream) UTF_8(java.nio.charset.StandardCharsets.UTF_8) IOException(java.io.IOException) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) AppBundleBuilder(com.android.tools.build.bundletool.testing.AppBundleBuilder) Rule(org.junit.Rule) XmlNode(com.android.aapt.Resources.XmlNode) BundleConfig(com.android.bundle.Config.BundleConfig) ManifestProtoUtils.withDebuggableAttribute(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withDebuggableAttribute) XmlElement(com.android.aapt.Resources.XmlElement) Compression(com.android.bundle.Config.Compression) AppBundle(com.android.tools.build.bundletool.model.AppBundle) TemporaryFolder(org.junit.rules.TemporaryFolder) PrintStream(java.io.PrintStream) AppBundle(com.android.tools.build.bundletool.model.AppBundle) AppBundleBuilder(com.android.tools.build.bundletool.testing.AppBundleBuilder) AppBundleSerializer(com.android.tools.build.bundletool.io.AppBundleSerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 27 with AppBundleSerializer

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);
    }
}
Also used : Closer(com.google.common.io.Closer) ZipPath(com.android.tools.build.bundletool.model.ZipPath) Path(java.nio.file.Path) ApexImages(com.android.bundle.Files.ApexImages) AppBundle(com.android.tools.build.bundletool.model.AppBundle) BundleModulesValidator(com.android.tools.build.bundletool.validation.BundleModulesValidator) NativeLibraries(com.android.bundle.Files.NativeLibraries) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ZipException(java.util.zip.ZipException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) AppBundleSerializer(com.android.tools.build.bundletool.io.AppBundleSerializer) BundleModule(com.android.tools.build.bundletool.model.BundleModule) BundleConfig(com.android.bundle.Config.BundleConfig) ZipFile(java.util.zip.ZipFile) Assets(com.android.bundle.Files.Assets)

Example 28 with AppBundleSerializer

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());
}
Also used : AppBundle(com.android.tools.build.bundletool.model.AppBundle) AppBundleSerializer(com.android.tools.build.bundletool.io.AppBundleSerializer)

Example 29 with AppBundleSerializer

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());
}
Also used : AppBundle(com.android.tools.build.bundletool.model.AppBundle) AppBundleSerializer(com.android.tools.build.bundletool.io.AppBundleSerializer)

Example 30 with AppBundleSerializer

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.");
}
Also used : TEST_LABEL_RESOURCE_ID(com.android.tools.build.bundletool.testing.ResourcesTableFactory.TEST_LABEL_RESOURCE_ID) ApkTargeting(com.android.bundle.Targeting.ApkTargeting) ManifestProtoUtils.androidManifest(com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifest) Variant(com.android.bundle.Commands.Variant) AndroidManifest(com.android.tools.build.bundletool.model.AndroidManifest) ManifestProtoUtils.androidManifestForAssetModule(com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifestForAssetModule) TargetingUtils.abiTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.abiTargeting) LocalTestingPreprocessor(com.android.tools.build.bundletool.preprocessors.LocalTestingPreprocessor) ARM64_V8A(com.android.bundle.Targeting.Abi.AbiAlias.ARM64_V8A) ResourcesTableFactory.resourceTableWithTestLabel(com.android.tools.build.bundletool.testing.ResourcesTableFactory.resourceTableWithTestLabel) ZipFile(java.util.zip.ZipFile) Path(java.nio.file.Path) ManifestProtoUtils.withTitle(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withTitle) ImmutableSet(com.google.common.collect.ImmutableSet) TargetingUtils.nativeDirectoryTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.nativeDirectoryTargeting) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ARMEABI_V7A(com.android.bundle.Targeting.Abi.AbiAlias.ARMEABI_V7A) ManifestProtoUtils.withMinSdkVersion(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withMinSdkVersion) UncheckedIOException(java.io.UncheckedIOException) ApkSet(com.android.bundle.Commands.ApkSet) List(java.util.List) AppBundleBuilder(com.android.tools.build.bundletool.testing.AppBundleBuilder) ManifestProtoUtils.withInstallTimeDelivery(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withInstallTimeDelivery) ManifestProtoUtils.androidManifestForFeature(com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifestForFeature) ManifestProtoUtils.withMaxSdkVersion(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withMaxSdkVersion) TruthZip.assertThat(com.android.tools.build.bundletool.testing.truth.zip.TruthZip.assertThat) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) AbiTargeting(com.android.bundle.Targeting.AbiTargeting) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResultUtils.standaloneApkVariants(com.android.tools.build.bundletool.model.utils.ResultUtils.standaloneApkVariants) BundleConfigBuilder(com.android.tools.build.bundletool.testing.BundleConfigBuilder) RunWith(org.junit.runner.RunWith) BuildApksResult(com.android.bundle.Commands.BuildApksResult) ApkSetUtils.extractTocFromApkSetFile(com.android.tools.build.bundletool.testing.ApkSetUtils.extractTocFromApkSetFile) ProtoTruth.assertThat(com.google.common.truth.extensions.proto.ProtoTruth.assertThat) ImmutableList(com.google.common.collect.ImmutableList) AppBundleSerializer(com.android.tools.build.bundletool.io.AppBundleSerializer) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ApkDescription(com.android.bundle.Commands.ApkDescription) ResultUtils.splitApkVariants(com.android.tools.build.bundletool.model.utils.ResultUtils.splitApkVariants) Truth8.assertThat(com.google.common.truth.Truth8.assertThat) Before(org.junit.Before) PrintStream(java.io.PrintStream) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) ManifestProtoUtils.withFusingAttribute(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withFusingAttribute) TargetingUtils.targetedNativeDirectory(com.android.tools.build.bundletool.testing.TargetingUtils.targetedNativeDirectory) AssetSliceSet(com.android.bundle.Commands.AssetSliceSet) IOException(java.io.IOException) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) File(java.io.File) Aapt2Helper(com.android.tools.build.bundletool.testing.Aapt2Helper) ApkSetUtils.extractFromApkSetFile(com.android.tools.build.bundletool.testing.ApkSetUtils.extractFromApkSetFile) TargetingUtils.nativeLibraries(com.android.tools.build.bundletool.testing.TargetingUtils.nativeLibraries) Rule(org.junit.Rule) XmlNode(com.android.aapt.Resources.XmlNode) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) AppBundle(com.android.tools.build.bundletool.model.AppBundle) TemporaryFolder(org.junit.rules.TemporaryFolder) AppBundle(com.android.tools.build.bundletool.model.AppBundle) AppBundleBuilder(com.android.tools.build.bundletool.testing.AppBundleBuilder) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) AppBundleSerializer(com.android.tools.build.bundletool.io.AppBundleSerializer) Test(org.junit.Test)

Aggregations

AppBundleSerializer (com.android.tools.build.bundletool.io.AppBundleSerializer)32 AppBundleBuilder (com.android.tools.build.bundletool.testing.AppBundleBuilder)27 Test (org.junit.Test)27 AppBundle (com.android.tools.build.bundletool.model.AppBundle)25 Path (java.nio.file.Path)25 ManifestProtoUtils.androidManifest (com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifest)24 Truth.assertThat (com.google.common.truth.Truth.assertThat)24 Before (org.junit.Before)24 Rule (org.junit.Rule)24 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)24 TemporaryFolder (org.junit.rules.TemporaryFolder)24 RunWith (org.junit.runner.RunWith)24 JUnit4 (org.junit.runners.JUnit4)23 IOException (java.io.IOException)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 PrintStream (java.io.PrintStream)17 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)17 ImmutableList (com.google.common.collect.ImmutableList)15 XmlNode (com.android.aapt.Resources.XmlNode)7 CodeRelatedFile (com.android.bundle.CodeTransparencyOuterClass.CodeRelatedFile)7