Search in sources :

Example 1 with AppBundleRecompressor

use of com.android.tools.build.bundletool.preprocessors.AppBundleRecompressor in project bundletool by google.

the class BuildApksCommand method execute.

public Path execute() {
    validateInput();
    Path outputDirectory = getOutputFormat().equals(APK_SET) ? getOutputFile().getParent() : getOutputFile();
    if (outputDirectory != null && Files.notExists(outputDirectory)) {
        logger.info("Output directory '" + outputDirectory + "' does not exist, creating it.");
        FileUtils.createDirectories(outputDirectory);
    }
    try (TempDirectory tempDir = new TempDirectory(getClass().getSimpleName())) {
        Path bundlePath;
        // The old APK serializer relies on the compression of entries in the App Bundle.
        // Unfortunately, we don't know the compression level that was used when the bundle was built,
        // so we re-compress all entries with our desired compression level.
        // Exception is made when the device spec is specified, we only need a fraction of the
        // entries, so re-compressing all entries would be a waste of CPU.
        boolean recompressAppBundle = !getDeviceSpec().isPresent() && !getEnableApkSerializerWithoutBundleRecompression();
        if (recompressAppBundle) {
            bundlePath = tempDir.getPath().resolve("recompressed.aab");
            new AppBundleRecompressor(getExecutorService()).recompressAppBundle(getBundlePath().toFile(), bundlePath.toFile());
        } else {
            bundlePath = getBundlePath();
        }
        try (ZipFile bundleZip = new ZipFile(bundlePath.toFile());
            ZipReader zipReader = ZipReader.createFromFile(bundlePath);
            Closer closer = Closer.create()) {
            AppBundleValidator bundleValidator = AppBundleValidator.create(getExtraValidators());
            bundleValidator.validateFile(bundleZip);
            AppBundle appBundle = AppBundle.buildFromZip(bundleZip);
            bundleValidator.validate(appBundle);
            validateSdkBundles(closer);
            AppBundlePreprocessorManager appBundlePreprocessorManager = DaggerAppBundlePreprocessorComponent.builder().setBuildApksCommand(this).build().create();
            AppBundle preprocessedAppBundle = appBundlePreprocessorManager.processAppBundle(appBundle);
            BuildApksManager buildApksManager = DaggerBuildApksManagerComponent.builder().setBuildApksCommand(this).setTempDirectory(tempDir).setAppBundle(preprocessedAppBundle).setZipReader(zipReader).setUseBundleCompression(recompressAppBundle).build().create();
            buildApksManager.execute();
        } catch (ZipException e) {
            throw InvalidBundleException.builder().withCause(e).withUserMessage("The App Bundle is not a valid zip file.").build();
        } finally {
            if (isExecutorServiceCreatedByBundleTool()) {
                getExecutorService().shutdown();
            }
        }
    } catch (IOException e) {
        throw new UncheckedIOException("An error occurred when processing the App Bundle.", e);
    }
    return getOutputFile();
}
Also used : Path(java.nio.file.Path) Closer(com.google.common.io.Closer) AppBundleValidator(com.android.tools.build.bundletool.validation.AppBundleValidator) AppBundle(com.android.tools.build.bundletool.model.AppBundle) ZipReader(com.android.tools.build.bundletool.io.ZipReader) ZipException(java.util.zip.ZipException) UncheckedIOException(java.io.UncheckedIOException) AppBundlePreprocessorManager(com.android.tools.build.bundletool.preprocessors.AppBundlePreprocessorManager) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) TempDirectory(com.android.tools.build.bundletool.io.TempDirectory) AppBundleRecompressor(com.android.tools.build.bundletool.preprocessors.AppBundleRecompressor) ZipFile(java.util.zip.ZipFile)

Aggregations

TempDirectory (com.android.tools.build.bundletool.io.TempDirectory)1 ZipReader (com.android.tools.build.bundletool.io.ZipReader)1 AppBundle (com.android.tools.build.bundletool.model.AppBundle)1 AppBundlePreprocessorManager (com.android.tools.build.bundletool.preprocessors.AppBundlePreprocessorManager)1 AppBundleRecompressor (com.android.tools.build.bundletool.preprocessors.AppBundleRecompressor)1 AppBundleValidator (com.android.tools.build.bundletool.validation.AppBundleValidator)1 Closer (com.google.common.io.Closer)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 Path (java.nio.file.Path)1 ZipException (java.util.zip.ZipException)1 ZipFile (java.util.zip.ZipFile)1