/** Verifies that all given ApkDescriptions point to the APK files in the output directory. */ private void verifyApksExist(Collection<ApkDescription> apkDescs, Path outputDirectory) { apkDescs.stream() .map(apkDesc -> outputDirectory.resolve(apkDesc.getPath())) .forEach(FilePreconditions::checkFileExistsAndReadable); }
public static Path createApksDirectory(BuildApksResult result, Path location) throws Exception { ImmutableList<ApkDescription> apkDescriptions = result.getVariantList().stream() .flatMap(variant -> variant.getApkSetList().stream()) .flatMap(apkSet -> apkSet.getApkDescriptionList().stream()) .collect(toImmutableList()); for (ApkDescription apkDescription : apkDescriptions) { Path apkPath = location.resolve(apkDescription.getPath()); Files.createDirectories(apkPath.getParent()); Files.write(apkPath, DUMMY_BYTES); } Files.write(location.resolve("toc.pb"), result.toByteArray()); return location; }
private ConfigurationSizes getSizeStandaloneVariant() { checkState( !getSizeRequest.getInstant(), "Standalone Variants cant be selected when instant flag is set"); // When modules are specified we ignore standalone variants. if (getSizeRequest.getModules().isPresent()) { return ConfigurationSizes.create(ImmutableMap.of(), ImmutableMap.of()); } VariantTargeting variantTargeting = variant.getTargeting(); SizeConfiguration sizeConfiguration = mergeWithDeviceSpec( getSizeConfiguration( variantTargeting.getSdkVersionTargeting(), variantTargeting.getAbiTargeting(), variantTargeting.getScreenDensityTargeting(), LanguageTargeting.getDefaultInstance()), getSizeRequest.getDeviceSpec()); // Variants of standalone APKs have only one APK each. long compressedSize = sizeByApkPaths.get( Iterables.getOnlyElement( Iterables.getOnlyElement(variant.getApkSetList()).getApkDescriptionList()) .getPath()); ImmutableMap<SizeConfiguration, Long> sizeConfigurationMap = ImmutableMap.of(sizeConfiguration, compressedSize); return ConfigurationSizes.create(sizeConfigurationMap, sizeConfigurationMap); }
assertThat(baseSplits.getModuleMetadata().getOnDemand()).isFalse(); assertThat(baseSplits.getApkDescriptionList()).hasSize(1); assertThat(apkSetFile).hasFile(baseSplits.getApkDescription(0).getPath()); assertThat(onDemandSplits.getModuleMetadata().getOnDemand()).isTrue(); assertThat(onDemandSplits.getApkDescriptionList()).hasSize(1); assertThat(apkSetFile).hasFile(onDemandSplits.getApkDescription(0).getPath());
.isEqualTo(abiTargeting(AbiAlias.X86, ImmutableSet.of())); File standaloneApkFile = extractFromApkSetFile(apkSetFile, standaloneApks.get(0).getPath(), outputDir); try (ZipFile standaloneApkZipFile = new ZipFile(standaloneApkFile)) { assertThat(filesUnderPath(standaloneApkZipFile, ZipPath.create("lib")))
assertThat(apkSetFile).hasFile(shard.getPath()); assertThat(ZipPath.create(shard.getPath()).getFileName()) .isEqualTo(ZipPath.create("standalone.apk")); try (ZipFile shardZip = new ZipFile(extractFromApkSetFile(apkSetFile, shard.getPath(), outputDir))) { assertThat(shardZip).hasFile("classes.dex"); byte[] mergedDexData =
variant -> getOnlyElement(getOnlyElement(variant.getApkSetList()).getApkDescriptionList()) .getPath()) .collect(toImmutableSet()); assertThat(apkPaths)
ApkSet apkSet = variant.getApkSetList().get(0); assertThat(apkSet.getModuleMetadata().getIsInstant()).isTrue(); assertThat(apkSetFile).hasFile(apkSet.getApkDescription(0).getPath()); apkSet = variant.getApkSetList().get(0); assertThat(apkSet.getModuleMetadata().getIsInstant()).isTrue(); assertThat(apkSetFile).hasFile(apkSet.getApkDescription(0).getPath());
@Test public void buildApksCommand_standalone_oneModuleOneVariant() throws Exception { bundlePath = FileUtils.getRandomFilePath(tmp, "bundle-", ".aab"); AppBundle appBundle = new AppBundleBuilder() .addModule( "base", builder -> builder.addFile("dex/classes.dex").setManifest(androidManifest("com.test.app"))) .build(); bundleSerializer.writeToDisk(appBundle, bundlePath); BuildApksCommand command = BuildApksCommand.builder() .setBundlePath(bundlePath) .setOutputFile(outputFilePath) .setAapt2Command(aapt2Command) .build(); Path apkSetFilePath = execute(command); ZipFile apkSetFile = new ZipFile(apkSetFilePath.toFile()); BuildApksResult result = extractTocFromApkSetFile(apkSetFile, outputDir); assertThat(standaloneApkVariants(result)).hasSize(1); Variant standaloneApkVariant = standaloneApkVariants(result).get(0); assertThat(standaloneApkVariant.getApkSetList()).hasSize(1); ApkSet shards = standaloneApkVariant.getApkSet(0); assertThat(shards.getModuleMetadata().getName()).isEqualTo("base"); assertThat(shards.getApkDescriptionList()).hasSize(1); assertThat(ZipPath.create(shards.getApkDescription(0).getPath()).getFileName()) .isEqualTo(ZipPath.create("standalone.apk")); assertThat(apkSetFile).hasFile(shards.getApkDescription(0).getPath()); }
/** * Writes an compressed system APK and stub system APK containing just android manifest to disk. */ public ImmutableList<ApkDescription> writeCompressedSystemApksToDisk( ModuleSplit systemSplit, Path outputDirectory) { ApkDescription stubApkDescription = writeSystemApkToDiskInternal( splitWithOnlyManifest(systemSplit), outputDirectory, SystemApkType.SYSTEM_STUB); ZipPath compressedApkPath = ZipPath.create(getCompressedApkPathFromStubApkPath(stubApkDescription.getPath())); apkSerializerHelper.writeCompressedApkToZipFile( systemSplit, outputDirectory.resolve(compressedApkPath.toString())); return ImmutableList.of( stubApkDescription, createSystemApkDescription( systemSplit, compressedApkPath, SystemApkType.SYSTEM_COMPRESSED)); }
public ImmutableList<ZipPath> getMatchingApksFromVariant(Variant variant) { ImmutableList.Builder<ZipPath> matchedApksBuilder = ImmutableList.builder(); Predicate<String> moduleNameMatcher = getModuleNameMatcher(variant); for (ApkSet apkSet : variant.getApkSetList()) { String moduleName = apkSet.getModuleMetadata().getName(); for (ApkDescription apkDescription : apkSet.getApkDescriptionList()) { ApkTargeting apkTargeting = apkDescription.getTargeting(); boolean isSplit = !apkDescription.hasStandaloneApkMetadata(); checkCompatibleWithApkTargeting(apkTargeting); if (matchesApk(apkTargeting, isSplit, moduleName, moduleNameMatcher)) { matchedApksBuilder.add(ZipPath.create(apkDescription.getPath())); } } } return matchedApksBuilder.build(); }
private void assertThatApksAreSigned( BuildApksResult result, ZipFile apkSetFile, X509Certificate expectedCertificate) throws Exception { for (Variant variant : result.getVariantList()) { for (ApkSet apkSet : variant.getApkSetList()) { for (ApkDescription apkDescription : apkSet.getApkDescriptionList()) { File apk = extractFromApkSetFile(apkSetFile, apkDescription.getPath(), outputDir); ApkVerifier.Result verifierResult = new ApkVerifier.Builder(apk).build().verify(); assertThat(verifierResult.isVerified()).isTrue(); assertThat(verifierResult.getSignerCertificates()).containsExactly(expectedCertificate); } } } }
@Override public ImmutableList<ApkDescription> addCompressedSystemApks(ModuleSplit split) { ImmutableList<ApkDescription> apkDescriptions = standaloneApkSerializer.writeCompressedSystemApksToDisk(split, tempDirectory); apkDescriptions.forEach(apkDescription -> addToApkSetArchive(apkDescription.getPath())); return apkDescriptions; }
@Override public ApkDescription addSystemApk(ModuleSplit split) { ApkDescription apkDescription = standaloneApkSerializer.writeSystemApkToDisk(split, tempDirectory); addToApkSetArchive(apkDescription.getPath()); return apkDescription; }
@Override public ApkDescription addStandaloneUniversalApk(ModuleSplit split) { ApkDescription apkDescription = standaloneApkSerializer.writeToDiskAsUniversal(split, tempDirectory); addToApkSetArchive(apkDescription.getPath()); return apkDescription; }
@Override public ApkDescription addInstantApk(ModuleSplit split) { ApkDescription apkDescription = splitApkSerializer.writeInstantSplitToDisk(split, tempDirectory); addToApkSetArchive(apkDescription.getPath()); return apkDescription; }
/** Extracts names of files inside the APK described by {@code apkDesc}. */ private ImmutableSet<String> filesInApk(ApkDescription apkDesc, ZipFile apkSetFile) { try { return filesInApk(extractFromApkSetFile(apkSetFile, apkDesc.getPath(), outputDir)); } catch (Exception e) { throw new RuntimeException("Failed to verify files in APK.", e); } }
public static Path createApksArchiveFile(BuildApksResult result, Path location) throws Exception { ZipBuilder archiveBuilder = new ZipBuilder(); result.getVariantList().stream() .flatMap(variant -> variant.getApkSetList().stream()) .flatMap(apkSet -> apkSet.getApkDescriptionList().stream()) .forEach( apkDesc -> archiveBuilder.addFileWithContent(ZipPath.create(apkDesc.getPath()), DUMMY_BYTES)); archiveBuilder.addFileWithProtoContent(ZipPath.create("toc.pb"), result); return archiveBuilder.writeTo(location); }
@Override public ApkDescription addSplitApk(ModuleSplit split) { ApkDescription apkDescription = splitApkSerializer.writeSplitToDisk(split, tempDirectory); addToApkSetArchive(apkDescription.getPath()); return apkDescription; }
@Override public ApkDescription addStandaloneApk(ModuleSplit split) { ApkDescription apkDescription = standaloneApkSerializer.writeToDisk(split, tempDirectory); addToApkSetArchive(apkDescription.getPath()); return apkDescription; }