android编译dex-preopt

对于android2.3编译时候选择下面的情况,既可以对dex进行preopt优化了。
如何从apk生成odex

在BoardConfig.mk中加入

# Enable the optimized DEX

WITH_DEXPREOPT=true

或者直接修改system.prop

# ODEX
dalvik.vm.verify-bytecode=true



$ cat buildspec.mk
TARGET_BUILD_VARIANT:=user
TARGET_BUILD_TYPE:=release


编译相关文件:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
build/core/product_config.mk:164
# Hack to make the linux build servers use dexpreopt (emulator-based
  # preoptimization). Most engineers don't use this type of target
  # ("make PRODUCT-blah-user"), so this should only tend to happen when
  # using buildbot.
  # TODO: Remove this once host Dalvik preoptimization is working.
  ifeq ($(TARGET_BUILD_VARIANT),user)
    WITH_DEXPREOPT_buildbot := true
  endif

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

build/core/dex_preopt.mk:127
# dexpreoption for binary released jars
# ONLY enabled in custom release
ifeq ($(PARTIAL_BUILD),true)
  ifeq ($(WITH_DEXPREOPT),true)
$(foreach m,$(DEXPREOPT_BINARY_RELEASE_BOOT_JARS_MODULES), \
    $(eval $(call install-dexpreopt-jars.binary-release,$(m))) \
    $(eval DEXPREOPT_BINARY_RELEASE_BOOT_JARS += \
        $(TARGET_OUT_JAVA_LIBRARIES)/$(m).jar \
        $(TARGET_OUT_JAVA_LIBRARIES)/$(m).odex \
     ) \
)
  endif
endif

# $(1): the input .jar or .apk file
# $(2): the output .odex file
define dexpreopt-one-file
$(hide) $(DEXPREOPT) --dexopt=$(DEXPREOPT_DEXOPT) --build-dir=$(DEXPREOPT_BUILD_DIR) \
        --product-dir=$(DEXPREOPT_PRODUCT_DIR) --boot-dir=$(DEXPREOPT_BOOT_JAR_DIR) \
        --boot-jars=$(DEXPREOPT_BOOT_JARS) $(DEXPREOPT_UNIPROCESSOR) \
        $(patsubst $(DEXPREOPT_BUILD_DIR)/%,%,$(1)) \
        $(patsubst $(DEXPREOPT_BUILD_DIR)/%,%,$(2))
endef

调用于:
build/core/dex_preopt.mk:111:   $$(call dexpreopt-one-file,$$(PRIVATE_DBJ_JAR),$$@)
build/core/package.mk:440:      $(call dexpreopt-one-file,$@,$(patsubst %.apk,%.odex,$@))
build/core/java_library.mk:121: $(call dexpreopt-one-file,$<,$@)
build/core/prebuilt.mk:82:      $(call dexpreopt-one-file,$@,$(patsubst %.apk,%.odex,$@))


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

build/core/package.mk:216
ifeq (true,$(WITH_DEXPREOPT))
ifeq (,$(TARGET_BUILD_APPS))
ifneq (,$(LOCAL_SRC_FILES))
ifndef LOCAL_DEX_PREOPT
LOCAL_DEX_PREOPT := true
endif
endif
endif
endif

实际编译时对生成apk的地方:
# Define the rule to build the actual package.
$(LOCAL_BUILT_MODULE): $(AAPT) | $(ZIPALIGN)
ifeq ($(LOCAL_DEX_PREOPT),true)
# Make sure the boot jars get dexpreopt-ed first
$(LOCAL_BUILT_MODULE): $(DEXPREOPT_BOOT_ODEXS) | $(DEXPREOPT) $(DEXOPT)
endif
$(LOCAL_BUILT_MODULE): PRIVATE_JNI_SHARED_LIBRARIES := $(jni_shared_libraries)
ifneq ($(TARGET_BUILD_APPS),)
    # Include all resources for unbundled apps.
    $(LOCAL_BUILT_MODULE): PRODUCT_AAPT_CONFIG :=
endif
$(LOCAL_BUILT_MODULE): $(all_res_assets) $(jni_shared_libraries) $(full_android_manifest)
@echo "target Package: $(PRIVATE_MODULE) ($@)"
$(create-empty-package)
$(add-assets-to-package)
ifneq ($(jni_shared_libraries),)
$(add-jni-shared-libs-to-package)
endif
ifneq ($(full_classes_jar),)
$(add-dex-to-package)
endif
$(sign-package) // apk签名
@# Alignment must happen after all other zip operations.
$(align-package)
ifeq ($(LOCAL_DEX_PREOPT),true)// 对apk进行dexpreopt,定义于build/core/dex_preopt.mk
$(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


apk签名函数定义于文件:build/core/definitions.mk +1456
# Sign a package using the specified key/cert.
#
define sign-package
$(hide) mv $@ [email protected]
$(hide) java -jar $(SIGNAPK_JAR) \
        $(PRIVATE_CERTIFICATE) $(PRIVATE_PRIVATE_KEY) [email protected] [email protected]
$(hide) mv [email protected] $@
endef

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
./core/main.mk:183:    WITH_DEXPREOPT := true
./core/main.mk:246:  WITH_DEXPREOPT := true
./core/main.mk:252:  ifeq ($(HOST_OS)-$(WITH_DEXPREOPT_buildbot),linux-true)
./core/main.mk:253:    WITH_DEXPREOPT := true


183
ifeq (user, $(TARGET_BUILD_VARIANT))
    WITH_DEXPREOPT := true
endif
# Bring in dex_preopt.mk
include $(BUILD_SYSTEM)/dex_preopt.mk

246
user_variant := $(filter userdebug user,$(TARGET_BUILD_VARIANT))
enable_target_debugging := true
ifneq (,$(user_variant))
...

# enable dex pre-optimization for all TARGET projects in default to
  # speed up device first boot-up
  WITH_DEXPREOPT := true

...
# TODO: Remove this and the corresponding block in
  # config/product_config.make once host-based Dalvik preoptimization is
  # working.
  ifneq (true,$(DISABLE_DEXPREOPT))
  ifeq ($(HOST_OS)-$(WITH_DEXPREOPT_buildbot),linux-true)
    WITH_DEXPREOPT := true
  endif
  endif
...


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
build/core/java_library.mk +68

ifeq (true,$(WITH_DEXPREOPT))
ifeq (,$(TARGET_BUILD_APPS))
ifndef LOCAL_DEX_PREOPT
LOCAL_DEX_PREOPT := true
endif
endif
endif

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
build/core/java_library.mk +68

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

你可能感兴趣的:(android,odex,dex-preopt)