Search in sources :

Example 1 with ZipReader

use of com.android.tools.build.bundletool.io.ZipReader 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)

Example 2 with ZipReader

use of com.android.tools.build.bundletool.io.ZipReader in project bundletool by google.

the class BuildSdkApksCommand method execute.

public void execute() {
    validateInput();
    try (ZipFile bundleZip = new ZipFile(getSdkBundlePath().toFile());
        ZipReader zipReader = ZipReader.createFromFile(getSdkBundlePath());
        TempDirectory tempDir = new TempDirectory(getClass().getSimpleName())) {
        SdkBundleValidator bundleValidator = SdkBundleValidator.create();
        bundleValidator.validateFile(bundleZip);
        SdkBundle sdkBundle = SdkBundle.buildFromZip(bundleZip, getVersionCode());
        bundleValidator.validate(sdkBundle);
        DaggerBuildSdkApksManagerComponent.builder().setBuildSdkApksCommand(this).setTempDirectory(tempDir).setSdkBundle(sdkBundle).setZipReader(zipReader).setUseBundleCompression(false).build().create().execute();
    } catch (ZipException e) {
        throw InvalidBundleException.builder().withCause(e).withUserMessage("The SDK Bundle is not a valid zip file.").build();
    } catch (IOException e) {
        throw new UncheckedIOException("An error occurred when validating the Sdk Bundle.", e);
    } finally {
        if (isExecutorServiceCreatedByBundleTool()) {
            getExecutorService().shutdown();
        }
    }
}
Also used : TempDirectory(com.android.tools.build.bundletool.io.TempDirectory) ZipFile(java.util.zip.ZipFile) SdkBundle(com.android.tools.build.bundletool.model.SdkBundle) SdkBundleValidator(com.android.tools.build.bundletool.validation.SdkBundleValidator) ZipReader(com.android.tools.build.bundletool.io.ZipReader) ZipException(java.util.zip.ZipException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 3 with ZipReader

use of com.android.tools.build.bundletool.io.ZipReader in project bundletool by google.

the class AppBundleRecompressor method recompressAppBundle.

public void recompressAppBundle(File inputFile, File outputFile) {
    try (ZipReader zipReader = ZipReader.createFromFile(inputFile.toPath());
        ZipArchive newBundle = new ZipArchive(outputFile);
        TempDirectory tempDirectory = new TempDirectory(getClass().getSimpleName())) {
        ZipEntrySourceFactory sourceFactory = new ZipEntrySourceFactory(zipReader, tempDirectory);
        List<ListenableFuture<ZipEntrySource>> sources = new ArrayList<>();
        BundleConfig bundleConfig = extractBundleConfig(zipReader);
        ImmutableSet<String> uncompressedAssetsModules = extractModulesWithUncompressedAssets(zipReader, bundleConfig);
        CompressionManager compressionManager = new CompressionManager(bundleConfig, uncompressedAssetsModules);
        for (Entry entry : zipReader.getEntries().values()) {
            CompressionLevel compressionLevel = compressionManager.getCompressionLevel(entry);
            // parallelization there either.
            if (compressionLevel.equals(SAME_AS_SOURCE) || compressionLevel.equals(NO_COMPRESSION) || entry.getUncompressedSize() < LARGE_ENTRY_SIZE_THRESHOLD_BYTES) {
                sources.add(immediateFuture(sourceFactory.create(entry, compressionLevel)));
            } else {
                sources.add(executor.submit(() -> sourceFactory.create(entry, compressionLevel)));
            }
        }
        // as they're ready.
        for (ListenableFuture<ZipEntrySource> sourceFuture : Futures.inCompletionOrder(sources)) {
            ZipEntrySource source = Futures.getUnchecked(sourceFuture);
            if (source.getCompressionLevel().isCompressed() && source.getCompressedSize() >= source.getUncompressedSize()) {
                // No benefit in compressing, leave the file uncompressed.
                newBundle.add(sourceFactory.create(source.getEntry(), NO_COMPRESSION));
            } else {
                newBundle.add(source);
            }
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) ZipArchive(com.android.zipflinger.ZipArchive) ZipReader(com.android.tools.build.bundletool.io.ZipReader) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ZipEntrySourceFactory(com.android.tools.build.bundletool.io.ZipEntrySourceFactory) TempDirectory(com.android.tools.build.bundletool.io.TempDirectory) BundleConfig(com.android.bundle.Config.BundleConfig) Entry(com.android.zipflinger.Entry) CompressionLevel(com.android.tools.build.bundletool.model.CompressionLevel) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ZipEntrySource(com.android.tools.build.bundletool.io.ZipEntrySource)

Aggregations

TempDirectory (com.android.tools.build.bundletool.io.TempDirectory)3 ZipReader (com.android.tools.build.bundletool.io.ZipReader)3 IOException (java.io.IOException)3 UncheckedIOException (java.io.UncheckedIOException)3 ZipException (java.util.zip.ZipException)2 ZipFile (java.util.zip.ZipFile)2 BundleConfig (com.android.bundle.Config.BundleConfig)1 ZipEntrySource (com.android.tools.build.bundletool.io.ZipEntrySource)1 ZipEntrySourceFactory (com.android.tools.build.bundletool.io.ZipEntrySourceFactory)1 AppBundle (com.android.tools.build.bundletool.model.AppBundle)1 CompressionLevel (com.android.tools.build.bundletool.model.CompressionLevel)1 SdkBundle (com.android.tools.build.bundletool.model.SdkBundle)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 SdkBundleValidator (com.android.tools.build.bundletool.validation.SdkBundleValidator)1 Entry (com.android.zipflinger.Entry)1 ZipArchive (com.android.zipflinger.ZipArchive)1 Closer (com.google.common.io.Closer)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1