Skip to content

Commit

Permalink
Support archived sandboxed SDKs when generating dependencies manifest.
Browse files Browse the repository at this point in the history
This changes the manifest generation command to also accept SDK archives and
reading their metadata to get the SDK package name and version.

Note that since this is meant for local deployment only, the APKs generated
from the archives are still expected to be signed with a debug key.

PiperOrigin-RevId: 567542808
Change-Id: I334a32fc002145db64210e96a71be2b6338e59fb
  • Loading branch information
lucasvtenorio authored and copybara-github committed Sep 22, 2023
1 parent cd4691d commit 5170a29
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 73 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Utilities for SDK module config proto message.
# Utilities for extracting information from SDK archives and Bundle metadata.

package(
default_applicable_licenses = ["//:license"],
Expand All @@ -8,10 +8,11 @@ package(
licenses(["notice"])

java_library(
name = "config",
name = "info",
srcs = glob(["*.java"]),
deps = [
"@rules_android_maven//:com_android_tools_build_bundletool",
"@rules_android_maven//:com_google_protobuf_protobuf_java",
"@rules_android_maven//:com_google_protobuf_protobuf_java_util",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2023 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.devtools.build.android.sandboxedsdktoolbox.info;

import java.util.Objects;

/**
* Information about a Sandboxed SDK. Used to define an SDK dependency and read from an SDK archive
* or bundle config.
*/
public final class SdkInfo {

private final String packageName;
private final long versionMajor;

SdkInfo(String packageName, long versionMajor) {
this.packageName = packageName;
this.versionMajor = versionMajor;
}

/** The SDK unique package name. */
public String getPackageName() {
return packageName;
}

/**
* The SDK versionMajor. This value is constructed from the full SDK version description and it
* represents the actual version of the SDK as used by the package manager later. The major and
* minor versions are merged and the patch version is ignored.
*/
public long getVersionMajor() {
return versionMajor;
}

@Override
public boolean equals(Object object) {
if (object instanceof SdkInfo) {
SdkInfo that = (SdkInfo) object;
return this.packageName.equals(that.packageName) && this.versionMajor == that.versionMajor;
}
return false;
}

@Override
public int hashCode() {
return Objects.hash(packageName, versionMajor);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2023 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.devtools.build.android.sandboxedsdktoolbox.info;

import com.android.bundle.SdkMetadataOuterClass.SdkMetadata;
import com.android.bundle.SdkModulesConfigOuterClass.RuntimeEnabledSdkVersion;
import com.android.bundle.SdkModulesConfigOuterClass.SdkModulesConfig;
import com.android.tools.build.bundletool.model.RuntimeEnabledSdkVersionEncoder;
import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.util.JsonFormat;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;

/** Reads SDK information SDK archives and Bundle metadata files. */
public final class SdkInfoReader {

// SDK metadata proto sits at the top level of an ASAR.
private static final String SDK_METADATA_ENTRY_PATH = "SdkMetadata.pb";

public static SdkInfo readFromSdkModuleJsonFile(Path sdkModulesConfigPath) {
SdkModulesConfig.Builder modulesConfig = SdkModulesConfig.newBuilder();
try {
JsonFormat.parser().merge(Files.newBufferedReader(sdkModulesConfigPath), modulesConfig);
return new SdkInfo(
modulesConfig.getSdkPackageName(), getVersionMajor(modulesConfig.getSdkVersion()));
} catch (IOException e) {
throw new UncheckedIOException("Failed to parse SDK Module Config.", e);
}
}

public static SdkInfo readFromSdkArchive(Path sdkArchivePath) {
URI uri = URI.create("jar:" + sdkArchivePath.toUri());
try (FileSystem zipfs = FileSystems.newFileSystem(uri, new HashMap<String, String>())) {
Path metadataInAsar = zipfs.getPath(SDK_METADATA_ENTRY_PATH);
if (!Files.exists(metadataInAsar)) {
throw new IllegalStateException(
String.format("Could not find %s in %s", SDK_METADATA_ENTRY_PATH, sdkArchivePath));
}
SdkMetadata metadata =
SdkMetadata.parseFrom(
Files.readAllBytes(metadataInAsar), ExtensionRegistry.getEmptyRegistry());
return new SdkInfo(metadata.getPackageName(), getVersionMajor(metadata.getSdkVersion()));
} catch (IOException e) {
throw new UncheckedIOException("Failed to extract SDK API descriptors.", e);
}
}

private static long getVersionMajor(RuntimeEnabledSdkVersion version) {
return RuntimeEnabledSdkVersionEncoder.encodeSdkMajorAndMinorVersion(
version.getMajor(), version.getMinor());
}

private SdkInfoReader() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
*/
package com.google.devtools.build.android.sandboxedsdktoolbox.sdkdependenciesmanifest;

import static com.google.devtools.build.android.sandboxedsdktoolbox.config.SdkModulesConfigUtils.getVersionMajor;

import com.android.bundle.SdkModulesConfigOuterClass.SdkModulesConfig;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.android.sandboxedsdktoolbox.info.SdkInfo;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -50,7 +48,7 @@ final class AndroidManifestWriter {
static void writeManifest(
String packageName,
String certificateDigest,
ImmutableSet<SdkModulesConfig> configs,
ImmutableSet<SdkInfo> infoSet,
Path outputPath) {
Document root = newEmptyDocument();

Expand All @@ -62,11 +60,11 @@ static void writeManifest(
Element applicationNode = root.createElement(APPLICATION_ELEMENT_NAME);
manifestNode.appendChild(applicationNode);

for (SdkModulesConfig config : configs) {
for (SdkInfo sdkInfo : infoSet) {
Element sdkDependencyElement = root.createElement(SDK_DEPENDENCY_ELEMENT_NAME);
sdkDependencyElement.setAttribute(ANDROID_NAME_ATTRIBUTE, config.getSdkPackageName());
sdkDependencyElement.setAttribute(ANDROID_NAME_ATTRIBUTE, sdkInfo.getPackageName());
sdkDependencyElement.setAttribute(
ANDROID_VERSION_MAJOR_ATTRIBUTE, Long.toString(getVersionMajor(config)));
ANDROID_VERSION_MAJOR_ATTRIBUTE, Long.toString(sdkInfo.getVersionMajor()));
sdkDependencyElement.setAttribute(ANDROID_CERTIFICATE_DIGEST_ATTRIBUTE, certificateDigest);
applicationNode.appendChild(sdkDependencyElement);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ java_library(
name = "sdkdependenciesmanifest",
srcs = glob(["*.java"]),
deps = [
"//src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/config",
"//src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/info",
"@rules_android_maven//:com_android_tools_build_bundletool",
"@rules_android_maven//:com_google_guava_guava",
"@rules_android_maven//:info_picocli_picocli",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,33 @@
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.devtools.build.android.sandboxedsdktoolbox.sdkdependenciesmanifest.AndroidManifestWriter.writeManifest;
import static com.google.devtools.build.android.sandboxedsdktoolbox.sdkdependenciesmanifest.CertificateDigestGenerator.generateCertificateDigest;
import static java.util.Arrays.stream;

import com.android.bundle.SdkModulesConfigOuterClass.SdkModulesConfig;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.android.sandboxedsdktoolbox.config.SdkModulesConfigUtils;
import com.google.devtools.build.android.sandboxedsdktoolbox.info.SdkInfo;
import com.google.devtools.build.android.sandboxedsdktoolbox.info.SdkInfoReader;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

/** Command for generating SDK dependencies manifest. */
@Command(
name = "generate-sdk-dependencies-manifest",
description =
"Generates an Android manifest with the <uses-sdk-library> tags from the given "
+ "SDK bundles.")
"Generates an Android manifest with <uses-sdk-library> tags from the given SDK bundles "
+ "or archives.")
public final class GenerateSdkDependenciesManifestCommand implements Runnable {

@Option(names = "--manifest-package", required = true)
String manifestPackage;

@Option(names = "--sdk-module-configs", split = ",", required = true)
Path[] sdkModuleConfigPaths;
@Option(names = "--sdk-module-configs", split = ",", required = false)
List<Path> sdkModuleConfigPaths = new ArrayList<>();

@Option(names = "--sdk-archives", split = ",", required = false)
List<Path> sdkArchivePaths = new ArrayList<>();

@Option(names = "--debug-keystore", required = true)
Path debugKeystorePath;
Expand All @@ -55,9 +60,15 @@ public final class GenerateSdkDependenciesManifestCommand implements Runnable {

@Override
public void run() {
ImmutableSet<SdkModulesConfig> configSet =
stream(sdkModuleConfigPaths)
.map(SdkModulesConfigUtils::readFromJsonFile)
if (sdkModuleConfigPaths.isEmpty() && sdkArchivePaths.isEmpty()) {
throw new IllegalArgumentException(
"At least one of --sdk-module-configs or --sdk-archives must be specified.");
}

ImmutableSet<SdkInfo> configSet =
Stream.concat(
sdkModuleConfigPaths.stream().map(SdkInfoReader::readFromSdkModuleJsonFile),
sdkArchivePaths.stream().map(SdkInfoReader::readFromSdkArchive))
.collect(toImmutableSet());

String certificateDigest =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@ java_test(
name = "GenerateSdkDependenciesManifestCommandTest",
size = "small",
srcs = ["GenerateSdkDependenciesManifestCommandTest.java"],
data = [
"testdata/com.example.firstsdkconfig.json",
"testdata/com.example.secondsdkconfig.json",
"testdata/expected_manifest_multiple_sdks.xml",
"testdata/expected_manifest_single_sdk.xml",
"testdata/test_key",
],
data = glob(["testdata/*"]),
deps = [
"//src/tools/javatests/com/google/devtools/build/android/sandboxedsdktoolbox/utils",
"@rules_android_maven//:junit_junit",
"@rules_android_maven//:com_android_tools_build_bundletool",
"@rules_android_maven//:com_google_protobuf_protobuf_java_util",
"@rules_android_maven//:com_google_truth_truth",
"@rules_android_maven//:junit_junit",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@
import static com.google.devtools.build.android.sandboxedsdktoolbox.utils.Runner.runCommand;
import static com.google.devtools.build.android.sandboxedsdktoolbox.utils.TestData.JAVATESTS_DIR;
import static com.google.devtools.build.android.sandboxedsdktoolbox.utils.TestData.readFromAbsolutePath;
import static com.google.devtools.build.android.sandboxedsdktoolbox.utils.Zip.createZipWithSingleEntry;

import com.android.bundle.SdkMetadataOuterClass.SdkMetadata;
import com.google.devtools.build.android.sandboxedsdktoolbox.utils.CommandResult;
import com.google.protobuf.util.JsonFormat;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -42,6 +47,9 @@ public final class GenerateSdkDependenciesManifestCommandTest {
TEST_DATA_DIR.resolve("com.example.firstsdkconfig.json");
private static final Path SECOND_SDK_CONFIG_JSON_PATH =
TEST_DATA_DIR.resolve("com.example.secondsdkconfig.json");
private static final Path ARCHIVE_CONFIG_JSON_PATH =
TEST_DATA_DIR.resolve("com.example.archivedsdkmetadata.json");

/*
The test key was generated with this command, its password is "android"
keytool -genkeypair \
Expand Down Expand Up @@ -109,4 +117,43 @@ public void generateManifest_forMultipleSdkModuleConfigs_success() throws Except
.isEqualTo(
readFromAbsolutePath(TEST_DATA_DIR.resolve("expected_manifest_multiple_sdks.xml")));
}

@Test
public void generateManifest_forSdksAndArchives_success() throws Exception {
String manifestPackage = "com.example.generatedmanifest";
// Create a zip with a single file containing the SdkMetadata proto message, serialized.
Path archiveConfigPath = testFolder.getRoot().toPath().resolve("sdk.asar");
createZipWithSingleEntry(archiveConfigPath, "SdkMetadata.pb", readSdkMetadata().toByteArray());
Path outputFile = testFolder.newFile().toPath();

CommandResult result =
runCommand(
"generate-sdk-dependencies-manifest",
"--manifest-package",
manifestPackage,
"--sdk-module-configs",
FIRST_SDK_CONFIG_JSON_PATH.toString(),
"--sdk-archives",
archiveConfigPath.toString(),
"--debug-keystore",
TEST_KEY_PATH.toString(),
"--debug-keystore-pass",
"android",
"--debug-keystore-alias",
"androiddebugkey",
"--output-manifest",
outputFile.toString());

assertThat(result.getStatusCode()).isEqualTo(0);
assertThat(result.getOutput()).isEmpty();
assertThat(readFromAbsolutePath(outputFile))
.isEqualTo(
readFromAbsolutePath(TEST_DATA_DIR.resolve("expected_manifest_with_archived_sdk.xml")));
}

private static SdkMetadata readSdkMetadata() throws IOException {
SdkMetadata.Builder metadata = SdkMetadata.newBuilder();
JsonFormat.parser().merge(Files.newBufferedReader(ARCHIVE_CONFIG_JSON_PATH), metadata);
return metadata.build();
}
}
Loading

0 comments on commit 5170a29

Please sign in to comment.