Android AAPT详解

 

 

AAPT是什么

AAPT - Android Asset Packaging Tool, 看全称,就可知道AAPT是Android资源打包工具。在讲这个之前,是有必要简单说下Android是如何构建一个APK的。

上图是Google官方发布的一张非常经典的Apk打包流程图。

流程概述:

  1. 工程的资源文件(res文件夹下的文件),通过AAPT打包成R.java类(资源索引表),以及.arsc资源文件
  2. 如果有aidl,通过aidl工具,打包成java接口类
  3. R.java和aidl.java通过java编译成想要的.class文件。
  4. 源码class文件和第三方jar或者library通过dx工具打包成dex文件。dx工具的主要作用是将java字节码转换成Dalvik字节码,在此过程中会压缩常量池,消除一些冗余信息等。
  5. apkbuilder工具会将所有没有编译的资源,.arsc资源,.dex文件打包到一个完成apk文件中中。
  6. 签名,5中完成apk通过配置的签名文件(debug和release都有),jarsigner工具会对齐签名。得到一个签名后的apk,signed.apk
  7. zipAlign工具对6中的signed.apk进行对齐处理,所谓对齐,主要过程是将APK包中所有的资源文件距离文件起始偏移为4字节整数倍,这样通过内存映射访问apk文件时的速度会更快。对齐的作用主要是为了减少运行时内存的使用。

总结:

  • 输入:res文件夹所有的资源(layout\drawable\string\array等),asset下的资源,AndroidManifest.xml,Android.jar文件
  • 工具: aapt 地址(/sdk path/build-tools/build tools version/aapt)
  • 输出:res下的资源都会被编译成一个资源索引文件resource.arsc以及一个R.java类。asset下的资源不会编译,直接压缩进apk。

 

AAPT命令详解

按照上面aapt的地址配置好环境变量后,在终端中输入 aapt v 会得到aapt版本信息,如下:

输入 aapt 命令会列出所有的aapt命令集合:如下

Android Asset Packaging Tool

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

 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.

 aapt p[ackage] [-d][-f][-m][-u][-v][-x][-z][-M AndroidManifest.xml] \
        [-0 extension [-0 extension ...]] [-g tolerance] [-j jarfile] \
        [--debug-mode] [--min-sdk-version VAL] [--target-sdk-version VAL] \
        [--app-version VAL] [--app-version-name TEXT] [--custom-package VAL] \
        [--rename-manifest-package PACKAGE] \
        [--rename-instrumentation-target-package PACKAGE] \
        [--utf16] [--auto-add-overlay] \
        [--max-res-version VAL] \
        [-I base-package [-I base-package ...]] \
        [-A asset-source-dir]  [-G class-list-file] [-P public-definitions-file]\
        [-D main-dex-class-list-file] \
        [-S resource-sources [-S resource-sources ...]] \
        [-F apk-file] [-J R-file-dir] \
        [--product product1,product2,...] \
        [-c CONFIGS] [--preferred-density DENSITY] \
        [--split CONFIGS [--split CONFIGS]] \
        [--feature-of package [--feature-after package]] \
        [raw-files-dir [raw-files-dir] ...] \
        [--output-text-symbols DIR]

   Package the android resources.  It will read assets and resources that are supplied with the -M -A -S or raw-files-dir arguments.  The -J -P -F and -R options control which files are output.

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

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

 aapt c[runch] [-v] -S resource-sources ... -C output-folder ...
   Do PNG preprocessing on one or several resource folders and store the results in the output folder.

 aapt s[ingleCrunch] [-v] -i input-file -o outputfile
   Do PNG preprocessing on a single file.

 aapt v[ersion]
   Print program version.

 Modifiers:
   -a  print Android-specific data (resources, manifest) when listing
       列出时打印Android特定数据(资源、清单)
   -c  specify which configurations to include.  The default is all configurations.  The value of the parameter should be a comma separated list of configuration values.  Locales should be specified as either a language or language-region pair.  Some examples:
	   指定要包含的配置。默认为所有配置。参数的值应该是配置值的逗号分隔列表。区域设置应指定为语言或语言区域对。一些例子:
		en
		port,en
		port,land,en_US
   -d  one or more device assets to include, separated by commas
       要包含的一个或多个设备assets,用逗号分隔
   -f  force overwrite of existing files
       强制重写现有文件
   -g  specify a pixel tolerance to force images to grayscale, default 0
       指定像素公差以强制图像灰度化,默认值为0
   -j  specify a jar or zip file containing classes to include
       指定包含要包含的类的jar或zip文件
   -k  junk path of file(s) added
       添加的文件垃圾的路径
   -m  make package directories under location specified by -J
       在-J指定的位置下生成包目录
   -u  update existing packages (add new, replace older, remove deleted files)
       更新现有包(添加新的,替换旧的,删除已删除的文件)
   -v  verbose output
       详细输出
   -x  create extending (non-application) resource IDs
       创建扩展(非应用程序)资源ID
   -z  require localization of resource attributes marked with localization="suggested"
       要求本地化标记为“建议”的资源属性
   -A  additional directory in which to find raw asset files
       查找原始资产文件的附加目录
   -G  A file to output proguard options into.
       将proguard选项输出到的文件。
   -D  A file to output proguard options for the main dex into.
       将主索引的proguard选项输出到的文件。
   -F  specify the apk file to output
       指定要输出的apk文件
   -I  add an existing package to base include set
       将现有包添加到基包含集合
   -J  specify where to output R.java resource constant definitions
       指定输出R.java资源常量定义的位置
   -M  specify full path to AndroidManifest.xml to include in zip
       指定要包含在zip中的AndroidManifest.xml的完整路径
   -P  specify where to output public resource definitions
       指定输出公共资源定义的位置
   -S  directory in which to find resources.  Multiple directories will be scanned and the first match found (left to right) will take precedence.
       查找资源的目录。将扫描多个目录,找到的第一个匹配项(从左到右)将优先。
   -0  specifies an additional extension for which such files will not be stored compressed in the .apk.  An empty string means to not compress any files at all.
       指定一个附加扩展名,对于该扩展名,此类文件将不会压缩存储在.apk中。空字符串意味着根本不压缩任何文件。
   --debug-mode
       inserts android:debuggable="true" in to the application node of the manifest, making the application debuggable even on production devices.
       将android:debuggable=“true”插入清单的应用程序节点,使应用程序即使在生产设备上也可以调试。
   --include-meta-data
       when used with "dump badging" also includes meta-data tags.
	   与“转储标记”一起使用时,还包括元数据标记。
   --pseudo-localize
       generate resources for pseudo-locales (en-XA and ar-XB).
	   为伪区域设置(en-XA和ar-XB)生成资源。
   --min-sdk-version
       inserts android:minSdkVersion in to manifest.  If the version is 7 or higher, the default encoding for resources will be in UTF-8.
	   Inserts Android:如果版本为7或更高,则资源编码将为UTF-8。
   --target-sdk-version
       inserts android:targetSdkVersion in to manifest.
       在清单中插入android:targetSdkVersion。
   --max-res-version
       ignores versioned resource directories above the given value.
	   忽略高于给定值的版本化资源目录。
   --values
       when used with "dump resources" also includes resource values.
	   与“转储资源”一起使用时,还包括资源值。
   --version-code
       inserts android:versionCode in to manifest.
   --version-name
       inserts android:versionName in to manifest.
   --replace-version
       If --version-code and/or --version-name are specified, these values will replace any value already in the manifest. By default, nothing is changed if the manifest already defines these attributes.
	   如果指定了--version code和/或--version name,则这些值将替换清单中已存在的任何值。默认情况下,如果清单已定义这些属性,则不会更改任何内容。
   --custom-package
       generates R.java into a different package.
	   将R.java生成到不同的包中。
   --extra-packages
       generate R.java for libraries. Separate libraries with ':'.
	   为库生成R.java。用“:”分隔库。
   --generate-dependencies
       generate dependency files in the same directories for R.java and resource package
	   在R.java和资源包的相同目录中生成依赖文件
   --auto-add-overlay
       Automatically add resources that are only in overlays.
	   自动添加仅在覆盖中的资源。
   --preferred-density
       Specifies a
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值