Android DEX preopt prebuilt APK

AOSP编译APK的时候,默认情况下,对于prebuilt的APK,没有preopt。preopt的过程是把APK里面的.class分离出来,成为一个单独的.odex,加快Dalvik下APK的安装以及代码的执行速度。

下面对于core/prebuilt.mk的补丁可以实现对prebuilt的APK执行preopt的功能。

--- a/core/prebuilt.mk
+++ b/core/prebuilt.mk
@@ -16,6 +16,16 @@
 $(error dont use LOCAL_PREBUILT_JAVA_LIBRARIES anymore LOCAL_PATH=$(LOCAL_PATH))
 endif

+ifneq ($(filter APPS,$(LOCAL_MODULE_CLASS)),)
+ifeq (true,$(WITH_DEXPREOPT))
+ifeq (,$(TARGET_BUILD_APPS))
+ifndef LOCAL_DEX_PREOPT
+LOCAL_DEX_PREOPT := true
+endif
+endif
+endif
+endif
+
 include $(BUILD_SYSTEM)/base_rules.mk

 # Deal with the OSX library timestamp issue when installing

@@ -30,8 +40,20 @@

 # Ensure that prebuilt .apks have been aligned.
 ifneq ($(filter APPS,$(LOCAL_MODULE_CLASS)),)
+ifeq ($(LOCAL_DEX_PREOPT),true)
+# Make sure the boot jars get dexpreopt-ed first
+$(LOCAL_BUILT_MODULE): $(DEXPREOPT_BOOT_ODEXS) | $(DEXPREOPT) $(DEXOPT) $(AAPT)
+endif
 $(LOCAL_BUILT_MODULE) : $(LOCAL_PATH)/$(LOCAL_SRC_FILES) | $(ZIPALIGN)
    $(transform-prebuilt-to-target-with-zipalign)
+ifeq ($(LOCAL_DEX_PREOPT),true)
+   $(hide) rm -f $(patsubst %.apk,%.odex,$@)
+   $(call dexpreopt-one-file,$@,$(patsubst %.apk,%.odex,$@))
+   $(call dexpreopt-remove-classes.dex,$@)
+
+built_odex := $(basename $(LOCAL_BUILT_MODULE)).odex
+$(built_odex): $(LOCAL_BUILT_MODULE)
+endif
 else
 ifneq ($(LOCAL_PREBUILT_STRIP_COMMENTS),)
 $(LOCAL_BUILT_MODULE) : $(LOCAL_PATH)/$(LOCAL_SRC_FILES)

你可能感兴趣的:(Android)