Android 源码树中提供了一份环境配置脚本文件,导入这份环境配置之后,我们可以得到一些额外的非常实用的命令。
导入环境配置的方法是在源码树的根路径下运行如下命令:
$ source build/envsetup.sh
或者
$ . build/envsetup.sh
之后,运行hmm
命令会得到帮助信息,显示可用的一些宏和函数,这些宏和函数可以当成shell下的命令使用。
$ hmm
Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
- lunch: lunch <product_name>-<build_variant>
- tapas: tapas [<App1> <App2> ...] [arm|x86|mips|armv5] [eng|userdebug|user]
- 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, but not their dependencies.
- mmm: Builds all of the modules in the supplied directories, but not their dependencies.
- mma: Builds all of the modules in the current directory, and their dependencies.
- mmma: Builds all of the modules in the supplied directories, and their dependencies.
- 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:
addcompletions add_lunch_combo cgrep check_product check_variant choosecombo chooseproduct choosetype
choosevariant cproj croot findmakefile gdbclient gdbwrapper get_abs_build_var getbugreports get_build_var
getlastscreenshot getprebuilt getscreenshotpath getsdcardpath gettargetarch gettop godir hmm isviewserverstarted
jgrep key_back key_home key_menu lunch _lunch m mangrep mm mma mmm mmma pez pid printconfig print_lunch_menu qpid
resgrep runhat runtest sepgrep set_java_home setpaths set_sequence_number set_stuff_for_environment settitle
smoketest stacks startviewserver stopviewserver systemstack tapas tracedmdump
设置基本的构建参数,使用choosecombo
或lunch
命令。这些基本的配置项是:
例如:
$ choosecombo
Build for the simulator or the device?
1. Device
2. Simulator
Which would you like? [1]
Build type choices are:
1. release
2. debug
Which would you like? [1] 2
Which product would you like? [generic]
Variant choices are:
1. user
2. userdebug
3. eng
Which would you like? [eng]
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=1.6
TARGET_PRODUCT=generic
TARGET_BUILD_VARIANT=eng
TARGET_SIMULATOR=false
TARGET_BUILD_TYPE=debug
TARGET_ARCH=arm
HOST_ARCH=x86
HOST_OS=linux
HOST_BUILD_TYPE=release
BUILD_ID=Donut
============================================
或者直接选定预设配置:
$ lunch generic-eng
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=1.6
TARGET_PRODUCT=generic
TARGET_BUILD_VARIANT=eng
TARGET_SIMULATOR=false
TARGET_BUILD_TYPE=debug
TARGET_ARCH=arm
HOST_ARCH=x86
HOST_OS=linux
HOST_BUILD_TYPE=release
BUILD_ID=Donut
============================================
注:方括号([])中是当前选项的默认值,不输入直接按Enter采用默认值。
启用ccache
$ export USE_CCACHE=1
或者直接将上面这条命令写入到.bashrc
文件中
$ echo export USE_CCACHE=1 >> ~/.bashrc
$ source ~/.bashrc
如果编译主机CPU是多核的,可以让多个CPU核并行参与编译。例如,8核CPU:
$ make -j8
下面命令可以显示CPU的核心数:
$ sed -n "/processor/p" /proc/cpuinfo | wc -l
如果是在脚本中使用可以直接写成下面这样更方便:
make -j`sed -n "/processor/p" /proc/cpuinfo | wc -l`
下面指定目标的编译命令可以用于编译特定的代码:
当导入build/envsetup.sh
的时候,一些有用的shell宏和函数就引入进来了,我们可以把它们当成普通的命令使用,非常方便。运行hmm
命令可以查看这些命令信息,下面是部分常用的命令:
例如,源代码的根目录是~/mydroid
,当前处在~/mydroid/frameworks/base/core/java/android/os
路径下,想回到源码根目录下,不需要输入一大堆的cd ../..
,指需要croot
一条命令就可以了:
~/mydroid/frameworks/base/core/java/android/os $ croot
~/mydroid $
这个命令基本上是croot
的反操作,从当前路径直接跳到某个文件所在的目录,如果有多个同名的文件,会给出一个列表做进一步选择。
~/mydroid/packages/apps/Music $ godir SystemClock.java
~/mydroid/frameworks/base/core/java/android/os $
不管当前处在哪个子目录下,想到源码根目录下执行make
命令,不需要先返回到源码根目录再执行make
,只需要在当前目录下执行一个m
命令就可以了。
在某个子模块目录下,编译这个子模块。例如,编译Music
~/mydroid/packages/apps/Music $ mm
在源码根目录下编译指定的子模块。例如,编译Music
~/mydroid $ mm packages/apps/Music