Search in sources :

Example 26 with Assets

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

the class BuildBundleCommandTest method assetsTargeting_generated.

@Test
public void assetsTargeting_generated() throws Exception {
    XmlNode manifest = androidManifest(PKG_NAME, withHasCode(true));
    Assets assetsConfig = Assets.newBuilder().addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets").setTargeting(AssetsDirectoryTargeting.getDefaultInstance())).addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/texture#tcf_atc/device#tier_0").setTargeting(mergeAssetsTargeting(assetsDirectoryTargeting(textureCompressionTargeting(TextureCompressionFormatAlias.ATC)), assetsDirectoryTargeting(deviceTierTargeting(0))))).build();
    Path module = new ZipBuilder().addFileWithContent(ZipPath.create("assets/anything.dat"), "any".getBytes(UTF_8)).addFileWithContent(ZipPath.create("assets/texture#tcf_atc/device#tier_0/file.dat"), "any2".getBytes(UTF_8)).addFileWithContent(ZipPath.create("dex/classes.dex"), "dex".getBytes(UTF_8)).addFileWithProtoContent(ZipPath.create("manifest/AndroidManifest.xml"), manifest).writeTo(tmpDir.resolve("base.zip"));
    BuildBundleCommand.builder().setOutputPath(bundlePath).setModulesPaths(ImmutableList.of(module)).build().execute();
    try (ZipFile bundle = new ZipFile(bundlePath.toFile())) {
        assertThat(bundle).hasFile("base/assets/anything.dat").withContent("any".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/assets/texture#tcf_atc/device#tier_0/file.dat").withContent("any2".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/dex/classes.dex").withContent("dex".getBytes(UTF_8));
        assertThat(bundle).hasFile("base/manifest/AndroidManifest.xml").withContent(manifest.toByteArray());
        assertThat(bundle).hasFile("base/assets.pb").withContent(assetsConfig.toByteArray());
    }
}
Also used : Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) XmlNode(com.android.aapt.Resources.XmlNode) ZipFile(java.util.zip.ZipFile) Assets(com.android.bundle.Files.Assets) ZipBuilder(com.android.tools.build.bundletool.io.ZipBuilder) Test(org.junit.Test)

Example 27 with Assets

use of com.android.bundle.Files.Assets 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)

Aggregations

Assets (com.android.bundle.Files.Assets)27 Test (org.junit.Test)21 BundleModule (com.android.tools.build.bundletool.model.BundleModule)11 BundleModuleBuilder (com.android.tools.build.bundletool.testing.BundleModuleBuilder)9 ZipPath (com.android.tools.build.bundletool.model.ZipPath)8 InvalidBundleException (com.android.tools.build.bundletool.model.exceptions.InvalidBundleException)7 TargetedAssetsDirectory (com.android.bundle.Files.TargetedAssetsDirectory)4 ModuleEntry (com.android.tools.build.bundletool.model.ModuleEntry)4 AssetsDirectoryTargeting (com.android.bundle.Targeting.AssetsDirectoryTargeting)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 XmlNode (com.android.aapt.Resources.XmlNode)2 NativeLibraries (com.android.bundle.Files.NativeLibraries)2 ApkTargeting (com.android.bundle.Targeting.ApkTargeting)2 AppBundle (com.android.tools.build.bundletool.model.AppBundle)2 ModuleSplit (com.android.tools.build.bundletool.model.ModuleSplit)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)2 Path (java.nio.file.Path)2 ZipFile (java.util.zip.ZipFile)2