aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/android
diff options
context:
space:
mode:
authorGravatar kmb <kmb@google.com>2018-05-16 08:51:43 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-05-16 08:53:19 -0700
commitcd76c1f8a9a645f0ecc2a95a66548b7178b35155 (patch)
treea2a82ab68616ec7ec5db0bd7292f536b7d33ae39 /tools/android
parent35a78c09cf2fbfc3de9c124d2142e3d72aac4348 (diff)
Enable proguarded Android builds with --experimental_desugar_java8_libs in Bazel
RELNOTES: Bazel supports including select Java 8 APIs into Android apps targeting pre-Nougat Android devices with --experimental_desugar_java8_libs PiperOrigin-RevId: 196833987
Diffstat (limited to 'tools/android')
-rw-r--r--tools/android/BUILD.tools13
-rw-r--r--tools/android/build_java8_legacy_dex.sh69
-rw-r--r--tools/android/minify_java8_legacy_libs.cfg45
3 files changed, 120 insertions, 7 deletions
diff --git a/tools/android/BUILD.tools b/tools/android/BUILD.tools
index 592e6dc966..4828323ad7 100644
--- a/tools/android/BUILD.tools
+++ b/tools/android/BUILD.tools
@@ -149,16 +149,21 @@ genrule(
sh_binary(
name = "build_java8_legacy_dex",
srcs = ["build_java8_legacy_dex.sh"],
- data = [":java8_legacy_dex"],
+ data = [
+ "minify_java8_legacy_libs.cfg",
+ ":desugared_java8_legacy_libs",
+ ":dexer",
+ "@bazel_tools//src/tools/android/java/com/google/devtools/build/android/desugar/scan:KeepScanner",
+ "@bazel_tools//third_party/java/proguard",
+ ],
visibility = ["//visibility:public"],
)
genrule(
name = "java8_legacy_dex",
- srcs = ["desugared_java8_legacy_libs"],
outs = ["java8_legacy.dex.zip"],
- cmd = """$(location :dexer) --dex "--output=$@" $<""",
- tools = [":dexer"],
+ cmd = """$(location :build_java8_legacy_dex) --output $@""",
+ tools = [":build_java8_legacy_dex"],
visibility = ["//visibility:public"],
)
diff --git a/tools/android/build_java8_legacy_dex.sh b/tools/android/build_java8_legacy_dex.sh
index 7ceaea8a4c..beb2e1d508 100644
--- a/tools/android/build_java8_legacy_dex.sh
+++ b/tools/android/build_java8_legacy_dex.sh
@@ -5,7 +5,7 @@
# 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
+# 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,
@@ -14,6 +14,45 @@
set -eu
+RUNFILES="${RUNFILES:-$0.runfiles}"
+RUNFILES_MANIFEST_FILE="${RUNFILES_MANIFEST_FILE:-$RUNFILES/MANIFEST}"
+
+IS_WINDOWS=false
+case "$(uname | tr [:upper:] [:lower:])" in
+msys*|mingw*|cygwin*)
+ IS_WINDOWS=true
+esac
+
+if "$IS_WINDOWS" && ! type rlocation &> /dev/null; then
+ function rlocation() {
+ # Use 'sed' instead of 'awk', so if the absolute path ($2) has spaces, it
+ # will be printed completely.
+ local result="$(grep "$1" "${RUNFILES_MANIFEST_FILE}" | head -1)"
+ # If the entry has a space, it is a mapping from a runfiles-path to absolute
+ # path, otherwise it resolves to itself.
+ echo "$result" | grep -q " " \
+ && echo "$result" | sed 's/^[^ ]* //' \
+ || echo "$result"
+ }
+fi
+
+# Find helper artifacts:
+# Windows (in MANIFEST): <repository_name>/<path/to>/file
+# Linux/MacOS (symlink): ${RUNFILES}/<repository_name>/<path/to>/file
+if "$IS_WINDOWS"; then
+ INPUT="$(rlocation "[^/]*/tools/android/desugared_java8_legacy_libs.jar")"
+ CONFIG="$(rlocation "[^/]*/tools/android/minify_java8_legacy_libs.cfg")"
+ SCAN="$(rlocation "[^/]*/src/tools/android/java/com/google/devtools/build/android/desugar/scan/KeepScanner")"
+ PG="$(rlocation "[^/]*/third_party/java/proguard/proguard")"
+ DEXER="$(rlocation "[^/]*/tools/android/dexer")"
+else
+ INPUT="$(find "${RUNFILES}" -path "*/tools/android/desugared_java8_legacy_libs.jar" | head -1)"
+ CONFIG="$(find "${RUNFILES}" -path "*/tools/android/minify_java8_legacy_libs.cfg" | head -1)"
+ SCAN="$(find "${RUNFILES}" -path "*/src/tools/android/java/com/google/devtools/build/android/desugar/scan/KeepScanner" | head -1)"
+ PG="$(find "${RUNFILES}" -path "*/third_party/java/proguard/proguard" | head -1)"
+ DEXER="$(find "${RUNFILES}" -path "*/tools/android/dexer" | head -1)"
+fi
+
android_jar=
binary_jar=
dest=
@@ -30,5 +69,29 @@ while [[ "$#" -gt 0 ]]; do
esac
done
-# TODO(b/77339644): Support minification
-cp "$(dirname "$0")/java8_legacy.dex.zip" "${dest}"
+todex="${INPUT}"
+if [[ -n "${binary_jar}" ]]; then
+ tmpdir=$(mktemp -d)
+ trap "rm -rf ${tmpdir}" EXIT
+
+ # Minification requested
+ # 1. compute -keep rules from binary
+ seeds="${tmpdir}/seeds.cfg"
+ "${SCAN}" \
+ --input "${binary_jar}" \
+ --classpath_entry "${todex}" \
+ --bootclasspath_entry "${android_jar}" \
+ --keep_file "${seeds}"
+
+ # 2. proguard with -keep rules generated above and standard config file.
+ # Use app's android.jar as -libraryjar.
+ todex="${tmpdir}/proguarded.jar"
+ "${PG}" \
+ -injars "${INPUT}" \
+ -outjars "${todex}" \
+ -libraryjars "${android_jar}" \
+ "@${CONFIG}" \
+ "@${seeds}"
+fi
+# Convert .jar file to .dex
+"${DEXER}" --dex "--output=${dest}" "${todex}"
diff --git a/tools/android/minify_java8_legacy_libs.cfg b/tools/android/minify_java8_legacy_libs.cfg
new file mode 100644
index 0000000000..74aa802d77
--- /dev/null
+++ b/tools/android/minify_java8_legacy_libs.cfg
@@ -0,0 +1,45 @@
+# 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
+
+# Proguard configuration for :build_java8_legacy_dex
+
+# Shrink only
+-dontoptimize
+-dontobfuscate
+-dontpreverify
+
+-dontskipnonpubliclibraryclasses
+
+-keepattributes SourceFile,LineNumberTable,*Annotation*
+
+# When keeping enums, keep all their values.
+-keepclassmembers enum * {
+ public static **[] values();
+}
+
+# Keep Serializable members magically invoked by the runtime.
+-keepclassmembers class * implements java.io.Serializable {
+ static final long serialVersionUID;
+ private static final java.io.ObjectStreamField[] serialPersistentFields;
+ private void writeObject(java.io.ObjectOutputStream);
+ private void readObject(java.io.ObjectInputStream);
+ java.lang.Object writeReplace();
+ java.lang.Object readResolve();
+}
+
+# Keep Externalizable members magically invoked by the runtime.
+-keepclassmembers class * implements java.io.Externalizable {
+ <init>(); # No-arg constructor required per spec
+ public void readExternal(java.io.ObjectInput);
+ public void writeExternal(java.io.ObjectOutput);
+}