Move dexpreopting to Soong

Move the dexpreopting logic into Soong.  Make modules will be
dexpreopted by executing the Soong logic in the standalone
dexpreopt_gen binary, which will generate scripts that will
perform dexpreopting for each module.  Export global configuration
as JSON to $OUT/dexpreopt.config, which will be used by
dexpreopt_gen and Soong, and per-module JSON configuration that
will be used by dexpreopt_gen.

Bug: 119412419
Bug: 120273280
Test: no differences to dexpreopt outputs on aosp_sailfish system/,
      only expected changes to dexpreopt outputs on system_other
      (.vdex files for privileged Soong modules no longer incorrectly
      contain .dex contents).
Change-Id: I59b20c931ee3e5a8d35eb30da4148691c5095502
diff --git a/core/dex_preopt_odex_install.mk b/core/dex_preopt_odex_install.mk
index 47a03e8..d1fb4e8 100644
--- a/core/dex_preopt_odex_install.mk
+++ b/core/dex_preopt_odex_install.mk
@@ -25,24 +25,6 @@
 LOCAL_DEX_PREOPT := $(strip $(LOCAL_DEX_PREOPT))
 ifndef LOCAL_DEX_PREOPT # LOCAL_DEX_PREOPT undefined
   LOCAL_DEX_PREOPT := $(DEX_PREOPT_DEFAULT)
-
-  ifeq ($(filter $(TARGET_OUT)/%,$(my_module_path)),) # Not installed to system.img.
-    # Default to nostripping for non system preopt (enables preopt).
-    # Don't strip in case the oat/vdex version in system ROM doesn't match the one in other
-    # partitions. It needs to be able to fall back to the APK for that case.
-    LOCAL_DEX_PREOPT := nostripping
-  endif
-
-  ifneq (,$(LOCAL_APK_LIBRARIES)) # LOCAL_APK_LIBRARIES not empty
-    LOCAL_DEX_PREOPT := nostripping
-  endif
-endif
-
-ifeq (nostripping,$(LOCAL_DEX_PREOPT))
-  LOCAL_DEX_PREOPT := true
-  LOCAL_STRIP_DEX :=
-else
-  LOCAL_STRIP_DEX := true
 endif
 
 ifeq (false,$(LOCAL_DEX_PREOPT))
@@ -90,38 +72,8 @@
   endif
 endif
 
-ifeq ($(LOCAL_DEX_PREOPT),true)
-  # Don't strip with dexes we explicitly uncompress (dexopt will not store the dex code).
-  ifeq ($(LOCAL_UNCOMPRESS_DEX),true)
-    LOCAL_STRIP_DEX :=
-  endif  # LOCAL_UNCOMPRESS_DEX
-
-  # system_other isn't there for an OTA, so don't strip
-  # if module is on system, and odex is on system_other.
-  ifeq ($(BOARD_USES_SYSTEM_OTHER_ODEX),true)
-    ifneq ($(call install-on-system-other, $(my_module_path)),)
-      LOCAL_STRIP_DEX :=
-    endif  # install-on-system-other
-  endif  # BOARD_USES_SYSTEM_OTHER_ODEX
-
-  # We also don't strip if all dexs are uncompressed (dexopt will not store the dex code),
-  # but that requires to inspect the source file, which is too early at this point (as we
-  # don't know if the source file will actually be used).
-  # See dexpreopt-remove-classes.dex.
-endif  # LOCAL_DEX_PREOPT
-
-built_odex :=
-built_vdex :=
-built_art :=
-installed_odex :=
-installed_vdex :=
-installed_art :=
-built_installed_odex :=
-built_installed_vdex :=
-built_installed_art :=
 my_process_profile :=
 my_profile_is_text_listing :=
-my_generate_dm :=
 
 ifeq (false,$(WITH_DEX_PREOPT_GENERATE_PROFILE))
   LOCAL_DEX_PREOPT_GENERATE_PROFILE := false
@@ -135,7 +87,7 @@
 
   ifneq (,$(wildcard $(LOCAL_DEX_PREOPT_PROFILE)))
     my_process_profile := true
-    my_profile_is_text_listing := false
+    my_profile_is_text_listing :=
   endif
 else
   my_process_profile := $(LOCAL_DEX_PREOPT_GENERATE_PROFILE)
@@ -144,273 +96,72 @@
 endif
 
 ifeq (true,$(my_process_profile))
-
-  ifeq (,$(LOCAL_DEX_PREOPT_APP_IMAGE))
-    LOCAL_DEX_PREOPT_APP_IMAGE := true
-  endif
-
   ifndef LOCAL_DEX_PREOPT_PROFILE
     $(call pretty-error,Must have specified class listing (LOCAL_DEX_PREOPT_PROFILE))
   endif
   ifeq (,$(dex_preopt_profile_src_file))
     $(call pretty-error, Internal error: dex_preopt_profile_src_file must be set)
   endif
-  my_built_profile := $(dir $(LOCAL_BUILT_MODULE))/profile.prof
-  my_dex_location := $(patsubst $(PRODUCT_OUT)%,%,$(LOCAL_INSTALLED_MODULE))
-  # Remove compressed APK extension.
-  my_dex_location := $(patsubst %.gz,%,$(my_dex_location))
-  $(my_built_profile): PRIVATE_BUILT_MODULE := $(dex_preopt_profile_src_file)
-  $(my_built_profile): PRIVATE_DEX_LOCATION := $(my_dex_location)
-  $(my_built_profile): PRIVATE_SOURCE_CLASSES := $(LOCAL_DEX_PREOPT_PROFILE)
-  $(my_built_profile): $(LOCAL_DEX_PREOPT_PROFILE)
-  $(my_built_profile): $(PROFMAN)
-  $(my_built_profile): $(dex_preopt_profile_src_file)
-  ifeq (true,$(my_profile_is_text_listing))
-    # The profile is a test listing of classes (used for framework jars).
-    # We need to generate the actual binary profile before being able to compile.
-    $(my_built_profile):
-	$(hide) mkdir -p $(dir $@)
-	ANDROID_LOG_TAGS="*:e" $(PROFMAN) \
-		--create-profile-from=$(PRIVATE_SOURCE_CLASSES) \
-		--apk=$(PRIVATE_BUILT_MODULE) \
-		--dex-location=$(PRIVATE_DEX_LOCATION) \
-		--reference-profile-file=$@
-  else
-    # The profile is binary profile (used for apps). Run it through profman to
-    # ensure the profile keys match the apk.
-    $(my_built_profile):
-	$(hide) mkdir -p $(dir $@)
-	touch $@
-	ANDROID_LOG_TAGS="*:e" $(PROFMAN) \
-	  --copy-and-update-profile-key \
-		--profile-file=$(PRIVATE_SOURCE_CLASSES) \
-		--apk=$(PRIVATE_BUILT_MODULE) \
-		--dex-location=$(PRIVATE_DEX_LOCATION) \
-		--reference-profile-file=$@ \
-	|| echo "Profile out of date for $(PRIVATE_BUILT_MODULE)"
-  endif
-
-  my_profile_is_text_listing :=
-  dex_preopt_profile_src_file :=
-
-  # Remove compressed APK extension.
-  my_installed_profile := $(patsubst %.gz,%,$(LOCAL_INSTALLED_MODULE)).prof
-
-  # my_installed_profile := $(LOCAL_INSTALLED_MODULE).prof
-  $(eval $(call copy-one-file,$(my_built_profile),$(my_installed_profile)))
-  build_installed_profile:=$(my_built_profile):$(my_installed_profile)
-else
-  build_installed_profile:=
-  my_installed_profile :=
 endif
 
+# If LOCAL_ENFORCE_USES_LIBRARIES is not set, default to true if either of LOCAL_USES_LIBRARIES or
+# LOCAL_OPTIONAL_USES_LIBRARIES are specified.
+ifeq (,$(LOCAL_ENFORCE_USES_LIBRARIES))
+  # Will change the default to true unconditionally in the future.
+  ifneq (,$(LOCAL_OPTIONAL_USES_LIBRARIES))
+    LOCAL_ENFORCE_USES_LIBRARIES := true
+  endif
+  ifneq (,$(LOCAL_USES_LIBRARIES))
+    LOCAL_ENFORCE_USES_LIBRARIES := true
+  endif
+endif
+
+my_dexpreopt_archs :=
+
 ifdef LOCAL_DEX_PREOPT
-
-  dexpreopt_boot_jar_module := $(filter $(DEXPREOPT_BOOT_JARS_MODULES),$(LOCAL_MODULE))
-
-  ifdef dexpreopt_boot_jar_module
-    # For libart, the boot jars' odex files are replaced by $(DEFAULT_DEX_PREOPT_INSTALLED_IMAGE).
-    # We use this installed_odex trick to get boot.art installed.
-    installed_odex := $(DEFAULT_DEX_PREOPT_INSTALLED_IMAGE)
-    # Append the odex for the 2nd arch if we have one.
-    installed_odex += $($(TARGET_2ND_ARCH_VAR_PREFIX)DEFAULT_DEX_PREOPT_INSTALLED_IMAGE)
-  else  # boot jar
-    ifeq ($(LOCAL_MODULE_CLASS),JAVA_LIBRARIES)
-
-      my_module_multilib := $(LOCAL_MULTILIB)
-      # If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
-      my_filtered_lib_name := $(patsubst %.impl,%,$(LOCAL_MODULE))
-      ifeq (,$(filter $(JAVA_SDK_LIBRARIES),$(my_filtered_lib_name)))
-        # For a Java library, by default we build odex for both 1st arch and 2nd arch.
-        # But it can be overridden with "LOCAL_MULTILIB := first".
-        ifneq (,$(filter $(PRODUCT_SYSTEM_SERVER_JARS),$(LOCAL_MODULE)))
-          # For system server jars, we build for only "first".
-          my_module_multilib := first
-        endif
-      endif
-
-      # Only preopt primary arch for translated arch since there is only an image there.
-      ifeq ($(TARGET_TRANSLATE_2ND_ARCH),true)
+  ifeq ($(LOCAL_MODULE_CLASS),JAVA_LIBRARIES)
+    my_module_multilib := $(LOCAL_MULTILIB)
+    # If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
+    my_filtered_lib_name := $(patsubst %.impl,%,$(LOCAL_MODULE))
+    ifeq (,$(filter $(JAVA_SDK_LIBRARIES),$(my_filtered_lib_name)))
+      # For a Java library, by default we build odex for both 1st arch and 2nd arch.
+      # But it can be overridden with "LOCAL_MULTILIB := first".
+      ifneq (,$(filter $(PRODUCT_SYSTEM_SERVER_JARS),$(LOCAL_MODULE)))
+        # For system server jars, we build for only "first".
         my_module_multilib := first
       endif
-
-      # #################################################
-      # Odex for the 1st arch
-      my_2nd_arch_prefix :=
-      include $(BUILD_SYSTEM)/setup_one_odex.mk
-      # #################################################
-      # Odex for the 2nd arch
-      ifdef TARGET_2ND_ARCH
-        ifneq ($(TARGET_TRANSLATE_2ND_ARCH),true)
-          ifneq (first,$(my_module_multilib))
-            my_2nd_arch_prefix := $(TARGET_2ND_ARCH_VAR_PREFIX)
-            include $(BUILD_SYSTEM)/setup_one_odex.mk
-          endif  # my_module_multilib is not first.
-        endif  # TARGET_TRANSLATE_2ND_ARCH not true
-      endif  # TARGET_2ND_ARCH
-      # #################################################
-    else  # must be APPS
-      # The preferred arch
-      my_2nd_arch_prefix := $(LOCAL_2ND_ARCH_VAR_PREFIX)
-      # Save the module multilib since setup_one_odex modifies it.
-      saved_my_module_multilib := $(my_module_multilib)
-      include $(BUILD_SYSTEM)/setup_one_odex.mk
-      my_module_multilib := $(saved_my_module_multilib)
-      ifdef TARGET_2ND_ARCH
-        ifeq ($(my_module_multilib),both)
-          # The non-preferred arch
-          my_2nd_arch_prefix := $(if $(LOCAL_2ND_ARCH_VAR_PREFIX),,$(TARGET_2ND_ARCH_VAR_PREFIX))
-          include $(BUILD_SYSTEM)/setup_one_odex.mk
-        endif  # LOCAL_MULTILIB is both
-      endif  # TARGET_2ND_ARCH
-    endif  # LOCAL_MODULE_CLASS
-  endif  # boot jar
-
-  built_odex := $(strip $(built_odex))
-  built_vdex := $(strip $(built_vdex))
-  built_art := $(strip $(built_art))
-  installed_odex := $(strip $(installed_odex))
-  installed_vdex := $(strip $(installed_vdex))
-  installed_art := $(strip $(installed_art))
-
-  ifdef built_odex
-    ifeq (true,$(my_process_profile))
-      $(built_odex): $(my_built_profile)
-      $(built_odex): PRIVATE_PROFILE_PREOPT_FLAGS := --profile-file=$(my_built_profile)
-    else
-      $(built_odex): PRIVATE_PROFILE_PREOPT_FLAGS :=
     endif
 
-    ifndef LOCAL_DEX_PREOPT_FLAGS
-      LOCAL_DEX_PREOPT_FLAGS := $(DEXPREOPT.$(TARGET_PRODUCT).$(LOCAL_MODULE).CONFIG)
-      ifndef LOCAL_DEX_PREOPT_FLAGS
-        LOCAL_DEX_PREOPT_FLAGS := $(PRODUCT_DEX_PREOPT_DEFAULT_FLAGS)
-      endif
+    # Only preopt primary arch for translated arch since there is only an image there.
+    ifeq ($(TARGET_TRANSLATE_2ND_ARCH),true)
+      my_module_multilib := first
     endif
 
-    my_system_server_compiler_filter := $(PRODUCT_SYSTEM_SERVER_COMPILER_FILTER)
-    ifeq (,$(my_system_server_compiler_filter))
-      my_system_server_compiler_filter := speed
-    endif
-
-    my_default_compiler_filter := $(PRODUCT_DEX_PREOPT_DEFAULT_COMPILER_FILTER)
-    ifeq (,$(my_default_compiler_filter))
-      # If no default compiler filter is specified, default to 'quicken' to save on storage.
-      my_default_compiler_filter := quicken
-    endif
-
-    ifeq (,$(filter --compiler-filter=%, $(LOCAL_DEX_PREOPT_FLAGS)))
-      ifneq (,$(filter $(PRODUCT_SYSTEM_SERVER_JARS),$(LOCAL_MODULE)))
-        # Jars of system server, use the product option if it is set, speed otherwise.
-        LOCAL_DEX_PREOPT_FLAGS += --compiler-filter=$(my_system_server_compiler_filter)
-      else
-        ifneq (,$(filter $(PRODUCT_DEXPREOPT_SPEED_APPS) $(PRODUCT_SYSTEM_SERVER_APPS),$(LOCAL_MODULE)))
-          # Apps loaded into system server, and apps the product default to being compiled with the
-          # 'speed' compiler filter.
-          LOCAL_DEX_PREOPT_FLAGS += --compiler-filter=speed
-        else
-          ifeq (true,$(my_process_profile))
-            # For non system server jars, use speed-profile when we have a profile.
-            LOCAL_DEX_PREOPT_FLAGS += --compiler-filter=speed-profile
-          else
-            LOCAL_DEX_PREOPT_FLAGS += --compiler-filter=$(my_default_compiler_filter)
-          endif
-        endif
-      endif
-    endif
-
-    my_generate_dm := $(PRODUCT_DEX_PREOPT_GENERATE_DM_FILES)
-    ifeq (,$(filter $(LOCAL_DEX_PREOPT_FLAGS),--compiler-filter=verify))
-      # Generating DM files only makes sense for verify, avoid doing for non verify compiler filter APKs.
-      my_generate_dm := false
-    endif
-
-    # No reason to use a dm file if the dex is already uncompressed.
-    ifeq ($(LOCAL_UNCOMPRESS_DEX),true)
-      my_generate_dm := false
-    endif
-
-    ifeq (true,$(my_generate_dm))
-      LOCAL_DEX_PREOPT_FLAGS += --copy-dex-files=false
-      LOCAL_DEX_PREOPT := true
-      LOCAL_STRIP_DEX :=
-      my_built_dm := $(dir $(LOCAL_BUILT_MODULE))generated.dm
-      my_installed_dm := $(patsubst %.apk,%,$(LOCAL_INSTALLED_MODULE)).dm
-      my_copied_vdex := $(dir $(LOCAL_BUILT_MODULE))primary.vdex
-      $(eval $(call copy-one-file,$(built_vdex),$(my_copied_vdex)))
-      $(my_built_dm): PRIVATE_INPUT_VDEX := $(my_copied_vdex)
-      $(my_built_dm): $(my_copied_vdex) $(ZIPTIME)
-	$(hide) mkdir -p $(dir $@)
-	$(hide) rm -f $@
-	$(hide) zip -qD -j -X -9 $@ $(PRIVATE_INPUT_VDEX)
-	$(ZIPTIME) $@
-      $(eval $(call copy-one-file,$(my_built_dm),$(my_installed_dm)))
-    endif
-
-    # By default, emit debug info.
-    my_dexpreopt_debug_info := true
-    # If the global setting suppresses mini-debug-info, disable it.
-    ifeq (false,$(WITH_DEXPREOPT_DEBUG_INFO))
-      my_dexpreopt_debug_info := false
-    endif
-
-    # PRODUCT_SYSTEM_SERVER_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO.
-    # PRODUCT_OTHER_JAVA_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO.
-    ifneq (,$(filter $(PRODUCT_SYSTEM_SERVER_JARS),$(LOCAL_MODULE)))
-      ifeq (true,$(PRODUCT_SYSTEM_SERVER_DEBUG_INFO))
-        my_dexpreopt_debug_info := true
-      else ifeq (false,$(PRODUCT_SYSTEM_SERVER_DEBUG_INFO))
-        my_dexpreopt_debug_info := false
-      endif
-    else
-      ifeq (true,$(PRODUCT_OTHER_JAVA_DEBUG_INFO))
-        my_dexpreopt_debug_info := true
-      else ifeq (false,$(PRODUCT_OTHER_JAVA_DEBUG_INFO))
-        my_dexpreopt_debug_info := false
-      endif
-    endif
-
-    # Never enable on eng.
-    ifeq (eng,$(filter eng, $(TARGET_BUILD_VARIANT)))
-      my_dexpreopt_debug_info := false
-    endif
-
-    # Add dex2oat flag for debug-info/no-debug-info.
-    ifeq (true,$(my_dexpreopt_debug_info))
-      LOCAL_DEX_PREOPT_FLAGS += --generate-mini-debug-info
-    else ifeq (false,$(my_dexpreopt_debug_info))
-      LOCAL_DEX_PREOPT_FLAGS += --no-generate-mini-debug-info
-    endif
-
-    # Set the compiler reason to 'prebuilt' to identify the oat files produced
-    # during the build, as opposed to compiled on the device.
-    LOCAL_DEX_PREOPT_FLAGS += --compilation-reason=prebuilt
-
-    $(built_odex): PRIVATE_DEX_PREOPT_FLAGS := $(LOCAL_DEX_PREOPT_FLAGS)
-    $(built_vdex): $(built_odex)
-    $(built_art): $(built_odex)
-  endif
-
-  ifneq (true,$(my_generate_dm))
-    # Add the installed_odex to the list of installed files for this module if we aren't generating a
-    # dm file.
-    ALL_MODULES.$(my_register_name).INSTALLED += $(installed_odex)
-    ALL_MODULES.$(my_register_name).INSTALLED += $(installed_vdex)
-    ALL_MODULES.$(my_register_name).INSTALLED += $(installed_art)
-
-    ALL_MODULES.$(my_register_name).BUILT_INSTALLED += $(built_installed_odex)
-    ALL_MODULES.$(my_register_name).BUILT_INSTALLED += $(built_installed_vdex)
-    ALL_MODULES.$(my_register_name).BUILT_INSTALLED += $(built_installed_art)
-
-    # Make sure to install the .odex and .vdex when you run "make <module_name>"
-    $(my_all_targets): $(installed_odex) $(installed_vdex) $(installed_art)
-  else
-    ALL_MODULES.$(my_register_name).INSTALLED += $(my_installed_dm)
-    ALL_MODULES.$(my_register_name).BUILT_INSTALLED += $(my_built_dm) $(my_installed_dm)
-
-    # Make sure to install the .dm when you run "make <module_name>"
-    $(my_all_targets): $(installed_dm)
-  endif
+    # #################################################
+    # Odex for the 1st arch
+    my_dexpreopt_archs += $(TARGET_ARCH)
+    # Odex for the 2nd arch
+    ifdef TARGET_2ND_ARCH
+      ifneq ($(TARGET_TRANSLATE_2ND_ARCH),true)
+        ifneq (first,$(my_module_multilib))
+          my_dexpreopt_archs += $(TARGET_2ND_ARCH)
+        endif  # my_module_multilib is not first.
+      endif  # TARGET_TRANSLATE_2ND_ARCH not true
+    endif  # TARGET_2ND_ARCH
+    # #################################################
+  else  # must be APPS
+    # The preferred arch
+    # Save the module multilib since setup_one_odex modifies it.
+    my_2nd_arch_prefix := $(LOCAL_2ND_ARCH_VAR_PREFIX)
+    my_dexpreopt_archs += $(TARGET_$(my_2nd_arch_prefix)ARCH)
+    ifdef TARGET_2ND_ARCH
+      ifeq ($(my_module_multilib),both)
+        # The non-preferred arch
+        my_2nd_arch_prefix := $(if $(LOCAL_2ND_ARCH_VAR_PREFIX),,$(TARGET_2ND_ARCH_VAR_PREFIX))
+        my_dexpreopt_archs += $(TARGET_$(my_2nd_arch_prefix)ARCH)
+      endif  # LOCAL_MULTILIB is both
+    endif  # TARGET_2ND_ARCH
+  endif  # LOCAL_MODULE_CLASS
 
   # Record dex-preopt config.
   DEXPREOPT.$(LOCAL_MODULE).DEX_PREOPT := $(LOCAL_DEX_PREOPT)
@@ -419,17 +170,97 @@
   DEXPREOPT.$(LOCAL_MODULE).PRIVILEGED_MODULE := $(LOCAL_PRIVILEGED_MODULE)
   DEXPREOPT.$(LOCAL_MODULE).VENDOR_MODULE := $(LOCAL_VENDOR_MODULE)
   DEXPREOPT.$(LOCAL_MODULE).TARGET_ARCH := $(LOCAL_MODULE_TARGET_ARCH)
-  DEXPREOPT.$(LOCAL_MODULE).INSTALLED := $(installed_odex)
   DEXPREOPT.$(LOCAL_MODULE).INSTALLED_STRIPPED := $(LOCAL_INSTALLED_MODULE)
   DEXPREOPT.MODULES.$(LOCAL_MODULE_CLASS) := $(sort \
     $(DEXPREOPT.MODULES.$(LOCAL_MODULE_CLASS)) $(LOCAL_MODULE))
 
+  $(call json_start)
+
+  $(call add_json_str,  Name,                          $(LOCAL_MODULE))
+  $(call add_json_str,  DexLocation,                   $(patsubst $(PRODUCT_OUT)%,%,$(LOCAL_INSTALLED_MODULE)))
+  $(call add_json_str,  BuildPath,                     $(LOCAL_BUILT_MODULE))
+  $(call add_json_str,  DexPath,                       $$1)
+  $(call add_json_str,  ExtrasOutputPath,              $$2)
+  $(call add_json_bool, PreferIntegrity,               $(filter true,$(LOCAL_PREFER_INTEGRITY)))
+  $(call add_json_bool, Privileged,                    $(filter true,$(LOCAL_PRIVILEGED_MODULE)))
+  $(call add_json_bool, UncompressedDex,               $(filter true,$(LOCAL_UNCOMPRESS_DEX)))
+  $(call add_json_bool, HasApkLibraries,               $(LOCAL_APK_LIBRARIES))
+  $(call add_json_list, PreoptFlags,                   $(LOCAL_DEX_PREOPT_FLAGS))
+  $(call add_json_str,  ProfileClassListing,           $(if $(my_process_profile),$(LOCAL_DEX_PREOPT_PROFILE)))
+  $(call add_json_bool, ProfileIsTextListing,          $(my_profile_is_text_listing))
+  $(call add_json_bool, EnforceUsesLibraries,          $(LOCAL_ENFORCE_USES_LIBRARIES))
+  $(call add_json_list, OptionalUsesLibraries,         $(LOCAL_OPTIONAL_USES_LIBRARIES))
+  $(call add_json_list, UsesLibraries,                 $(LOCAL_USES_LIBRARIES))
+  $(call add_json_map,  LibraryPaths)
+  $(foreach lib,$(sort $(LOCAL_USES_LIBRARIES) $(LOCAL_OPTIONAL_USES_LIBRARIES) org.apache.http.legacy.impl),\
+    $(call add_json_str, $(lib), $(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),,COMMON)/javalib.jar))
+  $(call end_json_map)
+  $(call add_json_list, Archs,                         $(my_dexpreopt_archs))
+  $(call add_json_str,  DexPreoptImageLocation,        $(LOCAL_DEX_PREOPT_IMAGE_LOCATION))
+  $(call add_json_bool, PreoptExtractedApk,            $(my_preopt_for_extracted_apk))
+  $(call add_json_bool, NoCreateAppImage,              $(filter false,$(LOCAL_DEX_PREOPT_APP_IMAGE)))
+  $(call add_json_bool, ForceCreateAppImage,           $(filter true,$(LOCAL_DEX_PREOPT_APP_IMAGE)))
+  $(call add_json_bool, PresignedPrebuilt,             $(filter PRESIGNED,$(LOCAL_CERTIFICATE)))
+
+  $(call add_json_str,  StripInputPath,                $$1)
+  $(call add_json_str,  StripOutputPath,               $$2)
+
+  $(call json_end)
+
+  my_dexpreopt_config := $(intermediates)/dexpreopt.config
+  my_dexpreopt_script := $(intermediates)/dexpreopt.sh
+  my_strip_script := $(intermediates)/strip.sh
+  my_dexpreopt_zip := $(intermediates)/dexpreopt.zip
+
+  $(my_dexpreopt_config): PRIVATE_MODULE := $(LOCAL_MODULE)
+  $(my_dexpreopt_config): PRIVATE_CONTENTS := $(json_contents)
+  $(my_dexpreopt_config):
+	@echo "$(PRIVATE_MODULE) dexpreopt.config"
+	echo -e -n '$(subst $(newline),\n,$(subst ','\'',$(subst \,\\,$(PRIVATE_CONTENTS))))' > $@
+
+  .KATI_RESTAT: $(my_dexpreopt_script) $(my_strip_script)
+  $(my_dexpreopt_script): PRIVATE_MODULE := $(LOCAL_MODULE)
+  $(my_dexpreopt_script): PRIVATE_GLOBAL_CONFIG := $(PRODUCT_OUT)/dexpreopt.config
+  $(my_dexpreopt_script): PRIVATE_MODULE_CONFIG := $(my_dexpreopt_config)
+  $(my_dexpreopt_script): PRIVATE_STRIP_SCRIPT := $(my_strip_script)
+  $(my_dexpreopt_script): .KATI_IMPLICIT_OUTPUTS := $(my_strip_script)
+  $(my_dexpreopt_script): $(DEXPREOPT_GEN)
+  $(my_dexpreopt_script): $(my_dexpreopt_config) $(PRODUCT_OUT)/dexpreopt.config
+	@echo "$(PRIVATE_MODULE) dexpreopt gen"
+	$(DEXPREOPT_GEN) -global $(PRIVATE_GLOBAL_CONFIG) -module $(PRIVATE_MODULE_CONFIG) \
+	-extras_zip '"$$2"' -dexpreopt_script $@ -strip_script $(PRIVATE_STRIP_SCRIPT)
+
+  my_dexpreopt_deps := $(my_dex_jar)
+  my_dexpreopt_deps += $(if $(my_process_profile),$(LOCAL_DEX_PREOPT_PROFILE))
+  my_dexpreopt_deps += \
+    $(foreach lib,$(sort $(LOCAL_USES_LIBRARIES) $(LOCAL_OPTIONAL_USES_LIBRARIES) org.apache.http.legacy.impl),\
+      $(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),,COMMON)/javalib.jar)
+  my_dexpreopt_deps += $(LOCAL_DEX_PREOPT_IMAGE_LOCATION)
+  # TODO: default boot images
+
+  $(my_dexpreopt_zip): PRIVATE_MODULE := $(LOCAL_MODULE)
+  $(my_dexpreopt_zip): $(my_dexpreopt_deps)
+  $(my_dexpreopt_zip): | $(DEXPREOPT_GEN_DEPS)
+  $(my_dexpreopt_zip): .KATI_DEPFILE := $(my_dexpreopt_zip).d
+  $(my_dexpreopt_zip): PRIVATE_DEX := $(my_dex_jar)
+  $(my_dexpreopt_zip): PRIVATE_SCRIPT := $(my_dexpreopt_script)
+  $(my_dexpreopt_zip): $(my_dexpreopt_script)
+	@echo "$(PRIVATE_MODULE) dexpreopt"
+	bash $(PRIVATE_SCRIPT) $(PRIVATE_DEX) $@
+
+  ifdef LOCAL_POST_INSTALL_CMD
+    # Add a shell command separator
+    LOCAL_POST_INSTALL_CMD += &&
+  endif
+
+  LOCAL_POST_INSTALL_CMD += unzip -qo -d $(PRODUCT_OUT) $(my_dexpreopt_zip)
+  $(LOCAL_INSTALLED_MODULE): PRIVATE_POST_INSTALL_CMD := $(LOCAL_POST_INSTALL_CMD)
+  $(LOCAL_INSTALLED_MODULE): $(my_dexpreopt_zip)
+
+  $(my_all_targets): $(my_dexpreopt_zip)
+
+  my_dexpreopt_config :=
+  my_dexpreopt_script :=
+  my_strip_script :=
+  my_dexpreopt_zip :=
 endif # LOCAL_DEX_PREOPT
-
-# Profile doesn't depend on LOCAL_DEX_PREOPT.
-ALL_MODULES.$(my_register_name).INSTALLED += $(my_installed_profile)
-ALL_MODULES.$(my_register_name).BUILT_INSTALLED += $(build_installed_profile)
-
-my_process_profile :=
-
-$(my_all_targets): $(my_installed_profile)