Androi源码编译的第一步,即执行envsetup.sh(sources build/envsetup.sh)脚本,初始化环境变量。可见envsetup.sh在整个Android编译系统所扮演的重要角色。
脚本envsetup.sh的主要作用是添加编译系统所需的环境变量以及一些方便Android开发的shell命令到当前shell。在执行envsetup.sh脚本后,可以通过如下命令来查看新加的变量及命令:
- ~/android/source/build$ . envsetup.sh
- ~/android/source/build$ set |less
在Android源码目录下,也可以通过如下命令查看新添加的shell命令:
- ~/android/source$ help
- Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
- - croot: Changes directory to the top of the tree.
- - m: Makes from the top of the tree.
- - mm: Builds all of the modules in the current directory.
- - mmm: Builds all of the modules in the supplied directories.
- - cgrep: Greps on all local C/C++ files.
- - jgrep: Greps on all local Java files.
- - resgrep: Greps on all local res/*.xml files.
- - godir: Go to the directory containing a file.
- Look at the source to view more functions. The complete list is:
- add_lunch_combo cgrep check_product check_variant choosecombo chooseproduct choosetype choosevariant cproj croot findmakefile gdbclient get_abs_build_var getbugreports get_build_var getprebuilt gettop godir help isviewserverstarted jgrep lunch m mm mmm pid printconfig print_lunch_menu resgrep runhat runtest set_java_home setpaths set_sequence_number set_stuff_for_environment settitle smoketest startviewserver stopviewserver systemstack tapas tracedmdump
下面将比较常用的shell命令逐一解释:
- croot
改变当前路径到Android源码的根目录 - m
编译整个Android项目 - mm
编译当前目录下所有的模块 - mmm
编译通过参数指定的模块,例如mmm dalvik/dexdump - cgrep
查询当前目录及子目录下所有符合查询条件的C文件(*.c, *.cc, *.cpp, *.h) - jgrep
查询当前目录及子目录下所有符合查询条件的JAVA文件(*.java) - resgrep
查询当前目录及子目录下所有资源相关的文件(*.xml) - lunch
选择编译的目标 - printconfig
输出类似如下格式的当前配置- ============================================
- PLATFORM_VERSION_CODENAME=REL
- PLATFORM_VERSION=2.3.5
- TARGET_PRODUCT=generic
- TARGET_BUILD_VARIANT=eng
- TARGET_SIMULATOR=
- TARGET_BUILD_TYPE=release
- TARGET_BUILD_APPS=
- TARGET_ARCH=arm
- HOST_ARCH=x86
- HOST_OS=linux
- HOST_BUILD_TYPE=release
- BUILD_ID=GINGERBREAD
- ============================================
在开始编译之前,还需要通过如下shell命令lunch选择编译目标:
- ~/android/source$ lunch
- You're building on Linux
- Lunch menu... pick a combo:
- 1. generic-eng
- 2. simulator
- 3. full_passion-userdebug
- 4. full_crespo4g-userdebug
- 5. full_crespo-userdebug
- Which would you like? [generic-eng]
如上所示,当前共有5种编译目标类型可供选择。其中generic-eng和simulator属于系统默认的编译目标类型,后面三种则是从vendor/*/vendorsetup.sh、vendor/*/build/vendorsetup.sh和device/*/*/vendorsetup.sh的文件中读取的。
编译目标类型由两部分构成,中间以"-"分隔。前半部分称之为编译名字,后半部分称之为编译类型。
目前Android一共包含3种编译类型,可以通过如下命令查看:
- ~/android/source/build$ set |grep “VARIANT_CHOICES=”
- VARIANT_CHOICES=([0]="user" [1]="userdebug" [2]="eng")
- eng
应该是engineering的缩写,属于适合开发的编译类型。
安装所有标记eng,user,debug或者development的模块
安装所有没有标记的非APK模块
按照产品定义文件安装APK模块
ro.secure=0
ro.debuggable=1
ro.kernel.android.checkjni=1
激活adb - user
编译适合用户使用的最终版本。
安装标记为user的模块
安装所有没有标记的非APK模块
按照产品定义文件安装APK模块
ro.secure=1
ro.debuggable=0
禁止adb - userdebug
除了以下几点,其他和user一样
安装标记为debug的模块
ro.debuggable=1
激活adb