From 0c1fe64ce896fc8e367b405b3f99195bf81b83f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Gjesse?= Date: Thu, 12 Oct 2017 16:09:13 +0200 Subject: [PATCH] Added support for using R8 instead of Proguard + dx With this change building with USE_R8=true will run R8 instead of Proguard + dx for all targets that have LOCAL_PROGUARD_ENABLED set. R8 (through the compatproguard wrapper) will use the same options as Proguard for now. Additional Proguard configurations for R8 can be added using LOCAL_R8_FLAG_FILES on individual targets. This makes it possible to make configuration updates without touching the Proguard + dx configuration Test: m -j Test: m -j USE_D8=true USE_R8=true Test: m -j USE_D8=true USE_R8=false Test: m -j USE_D8=true Change-Id: I78e8a0fd17cc5cefedccffe95b93c94a60a727f1 --- core/clear_vars.mk | 1 + core/config.mk | 3 ++- core/definitions.mk | 13 +++++++++++++ core/java.mk | 30 ++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/core/clear_vars.mk b/core/clear_vars.mk index b5bb963b62..1e64c4f3bf 100644 --- a/core/clear_vars.mk +++ b/core/clear_vars.mk @@ -200,6 +200,7 @@ LOCAL_PROTOC_FLAGS:= # lite(default),micro,nano,stream,full,nanopb-c,nanopb-c-enable_malloc LOCAL_PROTOC_OPTIMIZE_TYPE:= LOCAL_PROTO_JAVA_OUTPUT_PARAMS:= +LOCAL_R8_FLAG_FILES:= LOCAL_RECORDED_MODULE_TYPE:= LOCAL_RENDERSCRIPT_CC:= LOCAL_RENDERSCRIPT_COMPATIBILITY:= diff --git a/core/config.mk b/core/config.mk index 61f7743c3c..ed6c4aa0e5 100644 --- a/core/config.mk +++ b/core/config.mk @@ -523,6 +523,7 @@ ifeq (,$(TARGET_BUILD_APPS)$(filter true,$(TARGET_BUILD_PDK))) SIGNAPK_JAR := $(HOST_OUT_JAVA_LIBRARIES)/signapk$(COMMON_JAVA_PACKAGE_SUFFIX) SIGNAPK_JNI_LIBRARY_PATH := $(HOST_OUT_SHARED_LIBRARIES) ZIPALIGN := $(HOST_OUT_EXECUTABLES)/zipalign + R8 := $(HOST_OUT_EXECUTABLES)/r8 else # TARGET_BUILD_APPS || TARGET_BUILD_PDK AIDL := $(prebuilt_sdk_tools_bin)/aidl @@ -533,7 +534,7 @@ else # TARGET_BUILD_APPS || TARGET_BUILD_PDK SIGNAPK_JAR := $(prebuilt_sdk_tools)/lib/signapk$(COMMON_JAVA_PACKAGE_SUFFIX) SIGNAPK_JNI_LIBRARY_PATH := $(prebuilt_sdk_tools)/$(HOST_OS)/lib64 ZIPALIGN := $(prebuilt_sdk_tools_bin)/zipalign - + R8 := $(prebuilt_build_tools_wrappers)/r8 endif # TARGET_BUILD_APPS || TARGET_BUILD_PDK ifeq (,$(TARGET_BUILD_APPS)) diff --git a/core/definitions.mk b/core/definitions.mk index 99d048b450..af4b695d04 100644 --- a/core/definitions.mk +++ b/core/definitions.mk @@ -2787,6 +2787,19 @@ $(hide) $(PROGUARD) -injars '$<$(PRIVATE_PROGUARD_INJAR_FILTERS)' \ $(addprefix -injars , $(PRIVATE_EXTRA_INPUT_JAR)) endef +########################################################### +## Commands to call R8 +########################################################### +define transform-jar-to-dex-r8 +@echo R8: $@ +$(hide) $(R8) -injars '$<$(PRIVATE_PROGUARD_INJAR_FILTERS)' \ + --min-api $(PRIVATE_MIN_SDK_VERSION) \ + --force-proguard-compatibility --output $(subst classes.dex,,$@) \ + $(PRIVATE_PROGUARD_FLAGS) \ + $(addprefix -injars , $(PRIVATE_EXTRA_INPUT_JAR)) \ + $(PRIVATE_DX_FLAGS) +endef + ########################################################### ## Stuff source generated from one-off tools ########################################################### diff --git a/core/java.mk b/core/java.mk index 7584479efa..aa6331b2dd 100644 --- a/core/java.mk +++ b/core/java.mk @@ -694,6 +694,9 @@ endif # LOCAL_INSTRUMENTATION_FOR endif # LOCAL_PROGUARD_ENABLED is not nosystem proguard_flag_files := $(addprefix $(LOCAL_PATH)/, $(LOCAL_PROGUARD_FLAG_FILES)) +ifeq ($(USE_R8),true) +proguard_flag_files += $(addprefix $(LOCAL_PATH)/, $(LOCAL_R8_FLAG_FILES)) +endif # USE_R8 LOCAL_PROGUARD_FLAGS += $(addprefix -include , $(proguard_flag_files)) ifdef LOCAL_TEST_MODULE_TO_PROGUARD_WITH @@ -718,11 +721,20 @@ endif ifneq ($(filter obfuscation,$(LOCAL_PROGUARD_ENABLED)),) $(full_classes_proguard_jar): .KATI_IMPLICIT_OUTPUTS := $(proguard_dictionary) endif + +# If R8 is not enabled run Proguard. +ifneq ($(USE_R8),true) +# Changes to these dependencies need to be replicated below when using R8 +# instead of Proguard + dx. $(full_classes_proguard_jar): PRIVATE_PROGUARD_INJAR_FILTERS := $(proguard_injar_filters) $(full_classes_proguard_jar): PRIVATE_EXTRA_INPUT_JAR := $(extra_input_jar) $(full_classes_proguard_jar): PRIVATE_PROGUARD_FLAGS := $(legacy_proguard_flags) $(common_proguard_flags) $(LOCAL_PROGUARD_FLAGS) $(full_classes_proguard_jar) : $(full_classes_pre_proguard_jar) $(extra_input_jar) $(my_support_library_sdk_raise) $(common_proguard_flag_files) $(proguard_flag_files) $(legacy_proguard_lib_deps) | $(PROGUARD) $(call transform-jar-to-proguard) +else # !USE_R8 +# Running R8 instead of Proguard, proguarded jar is actually the pre-Proguarded jar. +full_classes_proguard_jar := $(full_classes_pre_proguard_jar) +endif # !USE_R8 else # LOCAL_PROGUARD_ENABLED not defined full_classes_proguard_jar := $(full_classes_pre_proguard_jar) @@ -739,8 +751,26 @@ $(built_dex_intermediate): PRIVATE_DX_FLAGS := $(LOCAL_DX_FLAGS) ifeq ($(LOCAL_EMMA_INSTRUMENT),true) $(built_dex_intermediate): PRIVATE_DX_FLAGS += --no-locals endif + +my_r8 := +ifdef LOCAL_PROGUARD_ENABLED +ifeq ($(USE_R8),true) +# These are the dependencies for the proguarded jar when running +# Proguard + dx. They are used for the generated dex when using R8, as +# R8 does Proguard + dx +my_r8 := true +$(built_dex_intermediate): PRIVATE_PROGUARD_INJAR_FILTERS := $(proguard_injar_filters) +$(built_dex_intermediate): PRIVATE_EXTRA_INPUT_JAR := $(extra_input_jar) +$(built_dex_intermediate): PRIVATE_PROGUARD_FLAGS := $(legacy_proguard_flags) $(common_proguard_flags) $(LOCAL_PROGUARD_FLAGS) +$(built_dex_intermediate) : $(full_classes_proguard_jar) $(extra_input_jar) $(my_support_library_sdk_raise) $(common_proguard_flag_files) $(proguard_flag_files) $(legacy_proguard_lib_deps) $(R8) + $(transform-jar-to-dex-r8) +endif # USE_R8 +endif # LOCAL_PROGUARD_ENABLED + +ifndef my_r8 $(built_dex_intermediate): $(full_classes_proguard_jar) $(DX) $(transform-classes.jar-to-dex) +endif $(built_dex): $(built_dex_intermediate) @echo Copying: $@