Android开发是一个庞大的工程,我们可以通过Android 提供的SDK实现Android软件的开发,也可以通过Android源码实现Android系统级别的开发。为此,Android SDK提供了一系列的工具来辅助开发。如下简单介绍下几个常见的Android开发工具。
aapt (Android asset packaging tool)
aapt(Android Asset Packaging Tool)即Android资源打包工具,用于建立zip兼容的包(zip、jar、apk),是一款运用于Android SDK的反汇编工具,具有apk编译、反编译、分析等功能。也可用于将资源编译到二进制的assets。
工具位置
Adnroid SDK / build-tools/<version>/aapt.ext
查看aapt版本信息
需要将build-tool
加入到path
路径中,才可通过appt
查看。
$ aapt v
aapt命令
通过 help
命令查看appt
常用的参数:
$ aapt help
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]
[-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
-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
-f force overwrite of existing files
-g specify a pixel tolerance to force images to grayscale, default 0
-j specify a jar or zip file containing classes to include
-k junk path of file(s) added
-m make package directories under location specified by -J
-u update existing packages (add new, replace older, remove deleted files)
-v verbose output
-x create extending (non-application) resource IDs
-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.
-F specify the apk file to output
-I add an existing package to base include set
-J specify where to output R.java resource constant definitions
-M specify full path to AndroidManifest.xml to include in zip
-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.
--debug-mode
inserts android:debuggable="true" in to the application node of the
manifest, making the application debuggable even on production devices.
--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).
--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.
--target-sdk-version
inserts android:targetSdkVersion in to manifest.
--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.
--custom-package
generates R.java into a different package.
--extra-packages
generate R.java for libraries. Separate libraries with ':'.
--generate-dependencies
generate dependency files in the same directories for R.java and resource package
--auto-add-overlay
Automatically add resources that are only in overlays.
--preferred-density
Specifies a preference for a particular density. Resources that do not
match this density and have variants that are a closer match are removed.
--split
Builds a separate split APK for the configurations listed. This can
be loaded alongside the base APK at runtime.
--feature-of
Builds a split APK that is a feature of the apk specified here. Resources
in the base APK can be referenced from the the feature APK.
--feature-after
An app can have multiple Feature Split APKs which must be totally ordered.
If --feature-of is specified, this flag specifies which Feature Split APK
comes before this one. The first Feature Split APK should not define
anything here.
--rename-manifest-package
Rewrite the manifest so that its package name is the package name
given here. Relative class names (for example .Foo) will be
changed to absolute names with the old package so that the code
does not need to change.
--rename-instrumentation-target-package
Rewrite the manifest so that all of its instrumentation
components target the given package. Useful when used in
conjunction with --rename-manifest-package to fix tests against
a package that has been renamed.
--product
Specifies which variant to choose for strings that have
product variants
--utf16
changes default encoding for resources to UTF-16. Only useful when API
level is set to 7 or higher where the default encoding is UTF-8.
--non-constant-id
Make the resources ID non constant. This is required to make an R java class
that does not contain the final value but is used to make reusable compiled
libraries that need to access resources.
--shared-lib
Make a shared library resource package that can be loaded by an application
at runtime to access the libraries resources. Implies --non-constant-id.
--error-on-failed-insert
Forces aapt to return an error if it fails to insert values into the manifest
with --debug-mode, --min-sdk-version, --target-sdk-version --version-code
and --version-name.
Insertion typically fails if the manifest already defines the attribute.
--error-on-missing-config-entry
Forces aapt to return an error if it fails to find an entry for a configuration.
--output-text-symbols
Generates a text file containing the resource symbols of the R class in the
specified folder.
--ignore-assets
Assets to be ignored. Default pattern is:
!.svn:!.git:!.ds_store:!*.scc:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~
--skip-symbols-without-default-localization
Prevents symbols from being generated for strings that do not have a default
localization
--no-version-vectors
Do not automatically generate versioned copies of vector XML resources.
常见命令
1、aapt l[ist] [-v] [-a] file.{zip,jar,apk}
列出压缩文件(zip,jar,apk)中的目录内容,示例命令如下:
$ aapt l app-debug.apk
2、aapt d[ump] [–values] [–include-meta-data] WHAT file.{apk} [asset [asset …]]
通过参数配置可以dump apk中各种详细信息。示例代码如下:
$ aapt dummp strings app-debug.apk
3、aapt p[ackage] [-d][-f][-m][-u][-v][-x][-z][-M AndroidManifest.xml]
android 编译资源打包资源文件的命令。参数格式如下:
-d
:包括一个或多个设备资源,由逗号分隔-f
:覆盖现有的文件命令,加上后编译生成直接覆盖目前已经存在的R.java
-m
:使生成的包的目录放在-J
参数指定的目录-u
:更新现有的包u = update
-v
:详细输出,加上此命令会在控制台输出每一个资源文件信息,R.java
生成后还有注释-x
:创建扩展资源ID
-z
:需要本地化的资源属性标记定位-M
:AndroidManifest.xml
的路径-0
:指定一个额外的扩展. apk
文件将不会存储压缩-g
:制定像素迫使图形的灰度-j
:指定包含一个jar
或zip
文件包,这个命令很特别–debug-mode
:指定的是调试模式下的编译资源–min-sdk-versopm VAL
:最小SDK
版本 如是7以上 则默认编译资源的格式是utf-8
–target-sdk-version VAL
:在androidMainfest
中的目标编译SDK
版本–app-version VAL
:应用程序版本号–app-version-name TEXT
:应该程序版本名字–custom-package VAL
:生成R.java
到一个不同的包–rename-mainifest-package PACKAGE
:修改APK
包名的选项–rename-instrumentation-target-package PACKAGE
:重写指定包名的选项–utf16
:资源编码修改为更改默认utf – 16
编码–auto-add-overlay
:自动添加资源覆盖–max-res-version
:最大资源版本-I
:指定的SDK
版本中android.jar
的路径-A
:assert
文件夹的路径-G
:一个文件输出混淆器选项,后面加文件逗号隔开-P
:指定的输出公共资源,可以指定一个文件 让资源ID
输出到那上面- –
S
:指定资源目录 一般是res
-F
:指定把资源输出到apk
文件中-J
:指定R.java
输出的路径raw-file-dir
:附加打包进APK
的文件
打包示例命令:
$
aapt package -f -S Android/sdk/platforms/android-25/android.jar -A src/main/assets -M src/main/AndroidManifest.xml -F app/out.apk
4、aapt r[emove] [-v] file.{zip,jar,apk} file1 [file2 …]
从一个zip archive文件中删除一个文件。代码示例如下:
$ aapt remove -v app.debug.apk AndroidMainfest.xml
5、aapt a[dd] [-v] file.{zip,jar,apk} file1 [file2 …]
在一个apk或者压缩包中添加一个指定的文件,格式和上类似。
6、aapt s[ingleCrunch] [-v] -i input-file -o outputfile
预处理一个文件
ADB(Android debug bridge)
adb
是Android
开发常用的工具,adb
可以辅助我们访问Android
模拟器和Android
设备的文件系统和shell环境,实现android
安装和调试等工作,也可以通过网络连接实现无线调试工作。需要注意使用之前需要将adb
加入到path
环境中。
常用命令
如下提供几个常见的adb命令。
1、安装应用
$adb install target.apk
2、连接Android设备shell环境
$ adb shell
3、传输文件
$ adb push <host_path> <target-path>
Monkey
Monkey 是Android SDK提供的用于黑盒测试的工具。它可以模拟用户动作,如:点击、拖拽、输入、滑动等。如下介绍如何使用monkey工具,monkey格式如下:
$ adb shell monkey <args>
-p
:指定要进行monkey测试的包,如果不使用参数,则会随机选择应用进行测试。-c
:指定activity的category类别。- –
v
: 指定日志级别 --ignore-crashes
:用于指定当应用程序崩溃(Crash
)时,Monkey
是否停止运行。--ignore-timeouts
:用于指定当应用程序发生无响应(ANR
)错误时,Monkey
是否停止运行。--ignore-native-crashes
:如果使用此参数,则忽略native
层代码的崩溃。--ignore-security-exceptions
:用于指定当应用程序发生许可错误时,Monkey
是否停止运行。--monitor-native-crashes
:用于指定是否监视并报告native
层发送的崩溃代码。--kill-procress-after-error
:用于指定当应用程序发生错误时,是否停止其运行。--hprof
:该选项设置后,将会在monkey
事件序列前后立刻生成report
,大小为大于5MB
,存储在/data/misc
。
TraceView
TraceView是Android SDK中提供的一个性能分析工具,traceView提供图形化的方式来跟踪了解运行程序的性能。