diff --git a/core/config.mk b/core/config.mk index 0920b56088..6c062a6ee2 100644 --- a/core/config.mk +++ b/core/config.mk @@ -570,7 +570,9 @@ USE_PREBUILT_SDK_TOOLS_IN_PLACE := true # Work around for b/68406220 # This should match the soong version. -USE_D8 := true +ifndef USE_D8 + USE_D8 := true +endif # Default R8 behavior when USE_R8 is not specified. ifndef USE_R8 @@ -584,6 +586,7 @@ ifeq (,$(TARGET_BUILD_APPS)$(filter true,$(TARGET_BUILD_PDK))) AIDL := $(HOST_OUT_EXECUTABLES)/aidl AAPT := $(HOST_OUT_EXECUTABLES)/aapt AAPT2 := $(HOST_OUT_EXECUTABLES)/aapt2 + DESUGAR := $(HOST_OUT_JAVA_LIBRARIES)/desugar.jar MAINDEXCLASSES := $(HOST_OUT_EXECUTABLES)/mainDexClasses SIGNAPK_JAR := $(HOST_OUT_JAVA_LIBRARIES)/signapk$(COMMON_JAVA_PACKAGE_SUFFIX) SIGNAPK_JNI_LIBRARY_PATH := $(HOST_OUT_SHARED_LIBRARIES) @@ -593,6 +596,7 @@ else # TARGET_BUILD_APPS || TARGET_BUILD_PDK AIDL := $(prebuilt_build_tools_bin)/aidl AAPT := $(prebuilt_sdk_tools_bin)/aapt AAPT2 := $(prebuilt_sdk_tools_bin)/aapt2 + DESUGAR := $(prebuilt_build_tools_jars)/desugar.jar MAINDEXCLASSES := $(prebuilt_sdk_tools)/mainDexClasses SIGNAPK_JAR := $(prebuilt_sdk_tools)/lib/signapk$(COMMON_JAVA_PACKAGE_SUFFIX) SIGNAPK_JNI_LIBRARY_PATH := $(prebuilt_sdk_tools)/$(HOST_OS)/lib64 diff --git a/core/definitions.mk b/core/definitions.mk index b1a1f8f886..e18f9a4ba8 100644 --- a/core/definitions.mk +++ b/core/definitions.mk @@ -2374,10 +2374,48 @@ define codename-or-sdk-to-sdk $(if $(filter $(1),$(PLATFORM_VERSION_CODENAME)),10000,$(1)) endef +# --add-opens is required because desugar reflects via java.lang.invoke.MethodHandles.Lookup +define desugar-classes-jar +@echo Desugar: $@ +@mkdir -p $(dir $@) +$(hide) rm -f $@ $@.tmp +@rm -rf $(dir $@)/desugar_dumped_classes +@mkdir $(dir $@)/desugar_dumped_classes +$(hide) $(JAVA) \ + $(if $(USE_OPENJDK9),--add-opens java.base/java.lang.invoke=ALL-UNNAMED,) \ + -Djdk.internal.lambda.dumpProxyClasses=$(abspath $(dir $@))/desugar_dumped_classes \ + -jar $(DESUGAR) \ + $(addprefix --bootclasspath_entry ,$(PRIVATE_BOOTCLASSPATH)) \ + $(addprefix --classpath_entry ,$(PRIVATE_SHARED_JAVA_HEADER_LIBRARIES)) \ + --min_sdk_version $(call codename-or-sdk-to-sdk,$(PRIVATE_MIN_SDK_VERSION)) \ + --allow_empty_bootclasspath \ + $(if $(filter --core-library,$(PRIVATE_DX_FLAGS)),--core_library) \ + -i $< -o $@.tmp + mv $@.tmp $@ +endef + define transform-classes.jar-to-dex @echo "target Dex: $(PRIVATE_MODULE)" @mkdir -p $(dir $@) +$(hide) rm -f $(dir $@)classes*.dex +$(hide) $(DX_COMMAND) \ + --dex --output=$(dir $@) \ + --min-sdk-version=$(PRIVATE_MIN_SDK_VERSION) \ + $(if $(NO_OPTIMIZE_DX), \ + --no-optimize) \ + $(if $(GENERATE_DEX_DEBUG), \ + --debug --verbose \ + --dump-to=$(@:.dex=.lst) \ + --dump-width=1000) \ + $(PRIVATE_DX_FLAGS) \ + $< +endef + + +define transform-classes-d8.jar-to-dex +@echo "target Dex: $(PRIVATE_MODULE)" +@mkdir -p $(dir $@) $(hide) rm -f $(dir $@)classes*.dex $(dir $@)d8_input.jar $(hide) $(ZIP2ZIP) -j -i $< -o $(dir $@)d8_input.jar "**/*.class" $(hide) $(DX_COMMAND) \ diff --git a/core/host_dalvik_java_library.mk b/core/host_dalvik_java_library.mk index d35f39da97..1ef0ccb17e 100644 --- a/core/host_dalvik_java_library.mk +++ b/core/host_dalvik_java_library.mk @@ -33,6 +33,7 @@ full_classes_header_jarjar := $(intermediates.COMMON)/classes-header-jarjar.jar full_classes_header_jar := $(intermediates.COMMON)/classes-header.jar full_classes_compiled_jar := $(intermediates.COMMON)/classes-full-debug.jar full_classes_combined_jar := $(intermediates.COMMON)/classes-combined.jar +full_classes_desugar_jar := $(intermediates.COMMON)/desugar.classes.jar full_classes_jarjar_jar := $(intermediates.COMMON)/classes-jarjar.jar full_classes_jar := $(intermediates.COMMON)/classes.jar built_dex := $(intermediates.COMMON)/classes.dex @@ -42,6 +43,7 @@ LOCAL_INTERMEDIATE_TARGETS += \ $(full_classes_turbine_jar) \ $(full_classes_compiled_jar) \ $(full_classes_combined_jar) \ + $(full_classes_desugar_jar) \ $(full_classes_jarjar_jar) \ $(full_classes_jar) \ $(built_dex) \ @@ -156,6 +158,22 @@ endif $(eval $(call copy-one-file,$(full_classes_jarjar_jar),$(full_classes_jar))) +ifneq ($(USE_D8_DESUGAR),true) +my_desugaring := +ifeq ($(LOCAL_JAVA_LANGUAGE_VERSION),1.8) +my_desugaring := true +$(full_classes_desugar_jar): PRIVATE_DX_FLAGS := $(LOCAL_DX_FLAGS) +$(full_classes_desugar_jar): $(full_classes_jar) $(full_java_header_libs) $(DESUGAR) + $(desugar-classes-jar) +endif +else +my_desugaring := +endif + +ifndef my_desugaring +full_classes_desugar_jar := $(full_classes_jar) +endif + ifeq ($(LOCAL_IS_STATIC_JAVA_LIBRARY),true) # No dex; all we want are the .class files with resources. $(LOCAL_BUILT_MODULE) : $(java_resource_sources) @@ -166,8 +184,12 @@ $(LOCAL_BUILT_MODULE) : $(full_classes_jar) else # !LOCAL_IS_STATIC_JAVA_LIBRARY $(built_dex): PRIVATE_INTERMEDIATES_DIR := $(intermediates.COMMON) $(built_dex): PRIVATE_DX_FLAGS := $(LOCAL_DX_FLAGS) -$(built_dex): $(full_classes_jar) $(DX) $(ZIP2ZIP) +$(built_dex): $(full_classes_desugar_jar) $(DX) $(ZIP2ZIP) +ifneq ($(USE_D8_DESUGAR),true) $(transform-classes.jar-to-dex) +else + $(transform-classes-d8.jar-to-dex) +endif $(LOCAL_BUILT_MODULE): PRIVATE_DEX_FILE := $(built_dex) $(LOCAL_BUILT_MODULE): PRIVATE_SOURCE_ARCHIVE := $(full_classes_jarjar_jar) diff --git a/core/java.mk b/core/java.mk index 19e63772a3..8e5fd1df84 100644 --- a/core/java.mk +++ b/core/java.mk @@ -72,6 +72,7 @@ full_classes_header_jarjar := $(intermediates.COMMON)/classes-header-jarjar.jar full_classes_header_jar := $(intermediates.COMMON)/classes-header.jar full_classes_compiled_jar := $(intermediates.COMMON)/classes-full-debug.jar full_classes_processed_jar := $(intermediates.COMMON)/classes-processed.jar +full_classes_desugar_jar := $(intermediates.COMMON)/classes-desugar.jar full_classes_jarjar_jar := $(intermediates.COMMON)/classes-jarjar.jar full_classes_proguard_jar := $(intermediates.COMMON)/classes-proguard.jar full_classes_combined_jar := $(intermediates.COMMON)/classes-combined.jar @@ -93,6 +94,7 @@ endif LOCAL_INTERMEDIATE_TARGETS += \ $(full_classes_turbine_jar) \ $(full_classes_compiled_jar) \ + $(full_classes_desugar_jar) \ $(full_classes_jarjar_jar) \ $(full_classes_jar) \ $(full_classes_combined_jar) \ @@ -374,7 +376,23 @@ ifdef TARGET_OPENJDK9 LOCAL_DX_FLAGS := $(filter-out --multi-dex,$(LOCAL_DX_FLAGS)) --multi-dex endif -full_classes_pre_proguard_jar := $(LOCAL_FULL_CLASSES_JACOCO_JAR) +ifneq ($(USE_D8_DESUGAR),true) +my_desugaring := +ifndef LOCAL_IS_STATIC_JAVA_LIBRARY +my_desugaring := true +$(full_classes_desugar_jar): PRIVATE_DX_FLAGS := $(LOCAL_DX_FLAGS) +$(full_classes_desugar_jar): $(LOCAL_FULL_CLASSES_JACOCO_JAR) $(full_java_header_libs) $(DESUGAR) + $(desugar-classes-jar) +endif +else +my_desugaring := +endif + +ifndef my_desugaring +full_classes_desugar_jar := $(LOCAL_FULL_CLASSES_JACOCO_JAR) +endif + +full_classes_pre_proguard_jar := $(full_classes_desugar_jar) # Keep a copy of the jar just before proguard processing. $(eval $(call copy-one-file,$(full_classes_pre_proguard_jar),$(intermediates.COMMON)/classes-pre-proguard.jar)) @@ -545,7 +563,11 @@ endif # LOCAL_PROGUARD_ENABLED ifndef my_r8 $(built_dex_intermediate): $(full_classes_proguard_jar) $(DX) $(ZIP2ZIP) +ifneq ($(USE_D8_DESUGAR),true) $(transform-classes.jar-to-dex) +else + $(transform-classes-d8.jar-to-dex) +endif endif ifneq ($(filter $(LOCAL_MODULE),$(PRODUCT_BOOT_JARS)),) # is_boot_jar