Skip to content

Commit

Permalink
Switch to using the d8 jar from maven.google.com instead of the jar f…
Browse files Browse the repository at this point in the history
…rom the

Android SDK.

Also fixes #13989

RELNOTES: Bazel uses the D8 jar from Maven instead of the SDK.
PiperOrigin-RevId: 440990736
  • Loading branch information
Googler authored and copybara-github committed Apr 11, 2022
1 parent 6409b28 commit b93f828
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 6 deletions.
20 changes: 19 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
@@ -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")

Expand Down Expand Up @@ -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",
],
},
)

Expand Down Expand Up @@ -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",
],
},
)

Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
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(
name = "android_tools",
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",
)
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public ImmutableList<String> 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();

Expand All @@ -66,6 +67,7 @@ public ImmutableList<String> 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')",
Expand All @@ -85,6 +87,7 @@ public ImmutableList<String> getWorkspaceContents(MockToolsConfig config) {
@Override
public ImmutableList<String> getWorkspaceRepos() {
return ImmutableList.of(
"android_gmaven_r8",
"bazel_tools",
"com_google_protobuf",
"local_config_platform",
Expand Down Expand Up @@ -228,6 +231,9 @@ public void setupMockClient(MockToolsConfig config, List<String> workspaceConten
ImmutableList<String> 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));
Expand Down Expand Up @@ -268,6 +274,11 @@ public void setupMockClient(MockToolsConfig config, List<String> 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);

Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
61 changes: 61 additions & 0 deletions src/test/shell/bazel/android/android_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
<resources>
<string name="app_name">HelloWorld</string>
<string name="title_activity_main">Hello Main</string>
</resources>
EOF

cat > java/com/example/hello/AndroidManifest.xml <<'EOF'
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hello"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="18" />
<application android:label="@string/app_name">
<activity
android:name="com.example.hello.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
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
Expand Down
1 change: 1 addition & 0 deletions src/test/shell/testenv.sh.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ EOF

repos=(
"android_tools_for_testing"
"android_gmaven_r8_for_testing"
"bazel_skylib"
"bazel_toolchains"
"com_google_protobuf"
Expand Down
10 changes: 6 additions & 4 deletions tools/android/android_sdk_repository_template.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit b93f828

Please sign in to comment.