-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This binary will be used to: - Generate manifests for APKs consuming sandboxed SDKs. - Generate client sources for communicating with sandboxed SDKs. - Extract API descriptors from SDKs - Generate XML metadata for APKs using SDKs in compatibility mode. For now, only the manifest generation command is available. This change also adds the binary to the toolchain, future rules will invoke it when consuming sandboxed SDKs. PiperOrigin-RevId: 555376579 Change-Id: Id746e1489f8f717f7e389b585b66ed2e4103c386
- Loading branch information
1 parent
df22114
commit 545a301
Showing
18 changed files
with
615 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Common tools for managing sandboxed SDKs. | ||
# Sandboxed SDKs are libraries that are released separately from Android apps and can run in the | ||
# Privacy Sandbox. | ||
|
||
package( | ||
default_applicable_licenses = ["//:license"], | ||
default_visibility = ["//src:__subpackages__"], | ||
) | ||
|
||
package_group( | ||
name = "sandboxed_sdk_toolbox_packages", | ||
packages = [ | ||
"//src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/...", | ||
], | ||
) | ||
|
||
licenses(["notice"]) | ||
|
||
java_library( | ||
name = "sandboxed_sdk_toolbox_lib", | ||
srcs = glob(["*.java"]), | ||
deps = [ | ||
"//src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest", | ||
"@rules_android_maven//:info_picocli_picocli", | ||
], | ||
) | ||
|
||
java_binary( | ||
name = "sandboxed_sdk_toolbox", | ||
main_class = "com.google.devtools.build.android.sandboxedsdktoolbox.SandboxedSdkToolbox", | ||
visibility = ["//visibility:public"], | ||
runtime_deps = [ | ||
":sandboxed_sdk_toolbox_lib", | ||
], | ||
) |
39 changes: 39 additions & 0 deletions
39
...tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/SandboxedSdkToolbox.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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; | ||
|
||
import com.google.devtools.build.android.sandboxedsdktoolbox.sdkdependenciesmanifest.GenerateSdkDependenciesManifestCommand; | ||
import picocli.CommandLine; | ||
import picocli.CommandLine.Command; | ||
|
||
/** Entrypoint for the Sandboxed SDK Toolbox binary. */ | ||
@Command( | ||
name = "sandboxed-sdk-toolbox", | ||
subcommands = { | ||
GenerateSdkDependenciesManifestCommand.class, | ||
}) | ||
public final class SandboxedSdkToolbox { | ||
|
||
public static final CommandLine create() { | ||
return new CommandLine(new SandboxedSdkToolbox()); | ||
} | ||
|
||
public static final void main(String[] args) { | ||
SandboxedSdkToolbox.create().execute(args); | ||
} | ||
|
||
private SandboxedSdkToolbox() {} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/config/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Utilities for SDK module config proto message. | ||
|
||
package( | ||
default_applicable_licenses = ["//:license"], | ||
default_visibility = ["//src:__subpackages__"], | ||
) | ||
|
||
licenses(["notice"]) | ||
|
||
java_library( | ||
name = "config", | ||
srcs = glob(["*.java"]), | ||
deps = [ | ||
"@rules_android_maven//:com_android_tools_build_bundletool", | ||
"@rules_android_maven//:com_google_protobuf_protobuf_java_util", | ||
], | ||
) |
45 changes: 45 additions & 0 deletions
45
...a/com/google/devtools/build/android/sandboxedsdktoolbox/config/SdkModulesConfigUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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) { | ||
var 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() {} | ||
} |
97 changes: 97 additions & 0 deletions
97
...ools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/AndroidManifestWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* 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.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 java.io.BufferedOutputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
import javax.xml.transform.OutputKeys; | ||
import javax.xml.transform.TransformerException; | ||
import javax.xml.transform.TransformerFactory; | ||
import javax.xml.transform.dom.DOMSource; | ||
import javax.xml.transform.stream.StreamResult; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
|
||
/** Writes an Android manifest that lists SDK dependencies for an app. */ | ||
final class AndroidManifestWriter { | ||
|
||
private static final String ANDROID_NAME_ATTRIBUTE = "android:name"; | ||
private static final String ANDROID_VERSION_MAJOR_ATTRIBUTE = "android:versionMajor"; | ||
private static final String ANDROID_CERTIFICATE_DIGEST_ATTRIBUTE = "android:certDigest"; | ||
private static final String APPLICATION_ELEMENT_NAME = "application"; | ||
private static final String MANIFEST_ELEMENT_NAME = "manifest"; | ||
private static final String MANIFEST_NAMESPACE_URI = "http://schemas.android.com/apk/res/android"; | ||
private static final String MANIFEST_NAMESPACE_NAME = "xmlns:android"; | ||
private static final String MANIFEST_PACKAGE_ATTRIBUTE = "package"; | ||
private static final String SDK_DEPENDENCY_ELEMENT_NAME = "uses-sdk-library"; | ||
|
||
static void writeManifest( | ||
String packageName, | ||
String certificateDigest, | ||
ImmutableSet<SdkModulesConfig> configs, | ||
Path outputPath) { | ||
var root = newEmptyDocument(); | ||
|
||
Element manifestNode = root.createElement(MANIFEST_ELEMENT_NAME); | ||
manifestNode.setAttribute(MANIFEST_NAMESPACE_NAME, MANIFEST_NAMESPACE_URI); | ||
manifestNode.setAttribute(MANIFEST_PACKAGE_ATTRIBUTE, packageName); | ||
root.appendChild(manifestNode); | ||
|
||
Element applicationNode = root.createElement(APPLICATION_ELEMENT_NAME); | ||
manifestNode.appendChild(applicationNode); | ||
|
||
for (SdkModulesConfig config : configs) { | ||
Element sdkDependencyElement = root.createElement(SDK_DEPENDENCY_ELEMENT_NAME); | ||
sdkDependencyElement.setAttribute(ANDROID_NAME_ATTRIBUTE, config.getSdkPackageName()); | ||
sdkDependencyElement.setAttribute( | ||
ANDROID_VERSION_MAJOR_ATTRIBUTE, Long.toString(getVersionMajor(config))); | ||
sdkDependencyElement.setAttribute(ANDROID_CERTIFICATE_DIGEST_ATTRIBUTE, certificateDigest); | ||
applicationNode.appendChild(sdkDependencyElement); | ||
} | ||
|
||
writeDocument(root, outputPath); | ||
} | ||
|
||
private static Document newEmptyDocument() { | ||
try { | ||
return DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); | ||
} catch (ParserConfigurationException e) { | ||
throw new IllegalStateException("Failed to create new XML document.", e); | ||
} | ||
} | ||
|
||
private static void writeDocument(Document document, Path outputPath) { | ||
try (var outputStream = new BufferedOutputStream(new FileOutputStream(outputPath.toFile()))) { | ||
var transformer = TransformerFactory.newInstance().newTransformer(); | ||
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); | ||
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); | ||
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); | ||
transformer.transform(new DOMSource(document), new StreamResult(outputStream)); | ||
} catch (TransformerException | IOException e) { | ||
throw new IllegalStateException("Failed to write manifest.", e); | ||
} | ||
} | ||
|
||
private AndroidManifestWriter() {} | ||
} |
19 changes: 19 additions & 0 deletions
19
.../java/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Command for generating an SDK dependencies Android manifest. | ||
|
||
package( | ||
default_applicable_licenses = ["//:license"], | ||
default_visibility = ["//src:__subpackages__"], | ||
) | ||
|
||
licenses(["notice"]) | ||
|
||
java_library( | ||
name = "sdkdependenciesmanifest", | ||
srcs = glob(["*.java"]), | ||
deps = [ | ||
"//src/tools/java/com/google/devtools/build/android/sandboxedsdktoolbox/config", | ||
"@rules_android_maven//:com_android_tools_build_bundletool", | ||
"@rules_android_maven//:com_google_guava_guava", | ||
"@rules_android_maven//:info_picocli_picocli", | ||
], | ||
) |
65 changes: 65 additions & 0 deletions
65
...build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/CertificateDigestGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* 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.sdkdependenciesmanifest; | ||
|
||
import static java.util.stream.Collectors.joining; | ||
|
||
import com.google.common.hash.Hashing; | ||
import com.google.common.io.ByteSource; | ||
import com.google.common.primitives.Bytes; | ||
import java.io.BufferedInputStream; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.security.GeneralSecurityException; | ||
import java.security.KeyStore; | ||
import java.security.cert.CertificateEncodingException; | ||
import java.security.cert.X509Certificate; | ||
|
||
/** Generates a SHA256 digest of a signing certificate. */ | ||
final class CertificateDigestGenerator { | ||
|
||
static final String generateCertificateDigest( | ||
Path keystorePath, String keystorePassword, String keystoreAlias) { | ||
var certificate = readCertificate(keystorePath, keystorePassword, keystoreAlias); | ||
return getCertificateDigest(certificate); | ||
} | ||
|
||
private static X509Certificate readCertificate( | ||
Path keystorePath, String keystorePassword, String keystoreAlias) { | ||
try (var keystoreInputStream = new BufferedInputStream(Files.newInputStream(keystorePath))) { | ||
var keystore = KeyStore.getInstance("JKS"); | ||
keystore.load(keystoreInputStream, keystorePassword.toCharArray()); | ||
return (X509Certificate) keystore.getCertificate(keystoreAlias); | ||
} catch (GeneralSecurityException | IOException e) { | ||
throw new IllegalStateException("Failed to read certificate", e); | ||
} | ||
} | ||
|
||
private static String getCertificateDigest(X509Certificate certificate) { | ||
try { | ||
return Bytes.asList( | ||
ByteSource.wrap(certificate.getEncoded()).hash(Hashing.sha256()).asBytes()) | ||
.stream() | ||
.map(b -> String.format("%02X", b)) | ||
.collect(joining(":")); | ||
} catch (CertificateEncodingException | IOException e) { | ||
throw new IllegalStateException("Failed to generate certificate digest", e); | ||
} | ||
} | ||
|
||
private CertificateDigestGenerator() {} | ||
} |
Oops, something went wrong.