Android里面classes.dex如何生成

1.生成classes.dex

  一直在寻找快速编译framework的方法,网上找到一篇,无奈修改的地方不同,我修改的是framework.jar里面的Multiwaveview,也就是修改解锁界面。想起编译整个系统到target Dex: framework这个地方特别耗时,于是:

cd ./build
grep -R "Dex" .

找到:./build/core/definitions.mk,

#TODO: use a smaller -Xmx value for most libraries;
#      only core.jar and framework.jar need a heap this big.
# Avoid the memory arguments on Windows, dx fails to load for some reason with them.
define transform-classes.jar-to-dex
@echo "target Dex: $(PRIVATE_MODULE)"
@mkdir -p $(dir $@)
$(hide) $(DX) \
    $(if $(findstring windows,$(HOST_OS)),,-JXms16M -JXmx2048M) \
    --dex --output=$@ \
    $(incremental_dex) \
    $(if $(NO_OPTIMIZE_DX), \
        --no-optimize) \
    $(if $(GENERATE_DEX_DEBUG), \
        --debug --verbose \
        --dump-to=$(@:.dex=.lst) \
        --dump-width=1000) \
    $(PRIVATE_DX_FLAGS) \
    $<
endef

从上面的shell函数定义来看,命令被隐藏了,因为使用了$(hide),所以具体在打包Dex文件时用了什么命令我们无从得知,去掉这个$(hide)。重新编译:

make clean-framework
make framework

编译到target Dex: framework时输出:

Aidl: framework <= frameworks/base/telephony/java/com/android/internal/telephony/ITelephony.aidlAidl: framework <= frameworks/base/telephony/java/com/android/internal/telephony/ITelephonyRegistry.aidl

Aidl: framework <= frameworks/base/telephony/java/com/android/internal/telephony/IIccPhoneBook.aidl
Aidl: framework <= frameworks/base/telephony/java/com/android/internal/telephony/ISms.aidl
Aidl: framework <= frameworks/base/telephony/java/com/android/internal/telephony/IWapPushManager.aidl
Aidl: framework <= frameworks/base/wifi/java/android/net/wifi/IWifiManager.aidl
Aidl: framework <= frameworks/base/wifi/java/android/net/wifi/p2p/IWifiP2pManager.aidl
Aidl: framework <= frameworks/base/ethernet/java/android/net/ethernet/IEthernetManager.aidl
Aidl: framework <= frameworks/base/telephony/java/com/android/internal/telephony/IExtendedNetworkService.aidl
Aidl: framework <= frameworks/base/voip/java/android/net/sip/ISipSession.aidl
Aidl: framework <= frameworks/base/voip/java/android/net/sip/ISipSessionListener.aidl
Aidl: framework <= frameworks/base/voip/java/android/net/sip/ISipService.aidl
logtags: out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/core/java/android/content/EventLogTags.java <= frameworks/base/core/java/android/content/EventLogTags.logtags
logtags: out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/core/java/android/speech/tts/EventLogTags.java <= frameworks/base/core/java/android/speech/tts/EventLogTags.logtags
logtags: out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/core/java/android/webkit/EventLogTags.java <= frameworks/base/core/java/android/webkit/EventLogTags.logtags
logtags: out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/telephony/java/com/android/internal/telephony/EventLogTags.java <= frameworks/base/telephony/java/com/android/internal/telephony/EventLogTags.logtags
target Java: framework (out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes)
注意:某些输入文件使用或覆盖了已过时的 API。
注意:要了解详细信息,请使用 -Xlint:deprecation 重新编译。
注意:某些输入文件使用了未经检查或不安全的操作。
注意:要了解详细信息,请使用 -Xlint:unchecked 重新编译。
Copying: out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes-jarjar.jar
Copying: out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/emma_out/lib/classes-jarjar.jar
Copying: out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar
Copying: out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/noproguard.classes.jar
target Dex: framework

out/host/linux-x86/bin/dx -JXms16M -JXmx1536M --dex --output=out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/noproguard.classes-with-local.dex    --core-library out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/noproguard.classes.jar


结论一:原来Android就是用dx工具来将jar文件进一步打包成dex文件。


2. 如何将classes.dex添加到jar文件

   还是definitions.mk文件,这个文件里面的函数,去掉前面的hide。

#TODO: update the manifest to point to the dex file
define add-dex-to-package
$(if $(filter classes.dex,$(notdir $(PRIVATE_DEX_FILE))),\
$(AAPT) add -k $@ $(PRIVATE_DEX_FILE),\
_adtp_classes_dex=$(dir $(PRIVATE_DEX_FILE))classes.dex; \
cp $(PRIVATE_DEX_FILE) $$_adtp_classes_dex && \
$(AAPT) add -k $@ $$_adtp_classes_dex && rm -f $$_adtp_classes_dex)
endef

   于是,我们在上面的基础上得到如下输出:

target Dex: framework
out/host/linux-x86/bin/dx -JXms16M -JXmx1536M --dex --output=out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/noproguard.classes-with-local.dex    --core-library out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/noproguard.classes.jar
Copying: out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/noproguard.classes.dex
target Jar: framework (out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/javalib.jar)
_adtp_classes_dex=out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.dex;cp out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/noproguard.classes.dex$_adtp_classes_dex &&out/host/linux-x86/bin/aapt add -k out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/javalib.jar $_adtp_classes_dex && rm -f $_adtp_classes_dex
 'out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.dex' as 'classes.dex'...

Notice file: frameworks/base/NOTICE -- out/target/product/g04ref/obj/NOTICE_FILES/src//system/framework/framework.jar.txt
target Package: framework-res (out/target/product/g04ref/obj/APPS/framework-res_intermediates/package.apk)
libpng warning: Ignoring attempt to set cHRM RGB triangle with zero area
libpng warning: Ignoring attempt to set cHRM RGB triangle with zero area
aapt: warning: string 'gsm_alphabet_default_charset' has no default translation in frameworks/base/core/res/res; found: ko
aapt: warning: **** string 'gsm_alphabet_default_charset' has no default or required localization for 'en_US' in frameworks/base/core/res/res
aapt: warning: **** string 'gsm_alphabet_default_charset' has no default or required localization for 'zh_CN' in frameworks/base/core/res/res
aapt: warning: **** string 'gsm_alphabet_default_charset' has no default or required localization for 'zh_TW' in frameworks/base/core/res/res
aapt: warning: **** string 'gsm_alphabet_default_charset' has no default or required localization for 'en_GB' in frameworks/base/core/res/res
aapt: warning: **** string 'gsm_alphabet_default_charset' has no default or required localization for 'fr_FR' in frameworks/base/core/res/res
aapt: warning: **** string 'gsm_alphabet_default_charset' has no default or required localization for 'it_IT' in frameworks/base/core/res/res
aapt: warning: **** string 'gsm_alphabet_default_charset' has no default or required localization for 'cs_CZ' in frameworks/base/core/res/res
aapt: warning: **** string 'gsm_alphabet_default_charset' has no default or required localization for 'de_DE' in frameworks/base/core/res/res
aapt: warning: **** string 'gsm_alphabet_default_charset' has no default or required localization for 'da_DK' in frameworks/base/core/res/res
aapt: warning: **** string 'gsm_alphabet_default_charset' has no default or required localization for 'el_GR' in frameworks/base/core/res/res
aapt: warning: **** string 'gsm_alphabet_default_charset' has no default or required localization for 'es_ES' in frameworks/base/core/res/res
aapt: warning: **** string 'gsm_alphabet_default_charset' has no default or required localization for 'ja_JP' in frameworks/base/core/res/res
aapt: warning: **** string 'gsm_alphabet_default_charset' has no default or required localization for 'nl_NL' in frameworks/base/core/res/res
aapt: warning: **** string 'gsm_alphabet_default_charset' has no default or required localization for 'pt_BR' in frameworks/base/core/res/res
aapt: warning: **** string 'gsm_alphabet_default_charset' has no default or required localization for 'ru_RU' in frameworks/base/core/res/res
aapt: warning: **** string 'gsm_alphabet_default_charset' has no default or required localization for 'sv_SE' in frameworks/base/core/res/res
aapt: warning: **** string 'gsm_alphabet_default_charset' has no default or required localization for 'mdpi' in frameworks/base/core/res/res
aapt: warning: **** string 'gsm_alphabet_default_charset' has no default or required localization for 'hdpi' in frameworks/base/core/res/res
aapt: warning: **** string 'gsm_alphabet_default_charset' has no default or required localization for 'nodpi' in frameworks/base/core/res/res
Notice file: frameworks/base/core/res/NOTICE -- out/target/product/g04ref/obj/NOTICE_FILES/src//system/framework/framework-res.apk.txt
Install: out/target/product/g04ref/system/framework/framework-res.apk
Install: out/target/product/g04ref/system/framework/framework.jar

你可能感兴趣的:(嵌入式linux,Android)