Search in sources :

Example 6 with BuildSdkApksResult

use of com.android.bundle.Commands.BuildSdkApksResult in project bundletool by google.

the class ApkSetWriter method zip.

/**
 * Creates ApkSet writer which stores all splits as ZIP archive.
 */
static ApkSetWriter zip(Path tempDirectory, Path outputFile) {
    return new ApkSetWriter() {

        @Override
        public Path getSplitsDirectory() {
            return tempDirectory;
        }

        @Override
        public void writeApkSet(BuildApksResult toc) throws IOException {
            Stream<ApkDescription> apks = toc.getVariantList().stream().flatMap(variant -> variant.getApkSetList().stream()).flatMap(apkSet -> apkSet.getApkDescriptionList().stream());
            Stream<ApkDescription> assets = toc.getAssetSliceSetList().stream().flatMap(assetSliceSet -> assetSliceSet.getApkDescriptionList().stream());
            ImmutableSet<String> apkRelativePaths = Stream.concat(apks, assets).map(ApkDescription::getPath).sorted().collect(toImmutableSet());
            zipApkSet(apkRelativePaths, toc.toByteArray());
        }

        @Override
        public void writeApkSet(BuildSdkApksResult toc) throws IOException {
            Stream<ApkDescription> apks = toc.getVariantList().stream().flatMap(variant -> variant.getApkSetList().stream()).flatMap(apkSet -> apkSet.getApkDescriptionList().stream());
            ImmutableSet<String> apkRelativePaths = apks.map(ApkDescription::getPath).sorted().collect(toImmutableSet());
            zipApkSet(apkRelativePaths, toc.toByteArray());
        }

        private void zipApkSet(ImmutableSet<String> apkRelativePaths, byte[] tocBytes) throws IOException {
            try (ZipArchive zipArchive = new ZipArchive(outputFile)) {
                zipArchive.add(new BytesSource(tocBytes, TABLE_OF_CONTENTS_FILE, Deflater.NO_COMPRESSION));
                for (String relativePath : apkRelativePaths) {
                    zipArchive.add(new LargeFileSource(getSplitsDirectory().resolve(relativePath), /* tmpStorage= */
                    null, relativePath, Deflater.NO_COMPRESSION));
                }
            }
        }
    };
}
Also used : TABLE_OF_CONTENTS_FILE(com.android.tools.build.bundletool.model.utils.FileNames.TABLE_OF_CONTENTS_FILE) ImmutableSet(com.google.common.collect.ImmutableSet) Files(java.nio.file.Files) BuildApksResult(com.android.bundle.Commands.BuildApksResult) BytesSource(com.android.zipflinger.BytesSource) LargeFileSource(com.android.zipflinger.LargeFileSource) IOException(java.io.IOException) Deflater(java.util.zip.Deflater) Stream(java.util.stream.Stream) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ApkDescription(com.android.bundle.Commands.ApkDescription) ZipArchive(com.android.zipflinger.ZipArchive) BuildSdkApksResult(com.android.bundle.Commands.BuildSdkApksResult) Path(java.nio.file.Path) BuildSdkApksResult(com.android.bundle.Commands.BuildSdkApksResult) BytesSource(com.android.zipflinger.BytesSource) ApkDescription(com.android.bundle.Commands.ApkDescription) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) BuildApksResult(com.android.bundle.Commands.BuildApksResult) ZipArchive(com.android.zipflinger.ZipArchive) LargeFileSource(com.android.zipflinger.LargeFileSource)

Example 7 with BuildSdkApksResult

use of com.android.bundle.Commands.BuildSdkApksResult in project bundletool by google.

the class BuildSdkApksManagerTest method sdkManifestMutation_patchVersionNotSet_defaultPatchVersionAdded.

@Test
public void sdkManifestMutation_patchVersionNotSet_defaultPatchVersionAdded() throws Exception {
    SdkBundle sdkBundle = new SdkBundleBuilder().setModule(new BundleModuleBuilder("base").setManifest(androidManifest(PACKAGE_NAME, withSdkLibraryElement("20"))).build()).build();
    execute(sdkBundle);
    ZipFile apkSetFile = new ZipFile(outputFilePath.toFile());
    BuildSdkApksResult result = extractTocFromSdkApkSetFile(apkSetFile, tmpDir);
    Variant variant = result.getVariant(0);
    ApkDescription apkDescription = variant.getApkSet(0).getApkDescription(0);
    File apkFile = extractFromApkSetFile(apkSetFile, apkDescription.getPath(), tmpDir);
    AndroidManifest manifest = extractAndroidManifest(apkFile, tmpDir);
    assertThat(manifest.getMetadataValue(SDK_PATCH_VERSION_ATTRIBUTE_NAME)).hasValue(DEFAULT_SDK_PATCH_VERSION);
}
Also used : BuildSdkApksResult(com.android.bundle.Commands.BuildSdkApksResult) Variant(com.android.bundle.Commands.Variant) ApkDescription(com.android.bundle.Commands.ApkDescription) ZipFile(java.util.zip.ZipFile) BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder) SdkBundle(com.android.tools.build.bundletool.model.SdkBundle) AndroidManifest(com.android.tools.build.bundletool.model.AndroidManifest) TestUtils.extractAndroidManifest(com.android.tools.build.bundletool.testing.TestUtils.extractAndroidManifest) SdkBundleBuilder(com.android.tools.build.bundletool.testing.SdkBundleBuilder) ApkSetUtils.extractTocFromSdkApkSetFile(com.android.tools.build.bundletool.testing.ApkSetUtils.extractTocFromSdkApkSetFile) ZipFile(java.util.zip.ZipFile) File(java.io.File) ApkSetUtils.extractFromApkSetFile(com.android.tools.build.bundletool.testing.ApkSetUtils.extractFromApkSetFile) Test(org.junit.Test)

Example 8 with BuildSdkApksResult

use of com.android.bundle.Commands.BuildSdkApksResult in project bundletool by google.

the class BuildSdkApksManagerTest method tocIsCorrect.

@Test
public void tocIsCorrect() throws Exception {
    execute(new SdkBundleBuilder().build());
    ZipFile apkSetFile = new ZipFile(outputFilePath.toFile());
    BuildSdkApksResult result = extractTocFromSdkApkSetFile(apkSetFile, tmpDir);
    assertThat(result.getVariantCount()).isEqualTo(1);
    assertThat(result.getPackageName()).isEqualTo(PACKAGE_NAME);
    assertThat(result.getBundletool().getVersion()).isEqualTo(DEFAULT_BUNDLE_CONFIG.getBundletool().getVersion());
}
Also used : BuildSdkApksResult(com.android.bundle.Commands.BuildSdkApksResult) ZipFile(java.util.zip.ZipFile) SdkBundleBuilder(com.android.tools.build.bundletool.testing.SdkBundleBuilder) Test(org.junit.Test)

Example 9 with BuildSdkApksResult

use of com.android.bundle.Commands.BuildSdkApksResult in project bundletool by google.

the class ApkSerializerManager method serializeSdkApkSet.

/**
 * Serialize SDK Bundle APKs.
 */
public void serializeSdkApkSet(ApkSetWriter apkSetWriter, GeneratedApks generatedApks) {
    try {
        BuildSdkApksResult toc = serializeSdkApkSetContent(apkSetWriter.getSplitsDirectory(), generatedApks);
        apkSetWriter.writeApkSet(toc);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : BuildSdkApksResult(com.android.bundle.Commands.BuildSdkApksResult) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException)

Aggregations

BuildSdkApksResult (com.android.bundle.Commands.BuildSdkApksResult)9 SdkBundleBuilder (com.android.tools.build.bundletool.testing.SdkBundleBuilder)7 ZipFile (java.util.zip.ZipFile)7 Test (org.junit.Test)7 ApkDescription (com.android.bundle.Commands.ApkDescription)5 BundleModuleBuilder (com.android.tools.build.bundletool.testing.BundleModuleBuilder)5 Variant (com.android.bundle.Commands.Variant)4 SdkBundle (com.android.tools.build.bundletool.model.SdkBundle)4 ApkSetUtils.extractFromApkSetFile (com.android.tools.build.bundletool.testing.ApkSetUtils.extractFromApkSetFile)4 ApkSetUtils.extractTocFromSdkApkSetFile (com.android.tools.build.bundletool.testing.ApkSetUtils.extractTocFromSdkApkSetFile)4 File (java.io.File)4 AndroidManifest (com.android.tools.build.bundletool.model.AndroidManifest)3 TestUtils.extractAndroidManifest (com.android.tools.build.bundletool.testing.TestUtils.extractAndroidManifest)3 SdkVersionInformation (com.android.bundle.Commands.SdkVersionInformation)2 IOException (java.io.IOException)2 ApkVerifier (com.android.apksig.ApkVerifier)1 BuildApksResult (com.android.bundle.Commands.BuildApksResult)1 TABLE_OF_CONTENTS_FILE (com.android.tools.build.bundletool.model.utils.FileNames.TABLE_OF_CONTENTS_FILE)1 BytesSource (com.android.zipflinger.BytesSource)1 LargeFileSource (com.android.zipflinger.LargeFileSource)1