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

使用ANT来对应用打包,一般会经过以下几个步骤:

1.用aapt命令生成R.java文件

2.用aidl命令生成相应java文件

3.用javac命令编译java源文件生成class文件

4.用dx.bat将class文件转换成classes.dex文件

5.用aapt命令生成资源包文件resources.ap_

6.用apkbuilder.bat打包资源和classes.dex文件,生成unsigned.apk

7.用jarsinger命令对apk认证,生成signed.apk

为了便于理解和记忆,下面来用一张流程图来说明以上的几个过程:




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

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

OPTIONS: 

   -I

    search path for import statements. 
   -d   generate dependency file. 
   -p   file created by --preprocess to import. 
   -o base output folder for generated files. 
   -b         fail when trying to compile a parcelable.

INPUT: 
   An aidl interface file.

OUTPUT: 
   The generated interface files.

 

3. 第三步 编译.java类文件生成class文件: 
例:E:\Androiddev\AndroidByread>javac -encoding GB18030 -target 1.5 -bootclasspath E:\Androiddev\android-sdk-windows2.2\platforms\android-3\android.jar -d bin src\com\byread\reader\*.java gen\com\byread\reader\R.java 


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

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

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

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

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

 

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


如何创建keystore证书:



build.xml文件:



	
	
	
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
	
	

	
	
	

	
	
	
	

	
	
	

	
	
	

	
	

	
	
	

	
	
		Initializing all output directories...
		
		
		
	

	
	
		Generating R.java from the resources...
		
			
			
			
			
			
			
			
			
			
			
			
		
	

	
	
		Compiling .aidl into java files...
		
			
			
			
			
			
			
			
			
				
			
		
	

	
	
		Compiling java source code...
		
			
				
				
					
				
			
		
	

	
	
		Converting compiled files and external libraries into a .dex file...
		
			
			
			
			
			
			
		
	
	
	
	
		Packaging resources and assets...
		
			
			
			
			
			
			
			
			
			
			
			
			
		
	

	
	
		Packaging unsigned apk for release...
		
			
			
			
			
			
			
			
			
		

		It will need to be signed with jarsigner before being published.
	
	
	
		Packaging signed apk for release...
		
			
			
			
			
			
			
			
			
			
			
			
		
	
	
	
		
		
		APK is released. path:${out-signed-package-ospath}
	







你可能感兴趣的:(Android)