当我们在Eclipse中对项目Run As Android Application的时候,会在bin文件夹下生成apk及其他一些文件,至于为什么生成这些文件开始我也不明白,查了相关资料才知道原来Android项目在Eclipse中是通过Ant进行构建的。学习Ant踩了很多坑,总结下来以后方便查看,也希望帮助更多的人。欢迎评论交流哦!
1、下载Ant并配置环境变量
Ant下载地址:http://ant.apache.org/bindownload.cgi;下载后直接解压到某目录下就OK。
添加Ant环境变量:ANT_HOME,变量值指向ant目录。
添加配置到Path中:%ANT_HOME%/bin;%ANT_HOME%/lib;
通过cmd命令行查看是否配置成功:
通过Ant打包Apk还需要配置JDK、Android SDK:
- JDK的配置大家肯定都会了。
- 添加系统环境变量ANDROID_HOME,值是Android sdk的路径。
2、配置打包项目
通过cmd进入到待代打包项目目录下执行android update project -n epeiwang -p . 命令。执行该命令后刷新项目,此时项目中会多build.xml
和local.properties文件。在项目下新建名为ant.properties的文件(非必须)然后将签名文件keystore放到项目下面或其他路径下。
- build.xml :定义了基础构建方法和打包策略的脚本。
- local.properties:存放sdk路径和其他一些本地配置。
- ant.properties:定义一些变量,一般为签名文件的地址,密码、别名等。
- 签名文件:用于对Ant打包后的Apk进行签名。
local.properties
sdk.dir=D:\\adt-bundle-windows\\sdk
ant.properties
#签名文件的路径
keystore=magic.keystore
#存储库口令
storepass=123456
#密匙库口令
keypass=123456
#别名
alias=magic
3、Ant打包流程
Ant打包主要流程:
1. 删除gen、bin目录并新建
2. 生成R.java类文件
3. 编译aidl文件
4. 编译源文件生成对应的class文件
5. 将.class文件转化成.dex文件
6. 打包资源文件
7. 生成未签名的apk包
8. 对未签名的apk包进行签名
9. 对签名的apk包进行字节对齐
(原图地址:http://blog.csdn.net/jason0539/article/details/44917745)
以上流程主要借助于sdk和jdk提供的相关工具进行完成。例如:aapt、dx、apkbuilder、jarsigner等,build.xml的编写其实就是对这些工具的使用。其中生成未签名的apk包可以,通过sdklib.jar配置新的apkbuilder使用或者直接使用sdklib.jar。
通过sdklib.jar配置新的apkbuilder.bat生成未签名的apk:
因为apkbuilder.bat在新版本的Android sdk中已经被移除,所以我们需要手动创建apkbuilder.bat文件,然后将此文件放在sdk tools目录下,apkbuilder.bat文件内容如下:
@echo off
rem Copyright (C) 2007 The Android Open Source Project
rem
rem Licensed under the Apache License, Version 2.0 (the "License");
rem you may not use this file except in compliance with the License.
rem You may obtain a copy of the License at
rem
rem http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License.
rem don't modify the caller's environment
setlocal
rem Set up prog to be the path of this script, including following symlinks,
rem and set up progdir to be the fully-qualified pathname of its directory.
set prog=%~f0
rem Change current directory and drive to where the script is, to avoid
rem issues with directories containing whitespaces.
cd /d %~dp0
rem Check we have a valid Java.exe in the path.
set java_exe=
call lib\find_java.bat
if not defined java_exe goto :EOF
set jarfile=sdklib.jar
set frameworkdir=
if exist %frameworkdir%%jarfile% goto JarFileOk
set frameworkdir=lib\
if exist %frameworkdir%%jarfile% goto JarFileOk
set frameworkdir=..\framework\
:JarFileOk
set jarpath=%frameworkdir%%jarfile%
call "%java_exe%" -classpath %jarpath% com.android.sdklib.build.ApkBuilderMain %*
build.xml脚本中使用
<!-- 根据classes.dex文件和resources.ap_生成未签名的apk包 -->
<target
name="package"
depends="package-res-and-assets" >
<echo message="package..."/>
<exec
executable="${apkbuilder}"
failonerror="true" >
<arg value="${unsigned-package}" /><!-- 输出 -->
<arg value="-u" /><!-- u指创建未签名的包-->
<arg value="-z" /><!-- 资源压缩包 -->
<arg value="${resources-package}" />
<arg value="-f" /><!-- dex文件 -->
<arg value="${bin}/classes.dex" />
<arg value="-rf" />
<arg value="${src}" />
<arg value="-