diff options
Diffstat (limited to 'src/tools/java/com/google')
-rw-r--r-- | src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/config/SdkModulesConfigUtils.java | 45 | ||||
-rw-r--r-- | src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/info/BUILD (renamed from src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/config/BUILD) | 5 | ||||
-rw-r--r-- | src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/info/SdkInfo.java | 61 | ||||
-rw-r--r-- | src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/info/SdkInfoReader.java | 73 | ||||
-rw-r--r-- | src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/AndroidManifestWriter.java | 12 | ||||
-rw-r--r-- | src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/BUILD | 2 | ||||
-rw-r--r-- | src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/GenerateSdkDependenciesManifestCommand.java | 31 |
7 files changed, 164 insertions, 65 deletions
diff --git a/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/config/SdkModulesConfigUtils.java b/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/config/SdkModulesConfigUtils.java deleted file mode 100644 index e023096..0000000 --- a/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/config/SdkModulesConfigUtils.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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.config; - -import com.android.bundle.SdkModulesConfigOuterClass.SdkModulesConfig; -import com.android.tools.build.bundletool.model.RuntimeEnabledSdkVersionEncoder; -import com.google.protobuf.util.JsonFormat; -import java.io.IOException; -import java.io.UncheckedIOException; -import java.nio.file.Files; -import java.nio.file.Path; - -/** Utilities for creating and extracting information from {@link SdkModulesConfig} messages. */ -public final class SdkModulesConfigUtils { - - public static SdkModulesConfig readFromJsonFile(Path configPath) { - SdkModulesConfig.Builder builder = SdkModulesConfig.newBuilder(); - try { - JsonFormat.parser().merge(Files.newBufferedReader(configPath), builder); - return builder.build(); - } catch (IOException e) { - throw new UncheckedIOException("Failed to parse SDK Module Config.", e); - } - } - - public static long getVersionMajor(SdkModulesConfig config) { - return RuntimeEnabledSdkVersionEncoder.encodeSdkMajorAndMinorVersion( - config.getSdkVersion().getMajor(), config.getSdkVersion().getMinor()); - } - - private SdkModulesConfigUtils() {} -} diff --git a/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/config/BUILD b/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/info/BUILD index cbc3f38..4f8e336 100644 --- a/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/config/BUILD +++ b/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/info/BUILD @@ -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"], @@ -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", ], ) diff --git a/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/info/SdkInfo.java b/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/info/SdkInfo.java new file mode 100644 index 0000000..867e55d --- /dev/null +++ b/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/info/SdkInfo.java @@ -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); + } +} diff --git a/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/info/SdkInfoReader.java b/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/info/SdkInfoReader.java new file mode 100644 index 0000000..7efef66 --- /dev/null +++ b/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/info/SdkInfoReader.java @@ -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() {} +} diff --git a/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/AndroidManifestWriter.java b/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/AndroidManifestWriter.java index f840c5f..67ce730 100644 --- a/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/AndroidManifestWriter.java +++ b/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/AndroidManifestWriter.java @@ -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; @@ -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(); @@ -62,11 +60,11 @@ final class AndroidManifestWriter { 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); } diff --git a/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/BUILD b/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/BUILD index b2584b2..12a7a1b 100644 --- a/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/BUILD +++ b/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/BUILD @@ -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", diff --git a/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/GenerateSdkDependenciesManifestCommand.java b/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/GenerateSdkDependenciesManifestCommand.java index c390f27..354f8fe 100644 --- a/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/GenerateSdkDependenciesManifestCommand.java +++ b/src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/GenerateSdkDependenciesManifestCommand.java @@ -18,12 +18,14 @@ package com.google.devtools.build.android.sandboxedsdktoolbox.sdkdependenciesman 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; @@ -31,15 +33,18 @@ import picocli.CommandLine.Option; @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; @@ -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 = |