Android 性能优化(27)*zipalign让apk数据对齐,运行更快。
1.zipalign 简介
zipalign is an archive alignment tool that provides important optimization to Android application (.apk) files. The purpose is to ensure that all uncompressed data starts with a particular alignment relative to the start of the file. Specifically, it causes all uncompressed data within the .apk, such as images or raw files, to be aligned on 4-byte boundaries. This allows all portions to be accessed directly with mmap()
even if they contain binary data with alignment restrictions. The benefit is a reduction in the amount of RAM consumed when running the application.
google play不支持没用它的apk上线
当系统启动一个应用时,如果它的apk是内存4字节对齐的,它可以直接通过mmap()函数访问资源和数据,比不对齐的快。zipalign 就是优化apk,让其按4字节对齐的工具。
This tool should always be used to align your .apk file before distributing it to end-users. The Android build tools can handle this for you. Android Studio automatically aligns your .apk after it signs it with your private key. The build scripts used when compiling your application with Gradle also align your .apk, as long as you have provided the path to your keystore and the key alias in your project gradle.properties
file, so that the build tools can sign the package first.
Caution: zipalign must only be performed after the .apk file has been signed with your private key. If you perform zipalign before signing, then the signing procedure will undo the alignment. Also, do not make alterations to the aligned package. Alterations to the archive, such as renaming or deleting entries, will potentially disrupt the alignment of the modified entry and all later entries. And any files added to an "aligned" archive will not be aligned.
本工具应该用在用私钥签名之后使用,否则无效。一但用zipalign优化后,就不要改变,否则会导致对齐失效。
The adjustment is made by altering the size of the "extra" field in the zip Local File Header sections. Existing data in the "extra" fields may be altered by this process.
工具是通过修改zip文件的header section 中的extra属性来实现对齐的。
For more information about how to use zipalign when building your application, please read Signing Your Application.
2.zipalign工具位置
android-sdks/build-tools/xx xx为版本号,每个目录下都有。
3.Usage 示例
To align infile.apk
and save it as outfile.apk
: 对已存在的apk进行对齐优化。
$zipalign [-f] [-v] <alignment> infile.apk outfile.apk
To confirm the alignment of existing.apk
: 对已经存在的apk进程对齐优化检测,验证。注意-c参数。
$zipalign -c -v <alignment> existing.apk
The <alignment>
is an integer that defines the byte-alignment boundaries. This must always be 4 (which provides 32-bit alignment) or else it effectively does nothing.
<alignment>通常值为4.
Flags:
zipalign 各参数的含义:
-f
: overwrite existing outfile.zip-v
: verbose output-c
: confirm the alignment of the given file
如:
$~/Android/android-sdks/build-tools/23.0.2/zipalign -f -v 4 app-debug.apk app-zipaligned.apk
$~/Android/android-sdks/build-tools/23.0.2/zipalign -c -v 4 app-zipaligned.apk
在终端下输出如下:
1 Verifying alignment of app-zipaligned.apk (4)... 2 53 AndroidManifest.xml (OK - compressed) 3 1388 res/drawable-hdpi-v11/__leak_canary_notification.png (OK) 4 2416 res/drawable-hdpi-v4/__leak_canary_icon.png (OK) 5 4744 res/drawable-hdpi-v4/__leak_canary_notification.png (OK) 6 5824 res/drawable-hdpi-v4/ab_overflow_btn.png (OK) 7 6136 res/drawable-hdpi-v4/corner_notice_new.png (OK) 8 7516 res/drawable-hdpi-v4/fast_scroll_preview_right.png (OK) 9 9488 res/drawable-hdpi-v4/fast_scroll_thumb_aje.png (OK) 10 9936 res/drawable-hdpi-v4/ic_launcher.png (OK) 11 13200 res/drawable-hdpi-v4/pgrsb.png (OK) 12 15132 res/drawable-hdpi-v4/refreshview_icon.png (OK) 13 15656 res/drawable-hdpi-v4/session_list_item_classes.png (OK) 14 25380 res/drawable-hdpi-v4/session_list_item_content_icon.png (OK) 15 26708 res/drawable-hdpi-v4/session_list_item_file_helper.png (OK) 16 27540 res/drawable-hdpi-v4/session_list_item_icon.png (OK) 17 28764 res/drawable-hdpi-v4/session_list_item_qq_news.png (OK) 18 31560 res/drawable-hdpi-v4/session_list_item_subscribe.png (OK) 19 32156 res/drawable-hdpi-v4/session_list_item_voice_notepad.png (OK) 20 33136 res/drawable-hdpi-v4/splash_bg.jpg (OK) 21 143168 res/drawable-hdpi-v4/tab_contaces_new_friend.png (OK) 22 144252 res/drawable-hdpi-v4/tab_contact_icon.png (OK) 23 145484 res/drawable-hdpi-v4/tab_contacts_cluster_chat.png (OK) 24 146800 res/drawable-hdpi-v4/tab_contacts_l.png (OK) 25 148096 res/drawable-hdpi-v4/tab_contacts_n.png (OK) 26 149944 res/drawable-hdpi-v4/tab_contacts_public_id.png (OK) 27 151240 res/drawable-hdpi-v4/tab_contacts_tags.png (OK) 28 152380 res/drawable-hdpi-v4/tab_discovery_fjr.png (OK) 29 153028 res/drawable-hdpi-v4/tab_discovery_gw.png (OK) 30 153680 res/drawable-hdpi-v4/tab_discovery_l.png (OK) 31 155392 res/drawable-hdpi-v4/tab_discovery_n.png (OK) 32 157728 res/drawable-hdpi-v4/tab_discovery_plp.png (OK) 33 158392 res/drawable-hdpi-v4/tab_discovery_pyq.png (OK) 34 159392 res/drawable-hdpi-v4/tab_discovery_sys.png (OK) 35 159932 res/drawable-hdpi-v4/tab_discovery_yx.png (OK) 36 160704 res/drawable-hdpi-v4/tab_discovery_yyy.png (OK) 37 161840 res/drawable-hdpi-v4/tab_personal_2wm.png (OK) 38 162100 res/drawable-hdpi-v4/tab_personal_bq.png (OK) 39 162868 res/drawable-hdpi-v4/tab_personal_l.png (OK) 40 163960 res/drawable-hdpi-v4/tab_personal_n.png (OK) 41 165668 res/drawable-hdpi-v4/tab_personal_qb.png (OK) 42 166124 res/drawable-hdpi-v4/tab_personal_sc.png (OK) 43 166896 res/drawable-hdpi-v4/tab_personal_sz.png (OK) 44 167724 res/drawable-hdpi-v4/tab_personal_xc.png (OK) 45 168164 res/drawable-hdpi-v4/tab_session_l.png (OK) 46 169524 res/drawable-hdpi-v4/tab_session_n.png (OK) 47 171604 res/drawable-hdpi-v4/tab_session_network_warning.png (OK) 48 172424 res/drawable-hdpi-v4/tab_session_qq_friend.png (OK) 49 174968 res/drawable-mdpi-v11/__leak_canary_notification.png (OK) 50 175728 res/drawable-mdpi-v4/__leak_canary_icon.png (OK) 51 177232 res/drawable-mdpi-v4/__leak_canary_notification.png (OK) 52 177716 res/drawable-mdpi-v4/ic_launcher.png (OK) 53 179700 res/drawable-xhdpi-v11/__leak_canary_notification.png (OK) 54 180952 res/drawable-xhdpi-v4/__leak_canary_icon.png (OK) 55 184060 res/drawable-xhdpi-v4/__leak_canary_notification.png (OK) 56 185460 res/drawable-xxhdpi-v11/__leak_canary_notification.png (OK) 57 187152 res/drawable-xxhdpi-v4/__leak_canary_icon.png (OK) 58 191840 res/drawable-xxhdpi-v4/__leak_canary_notification.png (OK) 59 193116 res/drawable-xxhdpi-v4/ic_launcher.png (OK) 60 198832 res/drawable-xxxhdpi-v11/__leak_canary_notification.png (OK) 61 200908 res/drawable-xxxhdpi-v4/__leak_canary_icon.png (OK) 62 207196 res/drawable-xxxhdpi-v4/__leak_canary_notification.png (OK) 63 208837 res/drawable/__leak_canary_toast_background.xml (OK - compressed) 64 209153 res/drawable/selector_dialog_bg.xml (OK - compressed) 65 209616 res/drawable/tab_contacts_state.xml (OK - compressed) 66 209947 res/drawable/tab_discovery_state.xml (OK - compressed) 67 210282 res/drawable/tab_personal_state_list.xml (OK - compressed) 68 210621 res/drawable/tab_session_list_item_layer.xml (OK - compressed) 69 210949 res/drawable/tab_session_state.xml (OK - compressed) 70 211283 res/layout/__leak_canary_display_leak.xml (OK - compressed) 71 211814 res/layout/__leak_canary_heap_dump_toast.xml (OK - compressed) 72 212314 res/layout/__leak_canary_leak_row.xml (OK - compressed) 73 212807 res/layout/__leak_canary_ref_row.xml (OK - compressed) 74 213383 res/layout/__leak_canary_ref_top_row.xml (OK - compressed) 75 213760 res/layout/aty_main.xml (OK - compressed) 76 214060 res/layout/aty_main_large.xml (OK - compressed) 77 214425 res/layout/aty_main_large_land.xml (OK - compressed) 78 214787 res/layout/aty_main_sw600dp.xml (OK - compressed) 79 215185 res/layout/aty_main_sw600dp_land.xml (OK - compressed) 80 215577 res/layout/aty_main_xlarge.xml (OK - compressed) 81 215965 res/layout/frgmt_main.xml (OK - compressed) 82 216517 res/layout/frgmt_splash.xml (OK - compressed) 83 216849 res/layout/tab_session_list_item.xml (OK - compressed) 84 217552 res/layout/tab_widget_layout.xml (OK - compressed) 85 217953 res/menu/main_menu.xml (OK - compressed) 86 218380 resources.arsc (OK) 87 238913 classes.dex (OK - compressed) 88 852364 pinyindb/pinyin_gwoyeu_mapping.xml (OK - compressed) 89 859683 pinyindb/pinyin_mapping.xml (OK - compressed) 90 865435 pinyindb/unicode_to_hanyu_pinyin.txt (OK - compressed) 91 959902 META-INF/MANIFEST.MF (OK - compressed) 92 962847 META-INF/CERT.SF (OK - compressed) 93 965810 META-INF/CERT.RSA (OK - compressed) 94 Verification succesful