• Android应用程序(APK)的编译打包过程


    流程图:
     
     
    我们重点关心的是(1)这个过程的输入是什么?(2)这个过程的输出是什么?(3)这个过程使用了什么工具?至于使用什么参数,可以自己去看对应命令的帮助文件,或者在网上搜索,这不是本文的重点。
    aapt->
    aidl -> javac-> dx(dex)-> apkbuilder-> jarsigner-> zipalign
     
    步骤中提到的工具如下表:
    名称 功能介绍 在操作系统中的路径
    aapt Android资源打包工具 ${ANDROID_SDK_HOME}/platform-tools/appt
    aidl Android接口描述语言转化为.java文件的工具 ${ANDROID_SDK_HOME}/platform-tools/aidl
    javac Java Compiler ${JDK_HOME}/javac或/usr/bin/javac
    dex 转化.class文件为Davik VM能识别的.dex文件 ${ANDROID_SDK_HOME}/platform-tools/dx
    apkbuilder 生成apk包 ${ANDROID_SDK_HOME}/tools/opkbuilder
    jarsigner .jar文件的签名工具 ${JDK_HOME}/jarsigner或/usr/bin/jarsigner
    zipalign 字节码对齐工具 ${ANDROID_SDK_HOME}/tools/zipalign
    第一步:打包资源文件,生成R.java文件
    编译R.java类需要用到AndroidSDK提供的aapt工具,aapt参数众多,以下是主要参数:
    1. -d  one or more device assets to include, separated by commas  
    2.  -f  force overwrite of existing files  
    3.  -g  specify a pixel tolerance to force images to grayscale, default 0  
    4.  -j  specify a jar or zip file containing classes to include  
    5.  -k  junk path of file(s) added  
    6.  -m  make package directories under location specified by -J  
    7.  -u  update existing packages (add new, replace older, remove deleted files)  
    8.  -v  verbose output  
    9.  -x  create extending (non-application) resource IDs  
    10.  -z  require localization of resource attributes marked with  
    11.      localization="suggested"  
    12.  -A  additional directory in which to find raw asset files  
    13.  -G  A file to output proguard options into.  
    14.  -F  specify the apk file to output  
    15.  -I  add an existing package to base include set  
    16.  -J  specify where to output R.java resource constant definitions  
    17.  -M  specify full path to AndroidManifest.xml to include in zip  
    18.  -P  specify where to output public resource definitions  
    19.  -S  directory in which to find resources.  Multiple directories will be scann  
     
    aapt编译R.java文件具体如下:
    需要进入应用程序目录,新建一个gen目录,没有gen目录,命令将会出现找不到文件的错误!
    命令成功执行后将会在gen目录下生成成包结构的目录树,及R.java文件!
     
    列子:
     
     
     
    第二步:处理AIDL文件,生成对应的.java文件(当然,有很多工程没有用到AIDL,那这个过程就可以省了)
     
    将.aidl文件生成.java文件需要用到AndroidSDK自带的aidl工具,此工具具体参数如下:
    1. -I<DIR>    search path for import statements.  
    2. -d<FILE>   generate dependency file.  
    3. -p<FILE>   file created by --preprocess to import.  
    4. -o<FOLDER> base output folder for generated files.  
    5. -b         fail when trying to compile a parcelable.  
    6. 值得注意的是:这个工具的参数与参数值之间不能有空格,Google也有对工资不满意的工程师! 
    例子:
     
     
     
    第三步:编译Java文件,生成对应的.class文件
     
    javac命令用法如下:
    1. 其中,可能的选项包括:  
    2.   -g                         生成所有调试信息  
    3.   -g:none                    不生成任何调试信息  
    4.   -g:{lines,vars,source}     只生成某些调试信息  
    5.   -nowarn                    不生成任何警告  
    6.   -verbose                   输出有关编译器正在执行的操作的消息  
    7.   -deprecation               输出使用已过时的 API 的源位置  
    8.   -classpath <路径>            指定查找用户类文件和注释处理程序的位置  
    9.   -cp <路径>                   指定查找用户类文件和注释处理程序的位置  
    10.   -sourcepath <路径>           指定查找输入源文件的位置  
    11.   -bootclasspath <路径>        覆盖引导类文件的位置  
    12.   -extdirs <目录>              覆盖安装的扩展目录的位置  
    13.   -endorseddirs <目录>         覆盖签名的标准路径的位置  
    14.   -proc:{none,only}          控制是否执行注释处理和/或编译。  
    15.   -processor <class1>[,<class2>,<class3>...]要运行的注释处理程序的名称;绕过默认的搜索进程  
    16.   -processorpath <路径>        指定查找注释处理程序的位置  
    17.   -d <目录>                    指定存放生成的类文件的位置  
    18.   -s <目录>                    指定存放生成的源文件的位置  
    19.   -implicit:{none,class}     指定是否为隐式引用文件生成类文件  
    20.   -encoding <编码>             指定源文件使用的字符编码  
    21.   -source <版本>               提供与指定版本的源兼容性  
    22.   -target <版本>               生成特定 VM 版本的类文件  
    23.   -version                   版本信息  
    24.   -help                      输出标准选项的提要  
    25.   -Akey[=value]              传递给注释处理程序的选项  
    26.   -X                         输出非标准选项的提要  
    27.   -J<标志>                     直接将 <标志> 传递给运行时系统  
     
    例子:
     
    javac -encoding utf-8 -target 1.5 -bootclasspath E:Androiddevandroid-sdk-windows2.2platformsandroid-3android.jar -d bin srccomyread eader*.java gencomyread eaderR.java 
     
     
    第四步:把.class文件转化成Davik VM支持的.dex文件
     
    将工程bin目录下的class文件编译成classes.dex,Android虚拟机只能执行dex文件!
     
    例子:
     
     
    第五步:打包生成未签名的.apk文件
     
    【输入】打包后的资源文件、打包后类文件(.dex文件)、libs文件(包括.so文件,当然很多工程都没有这样的文件,如果你不使用C/C++开发的话)
    【输出】未签名的.apk文件
    【工具】apkbuilder工具
    apkbuilder工具用法如下:
    1. -v      Verbose.  
    2. -d      Debug Mode: Includes debug files in the APK file.  
    3. -u      Creates an unsigned package.  
    4. -storetype Forces the KeyStore type. If ommited the default is used.  
    5.    
    6. -z      Followed by the path to a zip archive.  
    7.         Adds the content of the application package.  
    8.    
    9. -f      Followed by the path to a file.  
    10.         Adds the file to the application package.  
    11.    
    12. -rf     Followed by the path to a source folder.  
    13.         Adds the java resources found in that folder to the application  
    14.         package, while keeping their path relative to the source folder.  
    15.    
    16. -rj     Followed by the path to a jar file or a folder containing  
    17.         jar files.  
    18.         Adds the java resources found in the jar file(s) to the application  
    19.         package.  
    20.    
    21. -nf     Followed by the root folder containing native libraries to  
    22.         include in the application package.<span style="color: rgb(0, 0, 255); font-family: 楷体; line-height: 20px;font-size:18px; ">I:最后一步,通过jarsigner命令用证书文件对未签名的APK文件进行签名</span>  
     
    列子:
    apkbuilder  ${output.apk.file} -u -z  ${packagedresource.file} -f  ${dex.file}  -rf  ${source.dir}  -rj  ${libraries.dir} 
     
    第六步:对未签名.apk文件进行签名
    【输入】未签名的.apk文件
    【输出】签名的.apk文件
    【工具】jarsigner
     
    1. 用法:jarsigner [选项] jar 文件别名  
    2.        jarsigner -verify [选项] jar 文件  
    3.   
    4. [-keystore <url>]           密钥库位置  
    5.   
    6. [-storepass <口令>]         用于密钥库完整性的口令  
    7.   
    8. [-storetype <类型>]         密钥库类型  
    9.   
    10. [-keypass <口令>]           专用密钥的口令(如果不同)  
    11.   
    12. [-sigfile <文件>]           .SF/.DSA 文件的名称  
    13.   
    14. [-signedjar <文件>]         已签名的 JAR 文件的名称  
    15.   
    16. [-digestalg <算法>]    摘要算法的名称  
    17.   
    18. [-sigalg <算法>]       签名算法的名称  
    19.   
    20. [-verify]                   验证已签名的 JAR 文件  
    21.   
    22. [-verbose]                  签名/验证时输出详细信息  
    23.   
    24. [-certs]                    输出详细信息和验证时显示证书  
    25.   
    26. [-tsa <url>]                时间戳机构的位置  
    27.   
    28. [-tsacert <别名>]           时间戳机构的公共密钥证书  
    29.   
    30. [-altsigner <类>]           替代的签名机制的类名  
    31.   
    32. [-altsignerpath <路径列表>] 替代的签名机制的位置  
    33.   
    34. [-internalsf]               在签名块内包含 .SF 文件  
    35.   
    36. [-sectionsonly]             不计算整个清单的散列  
    37.   
    38. [-protected]                密钥库已保护验证路径  
    39.   
    40. [-providerName <名称>]      提供者名称  
    41.   
    42. [-providerClass <类>        加密服务提供者的名称  
    43. [-providerArg <参数>]] ... 主类文件和构造函数参数  
     
    第七步:对签名后的.apk文件进行对齐处理(不进行对齐处理是不能发布到Google Market的)
     
     
    【输入】签名后的.apk文件
    【输出】对齐后的.apk文件
    【工具】zipalign工具
     
  • 相关阅读:
    【持续更新】Android 源码下载地点
    android Notification 的使用
    如何设置 Notification 中PendingIntent 的 Intent
    CodeSmith部分类型转换代码
    【转】Request.ServerVariables参考
    ADT
    根据IP从纯真IP数据库查询地址
    VS2008重置默认环境
    Eclipse 安装SVN插件
    C#序列化JSON对象
  • 原文地址:https://www.cnblogs.com/ldq2016/p/10308559.html
Copyright © 2020-2023  润新知