41.Android aapt工具

41.Android aapt工具


aapt 介紹

可參考官方文檔: http://www.androidcn.net/wiki/index.php/Reference/aapt

aapt stands for Android Asset Packaging Tool and is included in the tools/ directory of the SDK. This tool allows you to view, create, and update Zip-compatible archives (zip, jar, apk). It can also compile resources into binary assets.

Though you probably won’t often use aapt directly, build scripts and IDE plugins can utilize this tool to package the apk file that constitutes an Android application.

For more usage details, open a terminal, go to the tools/ directory, and run the command:

Linux or Mac OS X:
./aapt
Windows:
aapt.exe

aapt 爲 Android Asset Packaging Tool , 在SDK的tools/目錄下. 該工具可以查看, 創建, 更新ZIP格式的文檔附件(zip, jar, apk). 也可將資源文件編譯成二進制文件。

儘管你可能沒有直接使用過aapt工具, 但是build scripts和IDE插件會使用這個工具打包apk文件構成一個Android 應用程序.

獲取更多的實用信息, 請打開終端控制檯, 到tools/目錄下, 執行命令:
Linux or Mac OS X:
./aapt
Windows:
aapt.exe


aapt 環境配置

aapt工具位於:sdk/buid-tools/android-[version]/aapt

  • Windows環境的話,可以直接在環境變量裏配置一個PATH,指定到sdk/buid-tools/android-[version]即可。

  • Mac環境的話,我用的是Oh My Zsh,所以直接vi ~/.zshrc,以下是我配置:

# Add Android build-tools AAPT variable
AAPT_HOME=/Users/CaMnter/Android/adt-bundle-mac-x86_64-20140702/sdk/build-tools/23.0.2
export AAPT_HOME
export PATH=$PATH:$AAPT_HOME

然後在命令行下輸入:aapt。
aapt_show

可以看到 aapt 的所有命令,和命令對應有哪些參數。


aapt list

aapt l[ist] [-v] [-a] file.{zip,jar,apk}
List contents of Zip-compatible archive.

  • 查看apk目錄:
    aapt l *.apk
    aapt_l

  • 查看apk目錄,以表格形式輸出:
    aapt l -v *.apk
    Method : 壓縮形式 , Deflate 或 Stored 。
    Ratio : 壓縮率
    CRC-32 : 循環冗餘校驗
    aapt_l_-v

  • 詳細查看apk目錄:
    aapt l -a *.apk
    aapt_l_-a


aapt dump

aapt d[ump] [–values] [–include-meta-data] WHAT file.{apk} [asset [asset …]]
strings Print the contents of the resource table string pool in the APK.
badging Print the label and icon for the app declared in APK.
permissions Print the permissions from the APK.
resources Print the resource table from the APK.
configurations Print the configurations in the APK.
xmltree Print the compiled xmls in the given assets.
xmlstrings Print the strings of the given compiled xml assets.

  • 查看apk:package、sdkVersion、targetSdkVersion、application-label、launchable-activity、feature-group等信息:
    aapt d badging *.apk
    aapt_d_badging

  • 查看apk權限:
    aapt d permissions *.apk
    aapt_d_permissions

  • 查看apk資源:
    aapt d resources *.apk
    aapt_d_resources

  • 查看apk配置:
    aapt d configurations *.apk
    aapt_d_configurations

  • 查看xml的樹形結構:
    aapt d xmltree *.apk res/*.xml
    aapt_d_xmltree

  • 查看xml中所有的string:
    aapt d xmlstrings *.apk res/*.xml
    aapt_d_xmlstrings

  • 查看信息後輸出文件:
    aapt d ... >*.txt
    aapt_d_log_1
    查看輸出:
    aapt_d_log_2


aapt package

  • 生成R.java:
    先定位到項目目錄下
    aapt package -m -J ~/temp/ -S app/src/main/res -I /Users/CaMnter/Android/adt-bundle-mac-x86_64-20140702/sdk/platforms/android-23/android.jar -M /Users/CaMnter/GitHub/AndroidLife/app/src/main/AndroidManifest.xml
    -J ~/temp/ : 設置輸出目錄
    -S app/src/main/res: 資源文件res目錄
    -I /Users/CaMnter/Android/adt-bundle-mac-x86_64-20140702/sdk/platforms/android-23/android.jar: android.jar路徑
    **-M /Users/CaMnter/GitHub/AndroidLife/app/src/main/AndroidManifest.xml:**AndroidManifest絕對路徑

  • 生成資源文件
    還是先定位到項目目錄下
    aapt package -f -M /Users/CaMnter/GitHub/AndroidLife/app/src/main/AndroidManifest.xml -S app/src/main/res -A app/src/main/assets -I /Users/CaMnter/Android/adt-bundle-mac-x86_64-20140702/sdk/platforms/android-23/android.jar -F ~/log/resouces
    **-M /Users/CaMnter/GitHub/AndroidLife/app/src/main/AndroidManifest.xml:**AndroidManifest絕對路徑
    -S app/src/main/res: 資源文件res目錄
    -A app/src/main/assets: assets目錄
    -I /Users/CaMnter/Android/adt-bundle-mac-x86_64-20140702/sdk/platforms/android-23/android.jar: android.jar路徑
    -F ~/log/resouces: resouces.arsc存放路徑


其他命令

輸出aapt版本

aapt v[ersion]
Print program version.


刪除apk中指定文件

aapt r[emove] [-v] file.{zip,jar,apk} file1 [file2 …]
Delete specified files from Zip-compatible archive.


添加指定文件到apk中

aapt a[dd] [-v] file.{zip,jar,apk} file1 [file2 …]
Add specified files to Zip-compatible archive.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章