Split the rules to build the odex file

Previously the odex file is byproduct generated by the package.apk rule.
Though we have the odex file depend on the package.apk it doesn't have
its own build recipe. In case package.apk isn't updated but we still
need to update the odex file (such as changed LOCAL_MULTILIB), the odex
file will never be rebuilt.
This change split out the rules to build the odex file and make sure the
build recipe get executed if the odex file needs rebuild.

Change-Id: I60c2f32b536b3d59045301ee863aae1451734aad
diff --git a/core/package_internal.mk b/core/package_internal.mk
index f8d2f43..cb1e424 100644
--- a/core/package_internal.mk
+++ b/core/package_internal.mk
@@ -336,12 +336,6 @@
 
 # Define the rule to build the actual package.
 $(LOCAL_BUILT_MODULE): $(AAPT) | $(ZIPALIGN)
-ifdef LOCAL_DEX_PREOPT
-$(LOCAL_BUILT_MODULE): PRIVATE_BUILT_ODEX := $(built_odex)
-
-# built_odex is byproduct of LOCAL_BUILT_MODULE without its own build recipe.
-$(built_odex) : $(LOCAL_BUILT_MODULE)
-endif
 $(LOCAL_BUILT_MODULE): PRIVATE_JNI_SHARED_LIBRARIES := $(jni_shared_libraries)
 $(LOCAL_BUILT_MODULE): PRIVATE_JNI_SHARED_LIBRARIES_ABI := $(jni_shared_libraries_abi)
 ifneq ($(TARGET_BUILD_APPS),)
@@ -371,7 +365,6 @@
 endif
 	$(sign-package)
 ifdef LOCAL_DEX_PREOPT
-	$(call dexpreopt-one-file,$@,$(PRIVATE_BUILT_ODEX))
 ifneq (nostripping,$(LOCAL_DEX_PREOPT))
 	$(call dexpreopt-remove-classes.dex,$@)
 endif
@@ -379,6 +372,18 @@
 	@# Alignment must happen after all other zip operations.
 	$(align-package)
 
+###############################
+## Rule to build the odex file
+ifdef LOCAL_DEX_PREOPT
+$(built_odex): PRIVATE_DEX_FILE := $(built_dex)
+$(built_odex) : $(built_dex)
+	$(create-empty-package)
+	$(add-dex-to-package)
+	$(hide) mv $@ $@.input
+	$(call dexpreopt-one-file,$@.input,$@)
+	$(hide) rm $@.input
+endif
+
 # Save information about this package
 PACKAGES.$(LOCAL_PACKAGE_NAME).OVERRIDES := $(strip $(LOCAL_OVERRIDES_PACKAGES))
 PACKAGES.$(LOCAL_PACKAGE_NAME).RESOURCE_FILES := $(all_resources)