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();
}
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();
}
}
}
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);
}
}
Aggregations