diff options
Diffstat (limited to 'src')
15 files changed, 555 insertions, 73 deletions
diff --git a/src/tools/ak/rjar/rjar.go b/src/tools/ak/rjar/rjar.go index c95dea8..fca8c76 100644 --- a/src/tools/ak/rjar/rjar.go +++ b/src/tools/ak/rjar/rjar.go @@ -240,6 +240,11 @@ func compileRJar(srcs []string, rjar, jdk, jartool string, targetLabel string) e "--output", rjar, }...) if len(targetLabel) > 0 { + // Deal with "@//"-prefixed labels (in Bazel) + if strings.HasPrefix(targetLabel, "@//") { + targetLabel = strings.Replace(targetLabel, "@//", "//", 1) + } + args = append(args, []string{ "--target_label", targetLabel, }...) 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 = diff --git a/src/tools/javatests/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/BUILD b/src/tools/javatests/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/BUILD index c2b38d1..84d1663 100644 --- a/src/tools/javatests/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/BUILD +++ b/src/tools/javatests/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/BUILD @@ -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", ], ) diff --git a/src/tools/javatests/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/GenerateSdkDependenciesManifestCommandTest.java b/src/tools/javatests/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/GenerateSdkDependenciesManifestCommandTest.java index 7dc04e7..749320e 100644 --- a/src/tools/javatests/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/GenerateSdkDependenciesManifestCommandTest.java +++ b/src/tools/javatests/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/GenerateSdkDependenciesManifestCommandTest.java @@ -19,8 +19,13 @@ import static com.google.common.truth.Truth.assertThat; 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; @@ -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 \ @@ -109,4 +117,43 @@ public final class GenerateSdkDependenciesManifestCommandTest { .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(); + } } diff --git a/src/tools/javatests/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/testdata/com.example.archivedsdkmetadata.json b/src/tools/javatests/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/testdata/com.example.archivedsdkmetadata.json new file mode 100644 index 0000000..d3be443 --- /dev/null +++ b/src/tools/javatests/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/testdata/com.example.archivedsdkmetadata.json @@ -0,0 +1,11 @@ +{ + "package_name": "com.example.archivedsdk", + "sdk_version": { + "major": 33, + "minor": 2, + "patch": 1 + }, + + // We always use the debug key digest instead of the one provided in the ASAR. + "certificate_digest": "fake digest that should be ignored." +}
\ No newline at end of file diff --git a/src/tools/javatests/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/testdata/expected_manifest_with_archived_sdk.xml b/src/tools/javatests/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/testdata/expected_manifest_with_archived_sdk.xml new file mode 100644 index 0000000..e3bab8f --- /dev/null +++ b/src/tools/javatests/com/google/devtools/build/android/sandboxedsdktoolbox/sdkdependenciesmanifest/testdata/expected_manifest_with_archived_sdk.xml @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="utf-8" standalone="no"?> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.generatedmanifest"> + <application> + <uses-sdk-library android:certDigest="91:8E:A3:7D:7D:D0:E0:A0:14:9F:21:28:83:95:8A:F0:80:E6:F9:7B:4D:5A:39:01:76:02:E8:2D:7D:FF:A9:10" android:name="com.example.firstsdkconfig" android:versionMajor="20003"/> + <uses-sdk-library android:certDigest="91:8E:A3:7D:7D:D0:E0:A0:14:9F:21:28:83:95:8A:F0:80:E6:F9:7B:4D:5A:39:01:76:02:E8:2D:7D:FF:A9:10" android:name="com.example.archivedsdk" android:versionMajor="330002"/> + </application> +</manifest>
\ No newline at end of file diff --git a/src/tools/mi/deployment_oss/BUILD b/src/tools/mi/deployment_oss/BUILD new file mode 100644 index 0000000..b078ff2 --- /dev/null +++ b/src/tools/mi/deployment_oss/BUILD @@ -0,0 +1,36 @@ +load("@bazel_skylib//rules:build_test.bzl", "build_test") +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") + +package( + default_applicable_licenses = ["//:license"], + default_visibility = ["//src/tools/mi/deployment_oss:__pkg__"], +) + +go_binary( + name = "deploy_binary", + srcs = ["deploy_binary.go"], + visibility = ["//visibility:public"], + deps = [ + "//src/common/golang:flagfile", + "//src/common/golang:flags", + "//src/common/golang:pprint", + "//src/tools/mi/deployment_oss:deployment", + "@com_github_golang_glog//:glog", + ], +) + +go_library( + name = "deployment", + importpath = "src/tools/mi/deployment_oss/deployment", + + srcs = [ + "deploy.go", + ], + visibility = ["//src/tools/mi:__subpackages__"], + deps = ["//src/common/golang:pprint"], +) + +build_test( + name = "deploy_binary_build_test", + targets = [":deploy_binary"] +)
\ No newline at end of file diff --git a/src/tools/mi/deployment_oss/deploy.go b/src/tools/mi/deployment_oss/deploy.go new file mode 100644 index 0000000..1446e58 --- /dev/null +++ b/src/tools/mi/deployment_oss/deploy.go @@ -0,0 +1,64 @@ +// Copyright 2018 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 deployment has utilities to sync mobile-install build outputs with a device. +package deployment + +import ( + "context" + "fmt" + "os" + "os/exec" + "strconv" + + "src/common/golang/pprint" +) + +// AndroidStudioSync calls to the Studio deployer with splits. +func AndroidStudioSync(ctx context.Context, deviceSerial, port, pkg string, splits []string, deployer, adbPath, jdk string, optimisticInstall bool, studioVerboseLog bool, userID int, useADBRoot bool) error { + args := []string{"-jar", deployer, "install", pkg} + if deviceSerial != "" { + args = append(args, fmt.Sprintf("--device=%s", deviceSerial)) + } + args = append(args, "--skip-post-install", "--no-jdwp-client-support") + if optimisticInstall { + args = append(args, "--optimistic-install") + } + if useADBRoot { + args = append(args, "--use-root-push-install") + } + if studioVerboseLog { + args = append(args, "--log-level=VERBOSE") + } + if adbPath != "" { + args = append(args, fmt.Sprintf("--adb=%s", adbPath)) + } + if userID != 0 { + args = append(args, fmt.Sprintf("--user=%s", strconv.Itoa(userID))) + } + args = append(args, splits...) + cmd := exec.Command(jdk, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if port != "" { + cmd.Env = append(os.Environ(), fmt.Sprintf("ANDROID_ADB_SERVER_PORT=%s", port)) + } + if studioVerboseLog { + pprint.Info("device: %s", deviceSerial) + pprint.Info("port: %s", port) + pprint.Info("Env: %s", cmd.Env) + pprint.Info("Cmd: %s", cmd.String()) + } + return cmd.Run() +} diff --git a/src/tools/mi/deployment_oss/deploy_binary.go b/src/tools/mi/deployment_oss/deploy_binary.go new file mode 100644 index 0000000..503c81a --- /dev/null +++ b/src/tools/mi/deployment_oss/deploy_binary.go @@ -0,0 +1,217 @@ +// Copyright 2018 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. + +// The deploy_binary command unpacks a workspace and deploys it to a device. +package main + +import ( + "context" + "flag" + "io/ioutil" + "log" + "os" + "os/exec" + "strings" + "time" + + glog "github.com/golang/glog" + + _ "src/common/golang/flagfile" + "src/common/golang/flags" + "src/common/golang/pprint" + "src/tools/mi/deployment_oss/deployment" +) + +var ( + adbArgs = flags.NewStringList("adb_arg", "Options for the adb binary.") + adbPath = flag.String("adb", "/usr/bin/adb", "Path to the adb binary to use with mobile-install.") + device = flag.String("device", "", "The adb device serial number.") + javaHome = flag.String("java_home", "", "Path to JDK.") + launchActivity = flag.String("launch_activity", "", "Activity to launch via am start -n package/.activity_to_launch.") + appPackagePath = flag.String("manifest_package_name_path", "", "Path to file containing the manifest package name.") + splits = flags.NewStringList("splits", "The list of split apk paths.") + start = flag.String("start", "", "start_type from mobile-install.") + startType = flag.String("start_type", "", "start_type (deprecated, use --start).") + useADBRoot = flag.Bool("use_adb_root", true, "whether (true) or not (false) to use root permissions.") + userID = flag.Int("user", 0, "User id to install the app for.") + + // Studio deployer args + studioDeployerPath = flag.String("studio_deployer", "", "Path to the Android Studio deployer.") + optimisticInstall = flag.Bool("optimistic-install", false, "If true, try to push changes to the device without installing.") + studioVerboseLog = flag.Bool("studio-verbose-log", false, "If true, enable verbose logging for the Android Studio deployer") + + // Need to double up on launch_app due as the built-in flag module does not support noXX for bool flags. + // Some users are using --nolaunch_app, so we need to explicitly declare this flag + launchApp = flag.Bool("launch_app", true, "Launch the app after the sync is done.") + noLaunchApp = flag.Bool("nolaunch_app", false, "Don't launch the app after the sync is done.") + noDeploy = flag.Bool("nodeploy", false, "Don't deploy or launch the app, useful for testing.") + + // Unused flags: Relevant only for Google-internal use cases, but need to exist in the flag parser + buildID = flag.String("build_id", "", "The id of the build. Set by Bazel, the user should not use this flag.") +) + +func resolveDeviceSerialAndPort(ctx context.Context, device string) (deviceSerialFlag, port string) { + switch { + case strings.Contains(device, ":tcp:"): + parts := strings.SplitN(device, ":tcp:", 2) + deviceSerialFlag = parts[0] + port = parts[1] + case strings.HasPrefix(device, "tcp:"): + port = strings.TrimPrefix(device, "tcp:") + default: + deviceSerialFlag = device + } + return deviceSerialFlag, port +} + +func determineDeviceSerial(deviceSerialFlag, deviceSerialEnv, deviceSerialADBArg string) string { + var deviceSerial string + switch { + case deviceSerialFlag != "": + deviceSerial = deviceSerialFlag + case deviceSerialEnv != "": + deviceSerial = deviceSerialEnv + case deviceSerialADBArg != "": + deviceSerial = deviceSerialADBArg + } + return deviceSerial +} + +// ReadFile reads file from a given path +func readFile(path string) []byte { + data, err := ioutil.ReadFile(path) + if err != nil { + log.Fatalf("Error reading file %q: %v", path, err) + } + return data +} + +func parseRepeatedFlag(n string, a *flags.StringList) { + var l []string + for _, f := range os.Args { + if strings.HasPrefix(f, n) { + l = append(l, strings.TrimPrefix(f, n)) + } + } + if len(l) > 1 { + *a = l + } +} + +// Flush all the metrics to Streamz before the program exits. +func flushAndExitf(ctx context.Context, unused1, unused2, unused3, unused4, format string, args ...any) { + glog.Exitf(format, args...) +} + +func main() { + ctx := context.Background() + + flag.Parse() + + pprint.Info("Deploying using OSS mobile-install!") + + if *noDeploy { + pprint.Warning("--nodeploy set, not deploying application.") + return + } + + // Override --launch_app if --nolaunch_app is passed + if *noLaunchApp { + *launchApp = false + } + + if *appPackagePath == "" { + glog.Exitf("--manifest_package_name is required") + } + + // Resolve the device serial and port. + var deviceSerialFlag, port string + if *device != "" { + deviceSerialFlag, port = resolveDeviceSerialAndPort(ctx, *device) + } + deviceSerialEnv := os.Getenv("ANDROID_SERIAL") + + // TODO(b/66490815): Remove once adb_arg flag is deprecated. + // Check for a device serial in adb_arg. If deviceSerial has not been specified, the value + // found here will become the deviceSerial. If the deviceSerial has been specified the value + // found here will be ignored but we will notify the user the device chosen. + var deviceSerialADBArg string + for i, arg := range *adbArgs { + if strings.TrimSpace(arg) == "-s" && len(*adbArgs) > i+1 { + deviceSerialADBArg = (*adbArgs)[i+1] + } + } + + // TODO(timpeut): Delete after the next blaze release + // Ignore the device passed by --adb_arg if it matches the device passed by --device. + if deviceSerialADBArg == *device { + deviceSerialADBArg = "" + } + + // Determine which value to use as the deviceSerial. + deviceSerial := determineDeviceSerial(deviceSerialFlag, deviceSerialEnv, deviceSerialADBArg) + + // Warn user of the multiple device serial specification, that is not equal to the first. + if (deviceSerialEnv != "" && deviceSerialEnv != deviceSerial) || + (deviceSerialADBArg != "" && deviceSerialADBArg != deviceSerial) { + pprint.Warning("A device serial was specified more than once with --device, $ANDROID_SERIAL or --adb_arg, using %s.", deviceSerial) + } + + appPackage := strings.TrimSpace(string(readFile(*appPackagePath))) + + startTime := time.Now() + + pprint.Info("Installing application using the Android Studio deployer ...") + if err := deployment.AndroidStudioSync(ctx, deviceSerial, port, appPackage, *splits, *studioDeployerPath, *adbPath, *javaHome, *optimisticInstall, *studioVerboseLog, *userID, *useADBRoot); err != nil { + flushAndExitf(ctx, "", "", "", "", "Got error installing using the Android Studio deployer: %v", err) + } + + deploymentTime := time.Since(startTime) + pprint.Info("Took %.2f seconds to sync changes", deploymentTime.Seconds()) + + if *startType != "" { + *start = *startType + } + + // Wait for the debugger if debug mode selected + if *start == "DEBUG" { + waitCmd := exec.Command(*adbPath, "shell", "am", "set-debug-app", "-w", appPackage) + if err := waitCmd.Wait(); err != nil { + pprint.Error("Unable to wait for debugger: %s", err.Error()) + } + } + + if *launchApp { + pprint.Info("Finished deploying changes. Launching app") + var launchCmd *exec.Cmd + if *launchActivity != "" { + launchCmd = exec.Command(*adbPath, "shell", "am", "start", "-a", "android.intent.action.MAIN", "-n", appPackage+"/"+*launchActivity) + } else { + launchCmd = exec.Command(*adbPath, "shell", "monkey", "-p", appPackage, "1") + pprint.Warning( + "No or multiple main activities found, falling back to Monkey launcher. Specify the activity you want with `-- --launch_activity` or `-- --nolaunch_app` to launch nothing.") + } + + if err := launchCmd.Run(); err != nil { + pprint.Warning("Unable to launch app. Specify an activity with --launch_activity.") + pprint.Warning("Original error: %s", err.Error()) + } + } else { + // Always stop the app since classloader needs to be reloaded. + stopCmd := exec.Command(*adbPath, "shell", "am", "force-stop", appPackage) + if err := stopCmd.Run(); err != nil { + pprint.Error("Unable to stop app: %s", err.Error()) + } + } +} |