Windows中使用命令行方式编译打包Android项目

网上很多用Ant来编译打包Android应用的文章,毕竟Ant是纯Java语言编写的,具有很好的跨平台性。今天想写个纯windows平台下的DOS命令行方式编译Android项目的bat文件,见以下代码:

echo off
rem =========基本参数配置============
rem jdk的路径
set JAVA_HOME=D:/Program Files/Java/jdk1.6.0_24
rem jdk的版本
set JDK_Version=1.6
rem sdk的路径
set AndroidHome=D:/Android/android-sdk-windows
rem 编译的android版本路径
set AndroidVersion=/platforms/android-8
rem 编译的android项目路径
set AndroidProject=D:/yourProject
rem 编译生成的未签名apk文件
set unsign_apk=yourProject.apk
rem 编译生成的已签名apk文件
set sign_apk=yourProject-sign.apk
rem 签名用的key
set apk_key=keyname
set apk_keypass=keypass
set apk_keystore=D:/yourProject/key.keystore

for %%x in ("%AndroidProject%") do set AndroidProject=%%~sx
for %%x in ("%JAVA_HOME%") do set JAVA_HOME=%%~sx
for %%x in ("%AndroidHome%") do set AndroidHome=%%~sx
rem jdk工具包
set EXE_JAVA=%JAVA_HOME%/bin/java
set JAVAC=%JAVA_HOME%/bin/javac
set JAR=%JAVA_HOME%/bin/jar
set KeyTool=%JAVA_HOME%/bin/keytool
set Jarsigner=%JAVA_HOME%/bin/jarsigner
rem sdk工具包
set AndroidAAPT=%AndroidHome%%AndroidVersion%/tools/aapt.exe
set AndroidDx=%AndroidHome%%AndroidVersion%/tools/dx.bat
set AndroidApkBuilder=%AndroidHome%/tools/apkbuilder.bat
set AndroidJar=%AndroidHome%%AndroidVersion%/android.jar
rem android项目引用的扩展jar包
set ExternerJar=%AndroidProject%/lib/commons-codec.jar;%AndroidProject%/lib/commons-httpclient-3.1.jar;
set ReferJar=%AndroidProject%/lib/commons-codec.jar %AndroidProject%/lib/commons-httpclient-3.1.jar
rem android项目基本目录及配置文件
set AndroidProjectRes=%AndroidProject%/res
set AndroidProjectGen=%AndroidProject%/gen
set AndroidProjectBin=%AndroidProject%/bin
set AndroidProjectAsset=%AndroidProject%/assets
set AndroidProjectAndroidMainfest=%AndroidProject%/AndroidManifest.xml
set AndroidProjectSrc=%AndroidProject%/src/weibo/*.java
set AndroidProjectSrc=%AndroidProjectSrc% %AndroidProject%/src/weibo/http/*.java
set AndroidProjectSrc=%AndroidProjectSrc% %AndroidProject%/src/weibo/util/*.java
set AndroidProjectSrc=%AndroidProjectSrc% %AndroidProject%/gen/yourProjectPackageName/*.java
rem 编译输出文件
set AndroidProjectClassDex=%AndroidProject%/bin/classes.dex
set AndroidProjectResources=%AndroidProject%/bin/resources.ap_
set AndroidProjectApk="%AndroidProject%/bin/%unsign_apk%"
set AndroidProjectSignApk="%AndroidProject%/bin/%sign_apk%"

echo 生成R.java
%AndroidAAPT% package -f -m -J %AndroidProjectGen% -S %AndroidProjectRes% -I %AndroidJar% -M %AndroidProjectAndroidMainfest%

echo 生成class
%JAVAC% -encoding UTF-8 -target %JDK_Version% -bootclasspath %AndroidJar% -classpath %ExternerJar% -d %AndroidProjectBin% %AndroidProjectSrc%

echo 生成dex
cd %AndroidProjectBin%
rem 把bin目录下*.class文件打成jar包
%JAR% cvf %AndroidProjectBin%/yourProject.jar *.*
cd %AndroidProject%
rem 生成dex  这里需要注意,因为调用的是bat的脚本,因此必须用Call
call %AndroidDx% --dex --output=%AndroidProjectClassDex% %AndroidProjectBin%/yourProject.jar %ReferJar%

echo 打包资源文件
%AndroidAAPT% package -f -M %AndroidProjectAndroidMainfest% -S %AndroidProjectRes% -A %AndroidProjectAsset% -I %AndroidJar% -F %AndroidProjectResources%

echo 生成未签名的apk文件
call %AndroidApkBuilder% %AndroidProjectApk% -v -u -z %AndroidProjectResources% -f %AndroidProjectClassDex% -rf %AndroidProject%/src

echo 生成数字签名key.keystore
%KeyTool% -genkey -v -keystore %apk_keystore% -storepass %apk_keypass% -keypass %apk_keypass% -alias myKey -dname CN=Liux,OU=makingware.com,O=makingware,L=sz,ST=gd,C=cn -keyalg RSA -validity 10000 

echo 进行数字签名
%Jarsigner% -verbose -keystore %apk_keystore% -keypass %apk_keypass% -storepass %apk_keypass% -signedjar %AndroidProjectSignApk% %AndroidProjectApk% myKey

echo 签名成功
pause

转载:http://my.oschina.net/liux/blog/37875

相关推荐