From b93f828133e74cb3589ba7ffcfe74d2fe72430cd Mon Sep 17 00:00:00 2001 From: Googler Date: Mon, 11 Apr 2022 14:03:56 -0700 Subject: [PATCH] Switch to using the d8 jar from maven.google.com instead of the jar from the Android SDK. Also fixes #13989 RELNOTES: Bazel uses the D8 jar from Maven instead of the SDK. PiperOrigin-RevId: 440990736 --- WORKSPACE | 20 +++++- src/BUILD | 1 + .../android/android_remote_tools.WORKSPACE | 9 ++- .../lib/analysis/mock/BazelAnalysisMock.java | 14 +++++ .../packages/BazelPackageLoaderTest.java | 3 + .../bazel/android/android_integration_test.sh | 61 +++++++++++++++++++ src/test/shell/testenv.sh.tmpl | 1 + .../android_sdk_repository_template.bzl | 10 +-- 8 files changed, 113 insertions(+), 6 deletions(-) diff --git a/WORKSPACE b/WORKSPACE index be90748f1ce8b7..bde40256ee148f 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -1,6 +1,6 @@ workspace(name = "io_bazel") -load("//tools/build_defs/repo:http.bzl", "http_archive", "http_file") +load("//tools/build_defs/repo:http.bzl", "http_archive", "http_file", "http_jar") load("//:distdir.bzl", "dist_http_archive", "dist_http_file", "distdir_tar") load("//:distdir_deps.bzl", "DIST_DEPS") @@ -119,16 +119,22 @@ distdir_tar( # Keep in sync with the archives fetched as part of building bazel. archives = [ "android_tools_pkg-0.23.0.tar.gz", + # for android_gmaven_r8 + "r8-3.3.28.jar", ], dirname = "derived/distdir", dist_deps = {dep: attrs for dep, attrs in DIST_DEPS.items() if "additional_distfiles" in attrs["used_in"]}, sha256 = { "android_tools_pkg-0.23.0.tar.gz": "ed5290594244c2eeab41f0104519bcef51e27c699ff4b379fcbd25215270513e", + "r8-3.3.28.jar": "8626ca32fb47aba7fddd2c897615e2e8ffcdb4d4b213572a2aefb3f838f01972", }, urls = { "android_tools_pkg-0.23.0.tar.gz": [ "https://mirror.bazel.build/bazel_android_tools/android_tools_pkg-0.23.0.tar.gz", ], + "r8-3.3.28.jar": [ + "https://maven.google.com/com/android/tools/r8/3.3.28/r8-3.3.28.jar", + ], }, ) @@ -346,16 +352,21 @@ distdir_tar( name = "test_WORKSPACE_files", archives = [ "android_tools_pkg-0.23.0.tar.gz", + "r8-3.3.28.jar", ], dirname = "test_WORKSPACE/distdir", dist_deps = {dep: attrs for dep, attrs in DIST_DEPS.items() if "test_WORKSPACE_files" in attrs["used_in"]}, sha256 = { "android_tools_pkg-0.23.0.tar.gz": "ed5290594244c2eeab41f0104519bcef51e27c699ff4b379fcbd25215270513e", + "r8-3.3.28.jar": "8626ca32fb47aba7fddd2c897615e2e8ffcdb4d4b213572a2aefb3f838f01972", }, urls = { "android_tools_pkg-0.23.0.tar.gz": [ "https://mirror.bazel.build/bazel_android_tools/android_tools_pkg-0.23.0.tar.gz", ], + "r8-3.3.28.jar": [ + "https://maven.google.com/com/android/tools/r8/3.3.28/r8-3.3.28.jar", + ], }, ) @@ -397,6 +408,13 @@ http_archive( url = "https://mirror.bazel.build/bazel_android_tools/android_tools_pkg-0.23.0.tar.gz", ) +# This must be kept in sync with src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_remote_tools.WORKSPACE +http_jar( + name = "android_gmaven_r8_for_testing", + sha256 = "8626ca32fb47aba7fddd2c897615e2e8ffcdb4d4b213572a2aefb3f838f01972", + url = "https://maven.google.com/com/android/tools/r8/3.3.28/r8-3.3.28.jar", +) + dist_http_archive( name = "remote_coverage_tools", patch_cmds = EXPORT_WORKSPACE_IN_BUILD_FILE, diff --git a/src/BUILD b/src/BUILD index 99835599bba582..2fa0cc5f7f7d76 100644 --- a/src/BUILD +++ b/src/BUILD @@ -646,6 +646,7 @@ release_archive( filegroup( name = "test_repos", srcs = [ + "@android_gmaven_r8_for_testing//jar:file", "@android_tools_for_testing//:WORKSPACE", "@bazel_skylib//:WORKSPACE", "@com_google_protobuf//:WORKSPACE", diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_remote_tools.WORKSPACE b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_remote_tools.WORKSPACE index 5f3972b6beaec6..c5fcf94fee340f 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_remote_tools.WORKSPACE +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_remote_tools.WORKSPACE @@ -1,4 +1,4 @@ -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_jar") # This must be kept in sync with the top-level WORKSPACE file. http_archive( @@ -6,3 +6,10 @@ http_archive( sha256 = "ed5290594244c2eeab41f0104519bcef51e27c699ff4b379fcbd25215270513e", url = "https://mirror.bazel.build/bazel_android_tools/android_tools_pkg-0.23.0.tar.gz", ) + +# This must be kept in sync with the top-level WORKSPACE file. +http_jar( + name = "android_gmaven_r8", + sha256 = "8626ca32fb47aba7fddd2c897615e2e8ffcdb4d4b213572a2aefb3f838f01972", + url = "https://maven.google.com/com/android/tools/r8/3.3.28/r8-3.3.28.jar", +) diff --git a/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java b/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java index e9df17e390de80..6b5d0d57517c62 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java +++ b/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java @@ -57,6 +57,7 @@ public ImmutableList getWorkspaceContents(MockToolsConfig config) { String bazelToolWorkspace = config.getPath("embedded_tools").getPathString(); String bazelPlatformsWorkspace = config.getPath("platforms_workspace").getPathString(); String rulesJavaWorkspace = config.getPath("rules_java_workspace").getPathString(); + String androidGmavenR8Workspace = config.getPath("android_gmaven_r8").getPathString(); String localConfigPlatformWorkspace = config.getPath("local_config_platform_workspace").getPathString(); @@ -66,6 +67,7 @@ public ImmutableList getWorkspaceContents(MockToolsConfig config) { "local_repository(name = 'local_config_xcode', path = '" + xcodeWorkspace + "')", "local_repository(name = 'com_google_protobuf', path = '" + protobufWorkspace + "')", "local_repository(name = 'rules_java', path = '" + rulesJavaWorkspace + "')", + "local_repository(name = 'android_gmaven_r8', path = '" + androidGmavenR8Workspace + "')", "register_toolchains('@rules_java//java/toolchains/runtime:all')", "register_toolchains('@rules_java//java/toolchains/javac:all')", "bind(name = 'android/sdk', actual='@bazel_tools//tools/android:sdk')", @@ -85,6 +87,7 @@ public ImmutableList getWorkspaceContents(MockToolsConfig config) { @Override public ImmutableList getWorkspaceRepos() { return ImmutableList.of( + "android_gmaven_r8", "bazel_tools", "com_google_protobuf", "local_config_platform", @@ -228,6 +231,9 @@ public void setupMockClient(MockToolsConfig config, List workspaceConten ImmutableList androidBuildContents = createAndroidBuildContents(); config.create( "embedded_tools/tools/android/BUILD", androidBuildContents.toArray(new String[0])); + config.create( + "embedded_tools/src/tools/android/java/com/google/devtools/build/android/r8/BUILD", + "java_library(name='r8')\n"); config.create( "embedded_tools/tools/android/emulator/BUILD", Iterables.toArray(createToolsAndroidEmulatorContents(), String.class)); @@ -268,6 +274,11 @@ public void setupMockClient(MockToolsConfig config, List workspaceConten " zipalign = ':empty_binary',", " tags = ['__ANDROID_RULES_MIGRATION__'],", ")"); + config.create( + "android_gmaven_r8/jar/BUILD", + "java_import(name = 'jar', jars=['r8.jar'])", + "filegroup(name = 'file', srcs=[])"); + config.create("android_gmaven_r8/WORKSPACE"); MockGenruleSupport.setup(config); @@ -500,6 +511,9 @@ public void setupMockToolsRepository(MockToolsConfig config) throws IOException " pass", "", "def http_file(**kwargs):", + " pass", + "", + "def http_jar(**kwargs):", " pass"); config.create( "embedded_tools/tools/jdk/local_java_repository.bzl", diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/packages/BazelPackageLoaderTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/packages/BazelPackageLoaderTest.java index 588f5237bbff8c..73a32952ac01da 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/packages/BazelPackageLoaderTest.java +++ b/src/test/java/com/google/devtools/build/lib/skyframe/packages/BazelPackageLoaderTest.java @@ -86,6 +86,9 @@ private static void mockEmbeddedTools(Path embeddedBinaries) throws IOException " pass", "", "def http_file(**kwargs):", + " pass", + "", + "def http_jar(**kwargs):", " pass"); FileSystemUtils.writeIsoLatin1( tools.getRelative("tools/build_defs/repo/utils.bzl"), diff --git a/src/test/shell/bazel/android/android_integration_test.sh b/src/test/shell/bazel/android/android_integration_test.sh index d7d9db06db4374..60de0cee1a9355 100755 --- a/src/test/shell/bazel/android/android_integration_test.sh +++ b/src/test/shell/bazel/android/android_integration_test.sh @@ -84,6 +84,67 @@ EOF assert_build //java/bazel/multidex:bin } +function write_hello_android_files() { + mkdir -p java/com/example/hello + mkdir -p java/com/example/hello/res/values + cat > java/com/example/hello/res/values/strings.xml <<'EOF' + + HelloWorld + Hello Main + +EOF + + cat > java/com/example/hello/AndroidManifest.xml <<'EOF' + + + + + + + + + + + + + +EOF + + cat > java/com/example/hello/MainActivity.java <<'EOF' +package com.example.hello; + +import android.app.Activity; + +public class MainActivity extends Activity { +} +EOF + +} + +function test_d8_compiles_hello_android() { + write_hello_android_files + setup_android_sdk_support + cat > java/com/example/hello/BUILD <<'EOF' +android_binary( + name = 'hello', + manifest = "AndroidManifest.xml", + srcs = ['MainActivity.java'], + resource_files = glob(["res/**"]), +) +EOF + + bazel clean + bazel build --define=android_standalone_dexing_tool=d8_compat_dx \ + //java/com/example/hello:hello || fail "build failed" +} + function test_android_tools_version() { create_new_workspace setup_android_sdk_support diff --git a/src/test/shell/testenv.sh.tmpl b/src/test/shell/testenv.sh.tmpl index e80f10a589ff5f..c78bec45b8937b 100755 --- a/src/test/shell/testenv.sh.tmpl +++ b/src/test/shell/testenv.sh.tmpl @@ -282,6 +282,7 @@ EOF repos=( "android_tools_for_testing" + "android_gmaven_r8_for_testing" "bazel_skylib" "bazel_toolchains" "com_google_protobuf" diff --git a/tools/android/android_sdk_repository_template.bzl b/tools/android/android_sdk_repository_template.bzl index 170d97ab0532eb..833b54c09eaca0 100644 --- a/tools/android/android_sdk_repository_template.bzl +++ b/tools/android/android_sdk_repository_template.bzl @@ -298,12 +298,14 @@ def create_android_sdk_rules( ) java_binary( name = "d8_compat_dx", - main_class = "com.android.tools.r8.compatdx.CompatDx", - runtime_deps = [":d8_jar_import"], + main_class = "com.google.devtools.build.android.r8.CompatDx", + runtime_deps = [ + "@bazel_tools//src/tools/android/java/com/google/devtools/build/android/r8:r8", + ], ) - java_import( + native.alias( name = "d8_jar_import", - jars = ["build-tools/%s/lib/d8.jar" % build_tools_directory], + actual = "@android_gmaven_r8//jar", ) TAGDIR_TO_TAG_MAP = {