Search in sources :

Example 11 with NativeLibraries

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

the class BuildBundleCommandTest method nativeLibrariesPresent_abiTargetingIsPresent.

@Test
public void nativeLibrariesPresent_abiTargetingIsPresent() throws Exception {
    Path moduleWithAbi = new ZipBuilder().addFileWithContent(ZipPath.create("lib/x86/libfast.so"), "native data".getBytes(UTF_8)).addFileWithContent(ZipPath.create("lib/x86/libfaster.so"), "native data".getBytes(UTF_8)).addFileWithProtoContent(ZipPath.create("manifest/AndroidManifest.xml"), androidManifest(PKG_NAME)).writeTo(tmpDir.resolve("base.zip"));
    BuildBundleCommand.builder().setOutputPath(bundlePath).setModulesPaths(ImmutableList.of(moduleWithAbi)).build().execute();
    NativeLibraries actualTargeting;
    try (ZipFile bundle = new ZipFile(bundlePath.toFile())) {
        assertThat(bundle).hasFile("base/native.pb");
        actualTargeting = NativeLibraries.parseFrom(bundle.getInputStream(new ZipEntry("base/native.pb")));
    }
    NativeLibraries expectedTargeting = NativeLibraries.newBuilder().addDirectory(TargetedNativeDirectory.newBuilder().setPath("lib/x86").setTargeting(NativeDirectoryTargeting.newBuilder().setAbi(Abi.newBuilder().setAlias(X86)))).build();
    Truth.assertThat(actualTargeting).isEqualTo(expectedTargeting);
}
Also used : Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) ZipFile(java.util.zip.ZipFile) NativeLibraries(com.android.bundle.Files.NativeLibraries) ZipEntry(java.util.zip.ZipEntry) ZipBuilder(com.android.tools.build.bundletool.io.ZipBuilder) Test(org.junit.Test)

Example 12 with NativeLibraries

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

the class TargetingGenerator method generateTargetingForNativeLibraries.

/**
 * Processes given native library directories, generating targeting based on their names.
 *
 * @param libDirectories Names of directories under lib/, including the "lib/" prefix.
 * @return Targeting for the given native libraries directories.
 */
public NativeLibraries generateTargetingForNativeLibraries(Collection<String> libDirectories) {
    NativeLibraries.Builder nativeLibraries = NativeLibraries.newBuilder();
    for (String directory : libDirectories) {
        checkRootDirectoryName(LIB_DIR, directory);
        // Split the directory under lib/ into tokens.
        String subDirName = directory.substring(LIB_DIR.length());
        Abi abi = checkAbiName(subDirName, directory);
        NativeDirectoryTargeting.Builder nativeBuilder = NativeDirectoryTargeting.newBuilder().setAbi(abi);
        if (subDirName.equals("arm64-v8a-hwasan")) {
            nativeBuilder.setSanitizer(Sanitizer.newBuilder().setAlias(SanitizerAlias.HWADDRESS));
        }
        nativeLibraries.addDirectory(TargetedNativeDirectory.newBuilder().setPath(directory).setTargeting(nativeBuilder).build());
    }
    return nativeLibraries.build();
}
Also used : NativeDirectoryTargeting(com.android.bundle.Targeting.NativeDirectoryTargeting) NativeLibraries(com.android.bundle.Files.NativeLibraries) MultiAbi(com.android.bundle.Targeting.MultiAbi) Abi(com.android.bundle.Targeting.Abi)

Example 13 with NativeLibraries

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

the class TargetingGeneratorTest method generateTargetingForNativeLibraries_sanitizer.

@Test
public void generateTargetingForNativeLibraries_sanitizer() throws Exception {
    NativeLibraries nativeTargeting = generator.generateTargetingForNativeLibraries(ImmutableList.of("lib/arm64-v8a-hwasan"));
    List<TargetedNativeDirectory> directories = nativeTargeting.getDirectoryList();
    assertThat(directories).hasSize(1);
    assertThat(directories.get(0)).isEqualTo(TargetedNativeDirectory.newBuilder().setPath("lib/arm64-v8a-hwasan").setTargeting(nativeDirectoryTargeting(AbiAlias.ARM64_V8A, SanitizerAlias.HWADDRESS)).build());
}
Also used : TargetedNativeDirectory(com.android.bundle.Files.TargetedNativeDirectory) NativeLibraries(com.android.bundle.Files.NativeLibraries) Test(org.junit.Test)

Example 14 with NativeLibraries

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

the class ModuleAbiSanitizer method sanitizedModule.

private static BundleModule sanitizedModule(BundleModule module, ImmutableMultimap<ZipPath, ModuleEntry> libFilesByAbiDirToKeep) {
    // Construct new module by making minimal modifications to the existing module.
    BundleModule.Builder newModule = module.toBuilder();
    if (module.getNativeConfig().isPresent()) {
        NativeLibraries newNativeConfig = filterNativeTargeting(module.getNativeConfig().get(), libFilesByAbiDirToKeep);
        newModule.setNativeConfig(newNativeConfig);
    }
    newModule.setEntryMap(module.getEntries().stream().filter(entry -> !entry.getPath().startsWith(LIB_DIRECTORY) || libFilesByAbiDirToKeep.containsValue(entry)).collect(toImmutableMap(ModuleEntry::getPath, Function.identity())));
    return newModule.build();
}
Also used : NativeLibraries(com.android.bundle.Files.NativeLibraries)

Example 15 with NativeLibraries

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

the class NativeTargetingValidatorTest method validateModule_pathOutsideLib_throws.

@Test
public void validateModule_pathOutsideLib_throws() throws Exception {
    NativeLibraries config = nativeLibraries(targetedNativeDirectory("lib/x86", nativeDirectoryTargeting(X86)), targetedNativeDirectory("assets/lib/x86_64", nativeDirectoryTargeting(X86_64)));
    BundleModule module = new BundleModuleBuilder("testModule").setNativeConfig(config).addFile("lib/x86/libX.so").setManifest(androidManifest("com.test.app")).build();
    InvalidBundleException e = assertThrows(InvalidBundleException.class, () -> new NativeTargetingValidator().validateModule(module));
    assertThat(e).hasMessageThat().contains("directory must be in format 'lib/<directory>'");
}
Also used : NativeLibraries(com.android.bundle.Files.NativeLibraries) BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) BundleModule(com.android.tools.build.bundletool.model.BundleModule) Test(org.junit.Test)

Aggregations

NativeLibraries (com.android.bundle.Files.NativeLibraries)26 Test (org.junit.Test)22 BundleModuleBuilder (com.android.tools.build.bundletool.testing.BundleModuleBuilder)18 BundleModule (com.android.tools.build.bundletool.model.BundleModule)17 ModuleSplit (com.android.tools.build.bundletool.model.ModuleSplit)10 ZipPath (com.android.tools.build.bundletool.model.ZipPath)6 Assets (com.android.bundle.Files.Assets)5 InvalidBundleException (com.android.tools.build.bundletool.model.exceptions.InvalidBundleException)5 TargetingUtils.nativeDirectoryTargeting (com.android.tools.build.bundletool.testing.TargetingUtils.nativeDirectoryTargeting)5 ResourceTable (com.android.aapt.Resources.ResourceTable)4 AbiAlias (com.android.bundle.Targeting.Abi.AbiAlias)4 ApkTargeting (com.android.bundle.Targeting.ApkTargeting)4 VariantTargeting (com.android.bundle.Targeting.VariantTargeting)4 AndroidManifest (com.android.tools.build.bundletool.model.AndroidManifest)4 AppBundle (com.android.tools.build.bundletool.model.AppBundle)4 BundleModuleName (com.android.tools.build.bundletool.model.BundleModuleName)4 StampType (com.android.tools.build.bundletool.model.SourceStamp.StampType)4 ManifestProtoUtils.androidManifest (com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifest)4 TargetingUtils.mergeApkTargeting (com.android.tools.build.bundletool.testing.TargetingUtils.mergeApkTargeting)4 TargetingUtils.nativeLibraries (com.android.tools.build.bundletool.testing.TargetingUtils.nativeLibraries)4