use of com.android.tools.build.bundletool.io.TempDirectory 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.TempDirectory 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.TempDirectory in project bundletool by google.
the class InstallApksCommand method execute.
public void execute() {
BuildApksResult toc = readBuildApksResult();
validateInput(toc);
AdbServer adbServer = getAdbServer();
adbServer.init(getAdbPath());
try (TempDirectory tempDirectory = new TempDirectory()) {
DeviceSpec deviceSpec = new DeviceAnalyzer(adbServer).getDeviceSpec(getDeviceId());
if (getDeviceTier().isPresent()) {
deviceSpec = deviceSpec.toBuilder().setDeviceTier(Int32Value.of(getDeviceTier().get())).build();
}
if (getDeviceGroups().isPresent()) {
deviceSpec = deviceSpec.toBuilder().addAllDeviceGroups(getDeviceGroups().get()).build();
}
final ImmutableList<Path> apksToInstall = getApksToInstall(toc, deviceSpec, tempDirectory.getPath());
final ImmutableList<Path> filesToPush = ImmutableList.<Path>builder().addAll(getApksToPushToStorage(toc, deviceSpec, tempDirectory.getPath())).addAll(getAdditionalLocalTestingFiles().orElse(ImmutableList.of())).build();
AdbRunner adbRunner = new AdbRunner(adbServer);
InstallOptions installOptions = InstallOptions.builder().setAllowDowngrade(getAllowDowngrade()).setAllowTestOnly(getAllowTestOnly()).setTimeout(getTimeout()).build();
if (getDeviceId().isPresent()) {
adbRunner.run(device -> device.installApks(apksToInstall, installOptions), getDeviceId().get());
} else {
adbRunner.run(device -> device.installApks(apksToInstall, installOptions));
}
if (!filesToPush.isEmpty()) {
pushFiles(filesToPush, toc, adbRunner);
}
if (toc.getLocalTestingInfo().getEnabled()) {
cleanUpEmulatedSplits(adbRunner, toc);
}
}
}
use of com.android.tools.build.bundletool.io.TempDirectory in project bundletool by google.
the class InstallMultiApksCommand method extractApkListFromApks.
/**
* Extracts the apk/apex files that will be installed from a given .apks.
*/
private static ImmutableList<PackagePathVersion> extractApkListFromApks(DeviceSpec deviceSpec, PackagePathVersion apksArchive, Optional<InstalledPackageInfo> installedPackage, TempDirectory tempDirectory) {
logger.info(String.format("Extracting package '%s'", apksArchive.getPackageName()));
try {
Path output = tempDirectory.getPath().resolve(apksArchive.getPackageName());
Files.createDirectory(output);
ExtractApksCommand.Builder extractApksCommand = ExtractApksCommand.builder().setApksArchivePath(apksArchive.getPath()).setDeviceSpec(deviceSpec).setOutputDirectory(output);
ImmutableList<Path> extractedPaths = fixExtension(extractApksCommand.build().execute(), installedPackage.map(InstalledPackageInfo::isApex).orElse(false));
return extractedPaths.stream().map(path -> PackagePathVersion.create(path, apksArchive.getPackageName(), apksArchive.getVersionCode())).collect(toImmutableList());
} catch (IncompatibleDeviceException e) {
logger.warning(String.format("Package '%s' is not supported by the attached device (SDK version %d). Skipping.", apksArchive.getPackageName(), deviceSpec.getSdkVersion()));
return ImmutableList.of();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
use of com.android.tools.build.bundletool.io.TempDirectory in project bundletool by google.
the class InstallMultiApksCommand method apksWithPackageName.
private static Optional<PackagePathVersion> apksWithPackageName(Path apksArchivePath, DeviceSpec deviceSpec, Supplier<Aapt2Command> aapt2CommandSupplier) {
try (TempDirectory tempDirectory = new TempDirectory()) {
// Any of the extracted .apk/.apex files will work.
Path extractedFile = ExtractApksCommand.builder().setApksArchivePath(apksArchivePath).setDeviceSpec(deviceSpec).setOutputDirectory(tempDirectory.getPath()).build().execute().get(0);
BadgingInfo badgingInfo = BadgingInfoParser.parse(aapt2CommandSupplier.get().dumpBadging(extractedFile));
return Optional.of(PackagePathVersion.create(apksArchivePath, badgingInfo.getPackageName(), badgingInfo.getVersionCode()));
} catch (IncompatibleDeviceException e) {
logger.warning(String.format("Unable to determine package name of %s, as it is not compatible with the attached" + " device. Skipping.", apksArchivePath));
return Optional.empty();
}
}
Aggregations