use of com.android.tools.build.bundletool.device.ApkMatcher.GeneratedApk in project bundletool by google.
the class ExtractApksCommand method extractMatchedApksFromApksArchive.
private ImmutableList<Path> extractMatchedApksFromApksArchive(ImmutableList<GeneratedApk> generatedApks, BuildApksResult toc) {
Path outputDirectoryPath = getOutputDirectory().orElseGet(ExtractApksCommand::createTempDirectory);
getOutputDirectory().ifPresent(dir -> {
if (!Files.exists(dir)) {
logger.info("Output directory '" + dir + "' does not exist, creating it.");
FileUtils.createDirectories(dir);
}
});
ImmutableList.Builder<Path> builder = ImmutableList.builder();
try (ZipFile apksArchive = new ZipFile(getApksArchivePath().toFile())) {
for (GeneratedApk matchedApk : generatedApks) {
ZipEntry entry = apksArchive.getEntry(matchedApk.getPath().toString());
checkNotNull(entry);
Path extractedApkPath = outputDirectoryPath.resolve(matchedApk.getPath().getFileName().toString());
try (InputStream inputStream = apksArchive.getInputStream(entry);
OutputStream outputApk = Files.newOutputStream(extractedApkPath)) {
ByteStreams.copy(inputStream, outputApk);
builder.add(extractedApkPath);
} catch (IOException e) {
throw new UncheckedIOException(String.format("Error while extracting APK '%s' from the APK Set.", matchedApk), e);
}
}
if (getIncludeMetadata()) {
produceCommandMetadata(generatedApks, toc, outputDirectoryPath);
}
} catch (IOException e) {
throw new UncheckedIOException(String.format("Error while processing the APK Set archive '%s'.", getApksArchivePath()), e);
}
System.err.printf("The APKs have been extracted in the directory: %s%n", outputDirectoryPath.toString());
return builder.build();
}
use of com.android.tools.build.bundletool.device.ApkMatcher.GeneratedApk in project bundletool by google.
the class ApkMatcherTest method apkMatch_withModuleNameFiltering_splitApks_permanentlyMergedModule.
@Test
public void apkMatch_withModuleNameFiltering_splitApks_permanentlyMergedModule() {
DeviceSpec device = deviceWithSdk(21);
ZipPath apk = ZipPath.create("master-de-fr.apk");
BuildApksResult buildApksResult = buildApksResult(createVariant(VariantTargeting.getDefaultInstance(), splitApkSet(/* moduleName= */
"base", splitApkDescription(ApkTargeting.getDefaultInstance(), apk)))).toBuilder().addPermanentlyFusedModules(PermanentlyFusedModule.newBuilder().setName("my-module")).build();
ImmutableList<GeneratedApk> matchedApks = createMatcher(device, Optional.of(ImmutableSet.of("my-module"))).getMatchingApks(buildApksResult);
assertThat(matchedApks).containsExactly(baseMatchedApk(apk));
}
use of com.android.tools.build.bundletool.device.ApkMatcher.GeneratedApk in project bundletool by google.
the class ExtractApksCommand method execute.
@VisibleForTesting
ImmutableList<Path> execute(PrintStream output) {
validateInput();
BuildApksResult toc = ResultUtils.readTableOfContents(getApksArchivePath());
Optional<ImmutableSet<String>> requestedModuleNames = getModules().map(modules -> resolveRequestedModules(modules, toc));
DeviceSpec deviceSpec = applyDefaultsToDeviceSpec(getDeviceSpec(), toc);
ApkMatcher apkMatcher = new ApkMatcher(deviceSpec, requestedModuleNames, getInstant(), /* ensureDensityAndAbiApksMatched= */
true);
ImmutableList<GeneratedApk> generatedApks = apkMatcher.getMatchingApks(toc);
if (generatedApks.isEmpty()) {
throw IncompatibleDeviceException.builder().withUserMessage("No compatible APKs found for the device.").build();
}
if (Files.isDirectory(getApksArchivePath())) {
return generatedApks.stream().map(matchedApk -> getApksArchivePath().resolve(matchedApk.getPath().toString())).collect(toImmutableList());
} else {
return extractMatchedApksFromApksArchive(generatedApks, toc);
}
}
use of com.android.tools.build.bundletool.device.ApkMatcher.GeneratedApk in project bundletool by google.
the class ExtractApksCommand method produceCommandMetadata.
private static void produceCommandMetadata(ImmutableList<GeneratedApk> generatedApks, BuildApksResult toc, Path outputDir) {
ImmutableList<ExtractedApk> apks = generatedApks.stream().map(apk -> ExtractedApk.newBuilder().setPath(apk.getPath().getFileName().toString()).setModuleName(apk.getModuleName()).setDeliveryType(apk.getDeliveryType()).build()).collect(toImmutableList());
try {
JsonFormat.Printer printer = JsonFormat.printer();
ExtractApksResult.Builder builder = ExtractApksResult.newBuilder();
if (toc.getLocalTestingInfo().getEnabled()) {
builder.setLocalTestingInfo(createLocalTestingInfo(toc));
}
String metadata = printer.print(builder.addAllApks(apks).build());
Files.write(outputDir.resolve(METADATA_FILE), metadata.getBytes(UTF_8));
} catch (IOException e) {
throw new UncheckedIOException("Error while writing metadata.json.", e);
}
}
Aggregations