[DESCRIPTION]
odex是从APK中提取出来的可运行文件,即将APK中的class.dex文件通过dex优化过程将其优化生成odex文件单独存放。
如果没有odex,系统需要在开机过程中从APK内提取dex再运行;如果在build过程中预先提取,可加快系统启动速度、节省开机时间。
目前的做法:
– User 版本会预先提取有source code的APK的odex文件,因为有source code的APK在Android.mk中都会通过include $(BUILD_PACKAGE)来编译,会调用到package.mk来提取odex
– 但User版本不会预先提取通过prebuilt方式预置的APK的odex文件,因为采用prebuilt方式预置APK是只有APK文件而没有source code时在Android.mk中通过 include $(BUILD_PREBUILT)预置APK,代码中原本的prebuilt.mk中不会提取odex
[KEYWORDS]
预置APK
build提取odex
odex
prebuilt
实现prebuilt APK提取odex的修改方法如下
修改build/core/prebuilt.mk(以下修改基于KK版本的源码,JB版本上此部分修改类似)
1.文件最开始部分添加红色部分
ifneq (true,$(WITH_DEXPREOPT))
LOCAL_DEX_PREOPT :=
else
ifndef LOCAL_DEX_PREOPT
LOCAL_DEX_PREOPT := $(DEX_PREOPT_DEFAULT)
endif
endif
ifeq (false,$(LOCAL_DEX_PREOPT))
LOCAL_DEX_PREOPT :=
endif
ifeq ($(filter APPS,$(LOCAL_MODULE_CLASS)),)
LOCAL_DEX_PREOPT :=
endif
$(info prebuild $(LOCAL_MODULE)==> $(LOCAL_DEX_PREOPT))
... ...
2.添加红色部分
... ...
ifdef LOCAL_IS_HOST_MODULE
my_prefix := HOST_
LOCAL_DEX_PREOPT :=
else
my_prefix := TARGET_
endif
... ...
3.添加红色部分
... ...
ifneq ($(filter APPS,$(LOCAL_MODULE_CLASS)),)
ifdef LOCAL_DEX_PREOPT
$(built_module): $(DEXPREOPT_BOOT_ODEXS) | $(DEXPREOPT) $(DEXOPT)
endif
ifeq ($(LOCAL_CERTIFICATE),PRESIGNED)
# Ensure that presigned .apks have been aligned.
$(built_module) : $(my_prebuilt_src_file) | $(ACP) $(ZIPALIGN)
#$(transform-prebuilt-to-target-with-zipalign)
$(transform-prebuilt-to-target)
ifdef LOCAL_DEX_PREOPT
$(hide) rm -f $(patsubst %.apk,%.odex,$@)
$(call dexpreopt-one-file,$@,$(patsubst %.apk,%.odex,$@))
ifneq (nostripping,$(LOCAL_DEX_PREOPT))
$(call dexpreopt-remove-classes.dex,$@)
endif
endif
$(align-package)
else
# Sign and align non-presigned .apks.
$(built_module) : $(my_prebuilt_src_file) | $(ACP) $(ZIPALIGN) $(SIGNAPK_JAR)
$(transform-prebuilt-to-target)
$(sign-package)
#add below
ifdef LOCAL_DEX_PREOPT
$(hide) rm -f $(patsubst %.apk,%.odex,$@)
$(call dexpreopt-one-file,$@,$(patsubst %.apk,%.odex,$@))
ifneq (nostripping,$(LOCAL_DEX_PREOPT))
$(call dexpreopt-remove-classes.dex,$@)
endif
endif
#add upon
$(align-package)
endif
ifdef LOCAL_DEX_PREOPT
built_odex := $(basename $(built_module)).odex
$(built_odex): $(built_module)
endif
else
... ...
说明:
(1)user build默认开启dexopt功能,eng build关闭,所以只是build user版本会提取APK的odex文件
(2)请务必不要忘记把预置的module添加到build/target/product/common.mk(或者[proj].mk)中的PRODUCT_PACKAGES里
(3)只有prebuilt预置APK时会生效,放置在vendor下拷贝的方式来预置APK是不会生效的。如一个apk文件放在目录vendor/mediatek/project_name/artifacts/out/target/product/project_name/system/app下通过拷贝方式来预置,build过程是不会提取它的odex文件的。