Search in sources :

Example 1 with ApexImages

use of com.android.bundle.Files.ApexImages in project bundletool by google.

the class ApexBundleValidator method validateModule.

@Override
public void validateModule(BundleModule module) {
    if (module.findEntriesUnderPath(APEX_DIRECTORY).count() == 0) {
        return;
    }
    Optional<ModuleEntry> apexManifest = module.getEntry(APEX_MANIFEST_PATH);
    if (apexManifest.isPresent()) {
        validateApexManifest(apexManifest.get());
    } else {
        apexManifest = module.getEntry(APEX_MANIFEST_JSON_PATH);
        if (!apexManifest.isPresent()) {
            throw InvalidBundleException.builder().withUserMessage("Missing expected file in APEX bundle: '%s' or '%s'.", APEX_MANIFEST_PATH, APEX_MANIFEST_JSON_PATH).build();
        }
        validateApexManifestJson(apexManifest.get());
    }
    ImmutableSet.Builder<String> apexImagesBuilder = ImmutableSet.builder();
    ImmutableSet.Builder<String> apexBuildInfosBuilder = ImmutableSet.builder();
    ImmutableSet.Builder<String> apexFileNamesBuilder = ImmutableSet.builder();
    for (ModuleEntry entry : module.getEntries()) {
        ZipPath path = entry.getPath();
        if (path.startsWith(APEX_DIRECTORY)) {
            if (path.getFileName().toString().endsWith(BundleModule.APEX_IMAGE_SUFFIX)) {
                apexImagesBuilder.add(path.toString());
                apexFileNamesBuilder.add(path.getFileName().toString());
            } else if (path.getFileName().toString().endsWith(BundleModule.BUILD_INFO_SUFFIX)) {
                apexBuildInfosBuilder.add(path.toString());
            } else {
                throw InvalidBundleException.builder().withUserMessage("Unexpected file in apex directory of bundle: '%s'.", entry.getPath()).build();
            }
        } else if (!ALLOWED_APEX_FILES_OUTSIDE_APEX_DIRECTORY.contains(path)) {
            throw InvalidBundleException.builder().withUserMessage("Unexpected file in APEX bundle: '%s'.", entry.getPath()).build();
        }
    }
    ImmutableSet<String> apexBuildInfos = apexBuildInfosBuilder.build();
    ImmutableSet<String> apexImages = apexImagesBuilder.build();
    ImmutableSet<ImmutableSet<AbiName>> allAbiNameSets = apexFileNamesBuilder.build().stream().map(ApexBundleValidator::abiNamesFromFile).collect(toImmutableSet());
    if (allAbiNameSets.size() != apexImages.size()) {
        throw InvalidBundleException.builder().withUserMessage("Every APEX image file must target a unique set of architectures, " + "but found multiple files that target the same set of architectures.").build();
    }
    ImmutableSet<String> expectedImages = apexBuildInfos.stream().map(f -> f.replace(BundleModule.BUILD_INFO_SUFFIX, BundleModule.APEX_IMAGE_SUFFIX)).collect(toImmutableSet());
    if (!apexBuildInfos.isEmpty() && !expectedImages.equals(apexImages)) {
        throw InvalidBundleException.builder().withUserMessage("If APEX build info is provided then one must be provided for each APEX image file:\n" + " Expected %s\n" + " Found %s.", expectedImages, apexImages).build();
    }
    if (REQUIRED_ONE_OF_ABI_SETS.stream().anyMatch(one_of -> one_of.stream().noneMatch(allAbiNameSets::contains))) {
        throw InvalidBundleException.builder().withUserMessage("APEX bundle must contain one of %s.", Joiner.on(" and one of ").join(REQUIRED_ONE_OF_ABI_SETS)).build();
    }
    module.getApexConfig().ifPresent(targeting -> validateTargeting(apexImages, targeting));
}
Also used : AbiName(com.android.tools.build.bundletool.model.AbiName) JsonObject(com.google.gson.JsonObject) ApexManifest(com.android.apex.ApexManifestProto.ApexManifest) ZipPath(com.android.tools.build.bundletool.model.ZipPath) JsonParser(com.google.gson.JsonParser) ARMEABI_V7A(com.android.tools.build.bundletool.model.AbiName.ARMEABI_V7A) JsonElement(com.google.gson.JsonElement) TargetedApexImage(com.android.bundle.Files.TargetedApexImage) ImmutableList(com.google.common.collect.ImmutableList) ARM64_V8A(com.android.tools.build.bundletool.model.AbiName.ARM64_V8A) X86(com.android.tools.build.bundletool.model.AbiName.X86) X86_64(com.android.tools.build.bundletool.model.AbiName.X86_64) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) APEX_MANIFEST_JSON_PATH(com.android.tools.build.bundletool.model.BundleModule.APEX_MANIFEST_JSON_PATH) APEX_PUBKEY_PATH(com.android.tools.build.bundletool.model.BundleModule.APEX_PUBKEY_PATH) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ABI_SPLITTER(com.android.tools.build.bundletool.model.BundleModule.ABI_SPLITTER) ImmutableSet(com.google.common.collect.ImmutableSet) UTF_8(java.nio.charset.StandardCharsets.UTF_8) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) IOException(java.io.IOException) Sets(com.google.common.collect.Sets) UncheckedIOException(java.io.UncheckedIOException) ApexImages(com.android.bundle.Files.ApexImages) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) Optional(java.util.Optional) BufferedReader(java.io.BufferedReader) APEX_DIRECTORY(com.android.tools.build.bundletool.model.BundleModule.APEX_DIRECTORY) APEX_NOTICE_PATH(com.android.tools.build.bundletool.model.BundleModule.APEX_NOTICE_PATH) BundleModule(com.android.tools.build.bundletool.model.BundleModule) APEX_MANIFEST_PATH(com.android.tools.build.bundletool.model.BundleModule.APEX_MANIFEST_PATH) Joiner(com.google.common.base.Joiner) InputStream(java.io.InputStream) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ImmutableSet(com.google.common.collect.ImmutableSet) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ZipPath(com.android.tools.build.bundletool.model.ZipPath)

Example 2 with ApexImages

use of com.android.bundle.Files.ApexImages in project bundletool by google.

the class StandaloneApexApksGeneratorTest method shardApexModule_oneApexImage.

@Test
public void shardApexModule_oneApexImage() throws Exception {
    ApexImages apexConfig = apexImages(targetedApexImage("apex/x86.img", apexImageTargeting("x86")));
    BundleModule apexModule = new BundleModuleBuilder("base").addFile("apex/x86.img").addFile("root/apex_manifest.json").setManifest(androidManifest("com.test.app")).setApexConfig(apexConfig).build();
    ImmutableList<ModuleSplit> shards = standaloneApexApksGenerator.generateStandaloneApks(ImmutableList.of(apexModule));
    assertThat(shards).hasSize(1);
    assertThat(shards.get(0).getApkTargeting()).isEqualTo(apkMultiAbiTargeting(X86));
    assertThat(extractPaths(shards.get(0).getEntries())).containsExactly("apex/x86.img", "root/apex_manifest.json");
}
Also used : ApexImages(com.android.bundle.Files.ApexImages) BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) BundleModule(com.android.tools.build.bundletool.model.BundleModule) Test(org.junit.Test)

Example 3 with ApexImages

use of com.android.bundle.Files.ApexImages in project bundletool by google.

the class AbiApexImagesSplitterTest method splittingBySingleAbi_twoImageFilesWithBuildInfo.

@Test
public void splittingBySingleAbi_twoImageFilesWithBuildInfo() throws Exception {
    ApexImages apexConfig = apexImages(targetedApexImageWithBuildInfo("apex/x86.img", "apex/x86.build_info.pb", apexImageTargeting("x86")), targetedApexImageWithBuildInfo("apex/x86_64.img", "apex/x86_64.build_info.pb", apexImageTargeting("x86_64")));
    BundleModule bundleModule = new BundleModuleBuilder("testModule").addFile("apex/x86.img").addFile("apex/x86.build_info.pb").addFile("apex/x86_64.img").addFile("apex/x86_64.build_info.pb").setApexConfig(apexConfig).setManifest(androidManifest("com.test.app")).build();
    AbiApexImagesSplitter abiApexImagesSplitter = new AbiApexImagesSplitter();
    ImmutableCollection<ModuleSplit> splits = abiApexImagesSplitter.split(ModuleSplit.forApex(bundleModule));
    assertThat(splits).hasSize(2);
    ApkTargeting x86Targeting = apkMultiAbiTargeting(X86, ImmutableSet.of(X86_64));
    ApkTargeting x64Targeting = apkMultiAbiTargeting(X86_64, ImmutableSet.of(X86));
    ImmutableMap<ApkTargeting, ModuleSplit> splitsByTargeting = Maps.uniqueIndex(splits, ModuleSplit::getApkTargeting);
    assertThat(splitsByTargeting.keySet()).containsExactly(x86Targeting, x64Targeting);
    assertThat(extractPaths(splitsByTargeting.get(x86Targeting).getEntries())).containsExactly("apex/x86.img", "apex/x86.build_info.pb");
    assertThat(extractPaths(splitsByTargeting.get(x64Targeting).getEntries())).containsExactly("apex/x86_64.img", "apex/x86_64.build_info.pb");
}
Also used : ApexImages(com.android.bundle.Files.ApexImages) ApkTargeting(com.android.bundle.Targeting.ApkTargeting) BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) BundleModule(com.android.tools.build.bundletool.model.BundleModule) Test(org.junit.Test)

Example 4 with ApexImages

use of com.android.bundle.Files.ApexImages in project bundletool by google.

the class AbiApexImagesSplitterTest method createSingleImageModule.

/**
 * Creates a minimal module with one apex image file targeted at the given cpu architecture.
 */
private static BundleModule createSingleImageModule(String moduleName, String architecture) throws Exception {
    String relativeFilePath = "apex/" + architecture + ".img";
    ApexImages apexConfig = apexImages(targetedApexImage(relativeFilePath, apexImageTargeting(architecture)));
    return new BundleModuleBuilder(moduleName).addFile(relativeFilePath).setApexConfig(apexConfig).setManifest(androidManifest("com.test.app")).build();
}
Also used : ApexImages(com.android.bundle.Files.ApexImages) BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder)

Example 5 with ApexImages

use of com.android.bundle.Files.ApexImages in project bundletool by google.

the class BundleModuleTest method correctApexProtoFile_parsedAndReturned.

@Test
public void correctApexProtoFile_parsedAndReturned() throws Exception {
    ApexImages apexConfig = ApexImages.newBuilder().addImage(TargetedApexImage.newBuilder().setPath("apex/x86.img")).build();
    BundleModule bundleModule = createMinimalModuleBuilder().addEntry(createModuleEntryForFile("apex.pb", apexConfig.toByteArray())).build();
    assertThat(bundleModule.getApexConfig()).hasValue(apexConfig);
}
Also used : ApexImages(com.android.bundle.Files.ApexImages) Test(org.junit.Test)

Aggregations

ApexImages (com.android.bundle.Files.ApexImages)24 Test (org.junit.Test)20 BundleModule (com.android.tools.build.bundletool.model.BundleModule)13 BundleModuleBuilder (com.android.tools.build.bundletool.testing.BundleModuleBuilder)12 InvalidBundleException (com.android.tools.build.bundletool.model.exceptions.InvalidBundleException)11 ZipPath (com.android.tools.build.bundletool.model.ZipPath)8 AbiAlias (com.android.bundle.Targeting.Abi.AbiAlias)7 ApkTargeting (com.android.bundle.Targeting.ApkTargeting)7 ImmutableList (com.google.common.collect.ImmutableList)7 ImmutableSet (com.google.common.collect.ImmutableSet)7 ModuleSplit (com.android.tools.build.bundletool.model.ModuleSplit)6 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)6 IOException (java.io.IOException)6 Path (java.nio.file.Path)5 ZipFile (java.util.zip.ZipFile)5 BundleConfig (com.android.bundle.Config.BundleConfig)4 AppBundle (com.android.tools.build.bundletool.model.AppBundle)4 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 NativeLibraries (com.android.bundle.Files.NativeLibraries)3 AssetsDirectoryTargeting (com.android.bundle.Targeting.AssetsDirectoryTargeting)3