转载地址: http://blog.csdn.net/mr_raptor/article/details/7539978
++++++++++++++++++++++++++++++++++++++++++
介绍:
Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。
Andriod编译系统使用自定义的编译工具生成,程序和文档。此篇文档只是简单的介绍一下:
android编译命令:
android编译系统是基于make的,要求 GNU make 为最新版本(因为android使用GNU make 的高级特性)。在编译之前应该先检查机器的make 版本。
使用命令: make -v[-version]
若显示的里面没有3.8及以上的版本,我们就需要升级make的版本。
Android官方的执行编译步骤如下:
1> source build/envsetup.sh:加载命令
2> lunch:选择平台编译选项
3> make:执行编译
详细解说,各个步骤操作内容:
1. source build/envsetup.sh
目地:用来将 envsetup.sh 里的所有用到的命令加载到环境变量里去。
文件 envsetup.sh 里的主要命令如下:
# # Vim build/envsetup.sh # function get_abs_build_var() # 获得一个绝对变量 function get_build_var() # 获得变量
function check_product() # 检查product function check_variant() # 检查变量 function setpaths() # 设置文件路径 function printconfig() # 打印配置 function set_stuff_for_environment() # 设置环境变量 function set_sequence_number() # 设置序列号 function settitle() # 设置标题 function addcompletions() # function choosetype() # 设置type function chooseproduct() # 设置product function choosevariant() # 设置variant function choosecombo() 设置编译参数
function tapas() #功能同上choosecombo() function add_lunch_combo() # 添加lunch选项
function print_lunch_menu() # 打印lunch列表 function lunch() # 配置lunch,选择编译的平台 function _lunch() # Tab completion for lunch. function gettop function m() # make from topfunction findmakefile() # 查找makefilefunction mm() # make from current directoryfunction mmm() # make the supplied directoriesfunction croot() # 回到根目录function cproj() # 回到项目function pid()function systemstack()function gdbclient()function gettargetarchfunction jgrep() # 查找java文件function cgrep() # 查找c/cpp文件function resgrep()function getprebuiltfunction tracedmdump()function runhat()function getbugreports()function getsdcardpath()function getscreenshotpath()function getlastscreenshot()function startviewserver()function stopviewserver()function isviewserverstarted()function key_home()function key_back()function key_menu()function smoketest()function runtest()function godir () # 跳到指定目录 # 这是系统自动增加了一个默认的编译项 full-eng, full_x86-eng# add the default one here add_lunch_combo full-eng add_lunch_combo full_x86-eng #查找vendorsetup.sh文件,如果查到了,就加载它 # Execute the contents of any vendorsetup.sh files we can find. for f in `test -d device && find device -maxdepth 6 -name 'vendorsetup.sh' 2> /dev/null` do echo "including $f" . $f done根据上面的内容,可以推测出,如果要想定义自己的产品编译项,简单的办法是直接在envsetup.sh最后,添加上add_lunch_combo myProduct-eng;当然这么做,不太符合上面代码最后的本意,我们还是老实的在目录下创建自己公司名字,然后在公司目录下创建一个新的vendorsetup.sh,在里面添加上自己的产品编译项
2. 按照android官网的步骤,开始执行 lunch
选择编译的平台执行lunch命令,它会打印出一个选择菜单,列出可用的编译选项,选择需要编译的平台即可。
lunch命令是envsetup.sh里定义的一个命令,用来让用户选择编译项,来定义Product和编译过程中用到的全局变量。
eng是产品的编译类型,除了eng外,还有user, userdebug
分别表示: eng ------ 工程机
user ------最终用户机
userdebug ------- 调试测试机
由上面的分析可知,lunch命令可以带参数和不带参数,最终导出一些重要的环境变量。从而影响到编译的结果。下面是分析lunch()函数 function lunch() { local answer if [ "$1" ] ; then #lunch后面直接带参数,eg:lunch full-eng answer=$1 else #lunch 后面没有带参数 print_lunch_menu echo -n "Which would you like? [full-eng] " read answer fi local selection= if [ -z "$answer" ] then # 如果用户在菜单中没有选择,直接回车,则为系统缺省的full-eng selection=full-eng elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$") then # 如果answer是选择菜单的数字,则获取该数字对应的字符串 if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ] then selection=${LUNCH_MENU_CHOICES[$(($answer-1))]} fi # 如果 answer字符串匹配 *-*模式(*的开头不能为-) elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$") then selection=$answer fi if [ -z "$selection" ] then echo echo "Invalid lunch combo: $answer" return 1 fi export TARGET_BUILD_APPS= # 将 product-variant模式种的product分离出来 local product=$(echo -n $selection | sed -e "s/-.*$//") # 检查 product check_product $product if [ $? -ne 0 ] then echo echo "** Don't have a product spec for: '$product'" echo "** Do you have the right repo manifest?" product= fi # 将 product-variant模式种的product分离出来 local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//") check_variant $variant if [ $? -ne 0 ] then echo echo "** Invalid variant: '$variant'" echo "** Must be one of ${VARIANT_CHOICES[@]}" variant= fi if [ -z "$product" -o -z "$variant" ] then echo return 1 fi if [ "$variant" = "userdebug_gms" ] then variant=userdebug export USE_GMS_ALL=true else export USE_GMS_ALL=false fi export TARGET_PRODUCT=$product export TARGET_BUILD_VARIANT=$variant export TARGET_BUILD_TYPE=release echo # 设置到环境变量 set_stuff_for_environment printconfig }
此时已经完成两个步骤,接着就执行 make 命令,下篇分析。。。。
# 更新代码的api接口,
make update-api
#编译代码
make -j4