基本配置--build.properties
首先编写基本配置build.properties ,主要配置如下:
- android sdk所在目录
- 编译项目所使用的项目版本
- Jdk所在目录
- 以及签名时,所需要的密钥文件名称和密码等信息(keystore文件存在在主工程根目录下)
- 以下标红的信息请注意替换成自己的skd等所在目录。
sdk.home= E: \\android_env\\android-sdks-new
sdk.platform= android-15
jdk.home= D: \\Java\\jdk1.6.0_16
keystore.password= 123456
keystore.name= my.keystore
keystore.alias= read
项目配置--auto-build.properties:
- 主工程绝对路径
- lib库1的绝对路径
- lib库2的绝对路径
- 打包工程的绝对路径
因为这些都是绝对路径,所以大家在下载demo后,在eclipse中附加工程时,请注意勾选copy to workspace选项。防止出现找不到文件路径的问题。以下绝对路径也注意要修改成自己工作空间所在的绝对路径。
mainproject.abs= E:\\linshi-workspace\\testAutobuild
libproject.abs= E:\\linshi-workspace\\testLibOne
libproject2.abs= E:\\linshi-workspace\\testLibTwo
curproject.abs= E:\\linshi-workspace\\auto-builder
重要的是Ant构建脚本
<? xml version= "1.0" encoding = "gbk"?>
<!-- Build an Android project. -->
< project
name= "testAutobuild"
default= "10zipalign"
basedir= "." > <!-- 通用属性 --> <property file= "build.properties" /> <!-- 项目属性 --> <property file= "auto-build.properties" /> <!-- 工程相关配置 --> <property
name= "libproject.abs.res"
location= "${libproject.abs}\res" /> <property
name= "libproject.abs.src"
location= "${libproject.abs}\src" /> <property
name= "libproject.abs.lib"
location= "${libproject.abs}\libs" /> <property
name= "libproject.package.name"
value= "com.liubing.testLibOne" /> <!-- lib2工程相关配置 --> <property
name= "libproject2.package.name"
value= "com.liubing.testlibtwo" /> <property
name= "libproject2.abs.res"
location= "${libproject2.abs}/res" /> <property
name= "libproject2.abs.src"
location= "${libproject2.abs}/src" /> <property
name= "libproject2.abs.lib"
value= "${libproject2.abs}/libs" /> <!-- The absolute paths for the tools. --> <property
name= "android.framework"
value= "${sdk.home}/platforms/${sdk.platform}/framework.aidl" >
</property > <property
name= "android.core"
value= "${sdk.home}/platforms/${sdk.platform}/android.jar" >
</property > <property
name= "android.proguard"
value= "${sdk.home}/tools/proguard/lib/proguard.jar" >
</property > <property
name= "android.support.lib"
value= "${sdk.home}/tools/support" >
</property > <property
name= "jarsigner"
value= "${jdk.home}/bin/jarsigner.exe" >
</property > <property
name= "zipalign"
value= "${sdk.home}/tools/zipalign.exe" >
</property > <property
name= "dx"
value= "${sdk.home}/platform-tools/dx.bat" >
</property > <property
name= "apk-builder"
value= "${sdk.home}/tools/apkbuilder.bat" >
</property > <property
name= "aapt"
value= "${sdk.home}/platform-tools/aapt.exe" >
</property > <property
name= "aidl"
value= "${sdk.home}/platform-tools/aidl.exe" >
</property > <property
name= "adb"
value= "${sdk.home}/platform-tools/adb.exe" >
</property > <!-- The absolute path. --> <property
name= "src.abs"
value= "${mainproject.abs}/src" >
</property > <property
name= "bin.abs"
value= "${mainproject.abs}\bin" >
</property > <property
name= "gen.abs"
value= "${mainproject.abs}/gen" >
</property > <property
name= "res.abs"
value= "${mainproject.abs}/res" >
</property > <property
name= "assets.abs"
value= "${mainproject.abs}/assets" >
</property > <property
name= "androidManifest.abs"
value= "${mainproject.abs}/AndroidManifest.xml" >
</property > <property
name= "dex.abs"
value= "${mainproject.abs}/bin/classes.dex" >
</property > <property
name= "resources.abs"
value= "${mainproject.abs}/bin/resources.ap_" >
</property > <property
name= "unsigned.abs"
value= "${bin.abs}/${ant.project.name}-unsigned.apk" >
</property > <property
name= "signed.abs"
value= "${bin.abs}/${ant.project.name}-signed.apk" >
</property > <property
name= "keystore.path"
location= "${mainproject.abs}/${keystore.name}" /> <!-- 1.Cleaning the project --> <target name= "1clean" > < echo>
Cleaning the project... </ echo> < delete
includeemptydirs ="true"
verbose= "true" > < fileset
dir= "${bin.abs}"
includes= "**/*" >
</ fileset>
</ delete> < delete
includeemptydirs ="true"
verbose= "true" > < fileset
dir= "${gen.abs}"
includes= "**/*" >
</ fileset>
</ delete>
</target > <!-- 2 Generate R.java --> <target
name= "2generate"
depends= "1clean" > < echo>
Generating R.java / Manifest.java from the resources... </ echo> < exec
executable ="${aapt}"
failonerror ="true" > < arg value= "package" /> < arg value= "-f" /> < arg value= "-m" /> < arg value= "--auto-add-overlay" /> < arg value= "-J" /> < arg value= "${gen.abs}" /> < arg value= "-M" /> < arg value= "${androidManifest.abs}" /> < arg value= "-S" /> < arg value= "${res.abs}" /> < arg value= "-S" /> < arg value= "${libproject.abs.res}" /> < arg value= "-S" /> < arg value= "${libproject2.abs.res}" /> < arg value= "--extra-packages" /> < arg value= "${libproject.package.name}:${libproject2.package.name}" />
<!-- Library Project的包名 --> < arg value= "-I" /> < arg value= "${android.core}" />
</ exec>
</target > <!-- 3Convert and compile AIDL files. --> <target
name= "3aidl"
depends= "1clean" > < echo>
Compiling aidl files into Java classes... </ echo> < apply
executable ="${aidl}"
failonerror ="true" > < arg value= "-p${android.framework}" /> < arg value= "-I${src.abs}" /> < arg value= "-o${gen.abs}" /> < fileset dir= "${src.abs}" > < include name= "**/*.aidl" >
</ include>
</ fileset>
</ apply>
</target > <!--
4Compile
srcdir="${framework_src.abs}:${src.abs}:${gen.abs}" 表示编译的源文件目录有三个 --> <target
name= "4compile"
depends= "1clean,2generate,3aidl" > < javac
encoding= "UTF-8"
target= "1.6"
debug= "off"
extdirs= ""
includeantruntime ="false"
srcdir ="${src.abs}:${gen.abs}:${libproject.abs.src}:${libproject2.abs.src}"
destdir= "${bin.abs}"
bootclasspath ="${android.core}" > < classpath> < fileset
dir= "${libproject.abs.lib}"
includes= "*.jar" >
</ fileset>
</ classpath> < classpath> < fileset
dir= "${android.support.lib}"
includes= "*.jar" >
</ fileset>
</ classpath> < classpath> < fileset
dir= "${libproject2.abs.lib}"
includes= "*.jar" >
</ fileset>
</ classpath>
</ javac>
</target > <!-- 5Convert and compress .class files to .dex file. --> <target
name= "5dex"
depends= "4compile" > < echo>
Converting compiled files and external libraries into dex format file... </ echo> < apply
executable ="${dx}"
failonerror ="true"
parallel= "true" > < arg value= "--dex" /> < arg value= "--output=${dex.abs}" /> < arg path= "${bin.abs}" /> < fileset
dir= "${libproject.abs.lib}"
includes= "*.jar" >
</ fileset> < fileset
dir= "${libproject2.abs.lib}"
includes= "*.jar" />
</ apply>
</target > <!-- 6Package the resources. --> <target name= "6package" > < echo>
Packaging resources and assets... </ echo> < exec
executable ="${aapt}"
failonerror ="true" > < arg value= "package" /> < arg value= "-f" /> < arg value= "-m" /> < arg value= "--auto-add-overlay" /> < arg value= "-M" /> < arg value= "${androidManifest.abs}" /> < arg value= "-S" /> < arg value= "${res.abs}" /> < arg value= "-S" /> < arg value= "${libproject.abs.res}" /> < arg value= "-S" /> < arg value= "${libproject2.abs.res}" /> < arg value= "-A" /> < arg value= "${assets.abs}" /> < arg value= "-I" /> < arg value= "${android.core}" /> < arg value= "-F" /> < arg value= "${resources.abs}" />
</ exec>
</target > <!-- 7Package an unsigned APK file. --> <target
name= "7genUnsignedApk"
depends= "5dex, 6package" > < echo>
Packaging an unsigned APK file... </ echo> < exec
executable ="${apk-builder}"
failonerror ="true" > < arg value= "${unsigned.abs}" /> < arg value= "-u" /> < arg value= "-z" /> < arg value= "${resources.abs}" /> < arg value= "-f" /> < arg value= "${dex.abs}" /> < arg value= "-rf" /> < arg value= "${src.abs}" /> <!-- 加jar库到应用包中 --> < arg value= "-rj" /> < arg value= "${libproject.abs.lib}" /> <!-- 加动态连接库到应用包中 --> < arg value= "-nf" /> < arg value= "${libproject.abs.lib}" />
</ exec> < echo>
It will need to be signed with jarsigner before being published. </ echo>
</target > <!-- 9Sign the APK. --> <target
name= "9genSignedApk"
depends= "7genUnsignedApk" > < exec
executable ="${jarsigner}"
failonerror ="true" > < arg value= "-verbose" /> < arg value= "-storepass" /> < arg value= "${keystore.password}" /> < arg value= "-keystore" /> < arg value= "${keystore.path}" /> < arg value= "-signedjar" /> < arg value= "${signed.abs}" /> < arg value= "${unsigned.abs}" /> < arg value= "${keystore.alias}" />
</ exec>
</target > <!-- 10Zipalign --> <target
name= "10zipalign"
depends= "9genSignedApk" > < exec
executable ="${zipalign}"
failonerror ="true" > < arg value= "-v" /> < arg value= "-f" /> < arg value= "4" /> < arg value= "${signed.abs}" /> < arg value= "${bin.abs}\${ant.project.name}-final.apk" />
</ exec>
</target > </ project>
注意事项:脚本中配置了项目的名称,注意可以改成自己的项目名称
两个lib库的包名,可根据自己需要增加或者删除
两个lib库的包名,可根据自己需要增加或者删除
省去若干调试,查询资料过程若干……终于跑通了
这样,我们的自动打包就算初具雏形,跑一下ant脚本,就可以将包自动打出来。
所有资源
点我下载
转载注明:
望月听涛
望月听涛