码迷,mamicode.com
首页 > 移动开发 > 详细

转:Android开发实践:用脚本编译Android工程

时间:2015-09-25 09:40:07      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

 转自: http://ticktick.blog.51cto.com/823160/1365947

 

 一般情况下,我们都是使用Eclipse+ADT插件或者Android studio软件来编译Android工程的,其实,Eclipse或者Android studio仅仅是将一些编译命令封装到可视化界面里而已,通过shell脚本、ADT附带的一些命令以及Ant工具,我们完全可以将Android工程的编译过程自动化,直接通过运行脚本就得到最终的APK文件。

 

   Linux下用脚本编译c/c++程序至少需要两个东西,一个是编译器,如gcc/g++,另一个则是编译配置文件,即Makefile;同理,编译Android程序也同样需要编译器和编译配置文件,由于Android程序是Java开发的,所以编译器一般使用Ant(Apache的核心项目,Java编译器),配置文件则是build.xml,该编译配置文件可以自己手写,也可以通过ADT的工具生成;如果Andrid工程涉及JNI层,则编译命令使用NDK-Build,对应的编译配置文件则是Android.mk。

 

   前面这段话,简单介绍了使用脚本编译Andriod工程的一些基本工具和关键词,详细的用法就留给读者自己去Google吧,这里给出一个我自己编写的通用脚本,放到你的Android工程的根目录,修改脚本的相关路径的配置,添加执行权限(chmod +x),即可编译你的Android工程了,具体的用法用注释的形式添加在脚本中了,文章最后的附件则是该脚本的测试Android工程。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
                                                                                                                                                                                                                                                             
#NOTE: 用法,在本工程根目录下执行 ./build.sh xxx
                                                                                                                                                                                                                                         
#其中,xxx为参数,可以是clean debug release,也可以不带参数,不带参数默认为debug
                                                                                                                                                                                                                                            
#如果参数是 clean,  则会删除所有编译生成的文件              
#如果参数是 debug , 则会生成 debug 版本的apk,位于 bin 目录下
#如果参数是 release,则生成 release版本的apk,位于 bin 目录下
                                                                                                                                                                                                                                             
#NOTE: 如果更改了工程配置,请先删除目录下的build.xml
                                                                                                                                                                                                                                            
#获取本目录的名称,作为build.xml的项目名称
PROJECT_NAME=${PWD##*/}
                                                                                                                                                                                                                                                   
#路径配置,请根据编译环境修改
ANDROID_SDK_PATH=/opt/android/sdk
ANDROID_NDK_PATH=/opt/android/ndk
ANT_PATH=/opt/apache-ant-1.9.0/bin
                                                                                                                                                                                                                                                             
#决定是否生成release版
BUILD_RELEASE=0
                                                                                                                                                                                                                                                              
if [ $1 = "clean" ]
then
    rm -rf gen
    rm -rf obj
    rm -rf libs/armeabi
    echo "clean success"
    exit
fi
                                                                                                                                                                                                                                                 
if [ $1 = "debug" ]
then
    BUILD_RELEASE=0
fi
                                                                                                                                                                                                                                                      
if [ $1 = "release" ]
then
    BUILD_RELEASE=1
fi
                                                                                                                                                                                                                                                                         
echo "begin to build..."
                                                                                                                                                                                                                                                             
#判断 build.xml 是否存在,若不存在,则创建一个
#请根据你的目标平台,修改update project命令的最后一个参数,该值为 android list 命令输出结果列表的id值
                                                                                                                                                                                                                                                        
if [ ! -e build.xml ]
then
    echo "No build.xml exist, generate build.xml"
    $SDK_PATH/tools/android update project -n $PROJECT_NAME -p . -s -t 1
fi
                                                                                                                                                                                                                                                                  
#判断jni目录是否存在,如果存在,则首先编译jni目录
if [ -d jni ]
then
    echo "build jni, generate libs"
    $ANDROID_NDK_PATH/ndk-build
fi
                                                                                                                                                                                                                                                               
#使用ant来编译
if [ $BUILD_RELEASE ]
then
    $ANT_PATH/ant release 
else
    $ANT_PATH/ant debug
fi
echo "build success"

 

   在Android工程的根目录运行该脚本文件后,会在bin目录下生成apk文件,如果工程目录下没有ant.properties文件和xxx.key文件的话,则只会生成debug版和unsigned-release版,如果希望直接生成签名后的apk文件,则需要给出一个Android key文件,并编写一个ant.properties文件,该文件的编写示例如下:

 

1
2
3
4
key.store=ticktick.key
key.alias=ticktick
key.store.password=123456
key.alias.password=123456

 

   在Android工程的根目录下,如果存在该ant.properties文件,则在编译release版的时候,会自动使用ticktick.key文件来签名生成的apk文件。

 

   关于使用脚本编译Android工程就介绍到这儿了,希望我的示例脚本能够给初学者提供一些参考和帮助,有任何疑问欢迎留言或者来信lujun.hust@gmail.com交流,或者关注我的新浪微博 @卢_俊 获取最新的文章和资讯。

转:Android开发实践:用脚本编译Android工程

标签:

原文地址:http://www.cnblogs.com/jhj117/p/4837129.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!