在前面《Android学习第一天-adb经常使用命令》和 《Android学习次日-android经常使用命令》两篇博文中,咱们重点讲解了adb和android的经常使用命令,下面咱们讲解一下打包的时候经常使用的命令吧html
通常状况下,咱们使用这个命令进行模拟器的开启比较多java
模拟器的开启方式以下:android
emulator -avd <avd_name> [<options>]
或者
emulator @<avd_name>
通常状况下咱们都是直接打开便可,具体的options能够参考http://developer.android.com/tools/help/emulator.html,在此不作重复说明windows
Android Asset Packaging Tool,简称aapt,经过aapt,咱们能够对打包的资源进行管理,包括查看,增长,删除。app
aapt l[ist] [] <path_to_apk>
eg:
aapt l xiaoxuetu-debug.apk
终端的执行结果是:工具
Usage: aapt l[ist] [-v] [-a] file.{zip,jar,apk} List contents of Zip-compatible archive. aapt d[ump] [--values] WHAT file.{apk} [asset [asset ...]] 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 dump permissions xiaoxuetu-debug.apk
终端的执行结果:ui
由于该项目并无使用到权限,因此也就显示为空了。编码
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-configurations CONFIGS] \ [raw-files-dir [raw-files-dir] ...] \ [--output-text-symbols DIR]
上面的参数有点多,下面给出一些经常使用参数的简单说明吧:spa
-f 若是编译出来的文件已经存在,强制覆盖
-m 使生成的包的目录存放在-J参数指定的目录
-J 指定生成的R.java 的输出目录
-S res文件夹路径
-A assert文件夹路径
-I 某个版本平台的android.jar的路径
-F 具体指定APK文件的输出
-J R.java文件的生成目录
在AAPT中经常使用的命令格式是:
aapt p -M <Android应用清单的路径> -I <额外添加的包> -F <目标文件的路径>
-I 额外添加的包必须包含android.jar这个jar包
下面举一个例子:
aapt p -S res -M AndroidManifest.xml -I D:\Java\adt-bundle-windows\sdk\platforms\android-10\android.jar -F ./xiaoxuetu.apk
终端执行结果:
固然,这里打出来的apk并非完整可用的,真正的apk打包还须要通过dex的编译等流程。
aapt r[emove] [-v] file.{zip,jar,apk} file1 [file2 ...]
Delete specified files from Zip-compatible archive.
首先,咱们查看下前面打包的xiaoxuetu.apk里面的内容
下面若是咱们要删除里面的AnroidManifest.xml文件,执行下面命令:
aapt r xiaoxuetu.apk AndroidManifest.xml
终端的执行结果:
什么都没有输出表示已经删除成功了,咱们再来查看下该apk中的AnroidManifest.xml是否是真的已经被删除了:
是的,咱们再也看不到AndroidManifest.xml。说明已经删除成功了。
aapt a[dd] [-v] file.{zip,jar,apk} file1 [file2 ...]
Add specified files to Zip-compatible archive.
假设咱们要往xiaoxuetu.apk中加回AndroidManifest.xml文件,咱们继续执行:
aapt a xiaoxuetu.apk AndroidManifest.xml
终端的执行结果是:
咱们再来查看xiaoxuetu.apk中的内容发现AndroidManifest.xml又回来啦:
aapt v
终端的执行结果:
首先咱们来说解下javac的用法:
用法: javac <options> <source files> 其中, 可能的选项包括: -g 生成全部调试信息 -g:none 不生成任何调试信息 -g:{lines,vars,source} 只生成某些调试信息 -nowarn 不生成任何警告 -verbose 输出有关编译器正在执行的操做的消息 -deprecation 输出使用已过期的 API 的源位置 -classpath <路径> 指定查找用户类文件和注释处理程序的位置 -cp <路径> 指定查找用户类文件和注释处理程序的位置 -sourcepath <路径> 指定查找输入源文件的位置 -bootclasspath <路径> 覆盖引导类文件的位置 -extdirs <目录> 覆盖所安装扩展的位置 -endorseddirs <目录> 覆盖签名的标准路径的位置 -proc:{none,only} 控制是否执行注释处理和/或编译。 -processor <class1>[,<class2>,<class3>...] 要运行的注释处理程序的名称; 绕过默认的搜索进程 -processorpath <路径> 指定查找注释处理程序的位置 -d <目录> 指定放置生成的类文件的位置 -s <目录> 指定放置生成的源文件的位置 -implicit:{none,class} 指定是否为隐式引用文件生成类文件 -encoding <编码> 指定源文件使用的字符编码 -source <发行版> 提供与指定发行版的源兼容性 -target <发行版> 生成特定 VM 版本的类文件 -version 版本信息 -help 输出标准选项的提要 -A关键字[=值] 传递给注释处理程序的选项 -X 输出非标准选项的提要 -J<标记> 直接将 <标记> 传递给运行时系统 -Werror 出现警告时终止编译 @<文件名> 从文件读取选项和文件名
经过上面的选项能够知道在编译过程当中:
1) 因为咱们的Android编码使用的是UTF-8,因此须要指定 -encoding UTF-8
2) 因为咱们还须要指定生成特定VM版本的类文件,因此咱们须要制定 -target 为1.7
3) 因为咱们编译的是Android项目,因此须要另外Android的jar包,才能在编译过程当中,避免引入错误,所以须要用到 -bootclasspath选项,引入响应android版本的jar包。
4) 因为咱们须要制定编译后的class文件的输出位置,因此咱们还须要用到 -d这个选项
所以,通常状况下咱们编译一个安卓项目中的class文件的命令格式为:
javac -encoding <源文件使用的编码格式> -target <VM版本> -bootclasspath <覆盖引导类文件的位置> -d <指定放置生成的类文件的位置> <须要编译的java文件, 格式是: file1, file2>
下面举一个例子,直接编译xiaoxuetu这个Android项目的java文件:
javac -encoding UTF-8 -source 1.6 -target 1.6 -bootclasspath D:\Java\adt-bundle-windows\sdk\platforms\android-10\android.jar -d E:\Workspaces\android\xiaoxuetu\xbin E:\Workspaces\android\xiaoxuetu\src\com\xiaoxuetu\*.java E:\Workspaces\android\xiaoxuetu\gen\com\xiaoxu etu\*.java
终端的执行结果是:
咱们在来看看xbin目录下,编译好的class文件都存在了:
相信你们都知道,在Android平台中,使用的是并无遵循JVM规范的Dalvik虚拟机,他并不直接运行java二进制文件,而是运行它特有的*.dex文件,所以咱们须要经过DX工具将Android应用的*.class文件转换为*.dex文件 .
dex工具的经常使用命令格式以下
dx --dex --dump-to <须要编译到的路径> --core-library <须要编译的java文件所在的路径>
注意:<须要编译到的路径> 和 <须要编译的java文件所在的路径> 必须是全路径,不然他是以dx.bat所在的路径为相对路径进行查找和编译的
下面举一个例子,假设我要把前面编译的class文件转换成*.dex文件,须要执行命令:
dx --dex --dump-to E:\Workspaces\android\xiaoxuetu\xbin\classes.dex --core-library E:\Workspaces\android\xiaoxuetu\xbin
终端的执行结果:
再到E:\Workspaces\android\xiaoxuetu\xbin目录下咱们就会找到classes.dex这个文件出来了。
缘由:这是由于咱们写了相对路径,假设dx.bat所在的路径是D:\android_dev_tools/platform_tools下,那么在编译的时候,它将会到 D:\android_dev_tools/platform_tools 目录去寻找xbin文件夹,因此找不到xbin文件夹
解决方法:像示例中那样使用绝对路径
缘由:通过谷歌,在Stackflow的解释为:
Your classes need to be compiled with jdk 5 or 6. You are probably using 7. If that doesn't work, you might also need to make sure that every single class is compiled right, even if in different projects. Solved my problem here.
解决方法:在javac编译的时候,经过source和target指定版本为1.6,详情见示例。
这个工具用于将各类资源打包成一个Android应用。它的使用方法是:
Usage: apkbuilder <out archive> [-v][-u][-storetype STORE_TYPE] [-z inputzip] [-f inputfile] [-rf input-folder] [-rj -input-path] -v Verbose. -d Debug Mode: Includes debug files in the APK file. -u Creates an unsigned package. -storetype Forces the KeyStore type. If ommited the default is used. -z Followed by the path to a zip archive. Adds the content of the application package. -f Followed by the path to a file. Adds the file to the application package. -rf Followed by the path to a source folder. Adds the java resources found in that folder to the application package, while keeping their path relative to the source folder. -rj Followed by the path to a jar file or a folder containing jar files. Adds the java resources found in the jar file(s) to the application package. -nf Followed by the root folder containing native libraries to include in the application package.
下面讲解下经常使用的命令吧:
-rf 参照源文件的目录的结构
-u 建立一个没有签名的包
-z 将某个压缩文件添加到包中
-f 将某个文件添加到保重
那么经常使用的apkbuilder命令格式就是:
apkbuilder <APK输出路径> -u -z <须要添加的压缩资源包> -f <须要添加的文件> -rf <源文件所在的目录>
注意:<须要编译到的路径> 和 <须要编译的java文件所在的路径> 必须是全路径
下面举个例子吧:
apkbuilder E:\Workspaces\android\xiaoxuetu\xbin\xiaoxuetu.apk -u -z E:\Workspaces\android\xiaoxuetu\xbin\xiaoxuetu.ap_ -f E:\Workspaces\android\xiaoxuetu\xbin\classes.dex -rf E:\Workspaces\android\xiaoxuetu\src
终端的执行结果是:
同时在E:\Workspaces\android\xiaoxuetu\xbin\咱们能够打包好的apk文件了。
注意:本文主要是讲解打包会用到的经常使用命令的使用方法,并非说根据这些步骤就可以打出一个可用的包,具体一个详细的打包流程,在后续的博文中会讲到,敬请期待。谢谢。