Android命令行手动编译打包详解

时间:2022-06-29 15:26:25

Android 命令行手动编译打包过程图

【详细步骤】:

Android命令行手动编译打包详解

Step1:使用aapt生成R.java类文件。
例:

F:/explorer/android-sdk-windows2.2/tools>f:/explorer/android-sdk-windows2.2/platforms/android-3/tools/aapt.exe package -f -m -J F:/explorer/AndroidByread/gen -S res -I f:/explorer/android-sdk-windows2.2/platforms/android-3/android.jar -M AndroidManifest.xml
其中  -f -m -J F:/explorer/AndroidByread/gen 代表按覆盖的形式在gen目录下生成带包路径的R.java,-S res指定资源文件 ,-I f:/explorer/android-sdk-windows2.2/platforms/android-3/android.jar 指定使用的android类,-M AndroidManifest.xml指定程序的配置文件

aapt Usage:

Android命令行手动编译打包详解

Step2:使用android SDK提供的aidl.exe把.aidl转成.java文件:
usage: aidl OPTIONS INPUT [OUTPUT]
       aidl --preprocess OUTPUT INPUT...

OPTIONS:
   -I<DIR>    search path for import statements.
   -d<FILE>   generate dependency file.
   -p<FILE>   file created by --preprocess to import.
   -o<FOLDER> base output folder for generated files.
   -b         fail when trying to compile a parcelable.

INPUT:
   An aidl interface file.

OUTPUT:
   The generated interface files.

Step3:编译.java类文件生成class文件。
例:F:/explorer/AndroidByread>javac -encoding GB18030 -target 1.5 -bootclasspath F:/explorer/android-sdk-windows2.2/platforms/android-3/android.jar -d bin src/com/byread/reader/*.java gen/com/byread/reader/R.java

Android命令行手动编译打包详解

Step4:使用android SDK提供的dx.bat命令行脚本生成classes.dex文件。

Android命令行手动编译打包详解

例:
F:/explorer/AndroidByread>F:/explorer/android-sdk-windows2.2/platforms/android-3/tools/dx.bat --dex --output=F:/explorer/AndroidByread/bin/classes.dex f:/explorer/AndroidByread/bin/classes
其中classes.dex为生成的目标文件,f:/explorer/AndroidByread/bin/classes为class文件所在目录

Step5:使用Android SDK提供的aapt.exe生成资源包文件(包括res、assets、androidmanifest.xml等)。
F:/explorer/AndroidByread>F:/explorer/android-sdk-windows2.2/platforms/android-3/tools/aapt.exe package -f -M AndroidManifest.xml -S res -A assets -I F:/explorer/android-sdk-windows2.2/platforms/android-3/android.jar -F bin/byreadreader
将AndroidManifest.xml,res和assets文件夹中的资源文件打包生成byreadreader,用法参见1

Step6:生成未签名的apk安装文件:
apkbuilder  ${output.apk.file} -u -z  ${packagedresource.file} -f  ${dex.file}  -rf  ${source.dir}  -rj  ${libraries.dir}

Android命令行手动编译打包详解

例:
F:/explorer/AndroidByread>f:/explorer/android-sdk-windows2.2/tools/apkbuilder.bat f:/explorer/byreadreader.apk –v -u -z f:/explorer/AndroidByread/bin/byreadreader -f f:/explorer/AndroidByread/bin/class.dex -rf F:/explorer/AndroidByread/src
其中f:/explorer/byreadreader.apk为生成的apk ,-z f:/explorer/AndroidByread/bin/byreadreader为资源包,f:/explorer/AndroidByread/bin/class.dex为类文件包


Step7:使用jdk的jarsigner对未签名的包进行apk签名:
use jarsigner
jarsigner  -keystore ${keystore} -storepass  ${keystore.password} -keypass ${keypass} -signedjar ${signed.apkfile} ${unsigned.apkfile} ${keyalias}
例如:
F:/explorer/android-sdk-windows2.2/tools>jarsigner –keystore f:/explorer/eclipse3.5/bbyread.keystore -storepass byread002 -keypass byread002 -signedjar f:/explorer/byread.apk f:/explorer/byreadreader.apk byread
其中–keystore f:/explorer/eclipse3.5/bbyread.keystore 为密钥文件  -storepass byread002为密钥文件密码 byread 为密钥别名 -keypass byread002为密钥别名密码,-signedjar f:/explorer/byread.apk为签名后生成的apk文件 f:/explorer/byreadreader.apk为未签名的文件。

 

转自:http://jojol-zhou.iteye.com/blog/729254