Android 源码工作环境实用技巧

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

基本的构建设置

设置基本的构建参数,使用choosecombolunch命令。这些基本的配置项是:

  • the product ('generic' or some specific board or platform name)
  • the build variant ('user', 'userdebug', or 'eng')
  • whether you're running on a simulator ('true' or 'false')
  • the build type ('release' or 'debug')

例如:

$ 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`

编译特定目标

下面指定目标的编译命令可以用于编译特定的代码:

  • make sdk - build the tools that are part of an SDK (adb, fastboot, etc.)
  • make snod - build the system image from the current software binaries
  • make services
  • make runtime
  • make droid - make droid is the normal build.
  • make all - make everything, whether it is included in the product definition or not
  • make clean - remove all built files (prepare for a new build). Same as rm -rf out _
  • make modules - shows a list of submodules that can be built (List of all LOCAL_MODULE definitions)
  • make _ - make a specific module (note that this is not the same as directory name. It is the LOCAL_MODULE definition in the Android.mk file)
  • make clean-_ - clean a specific module
  • make bootimage - build kernel and create a new boot image
  • make bootimage TARGET_PREBUILT_KERNEL=/path/to/bzImage - create a new boot image with custom bzImage

一些实用的命令

当导入build/envsetup.sh的时候,一些有用的shell宏和函数就引入进来了,我们可以把它们当成普通的命令使用,非常方便。运行hmm命令可以查看这些命令信息,下面是部分常用的命令:

  • croot - change directory to the top of the tree
  • godir _ - go to the directory containing a file
  • m - execute 'make' from the top of the tree (even if your current directory is somewhere else)
  • mm - builds all of the modules in the current directory
  • mmm _ … - build all of the modules in the supplied directories
  • cgrep _ - grep on all local C/C++ files
  • jgrep _ - grep on all local Java files
  • resgrep _ - grep on all local res/*.xml files

croot

例如,源代码的根目录是~/mydroid,当前处在~/mydroid/frameworks/base/core/java/android/os路径下,想回到源码根目录下,不需要输入一大堆的cd ../..,指需要croot一条命令就可以了:

~/mydroid/frameworks/base/core/java/android/os $ croot
~/mydroid $

godir

这个命令基本上是croot的反操作,从当前路径直接跳到某个文件所在的目录,如果有多个同名的文件,会给出一个列表做进一步选择。

~/mydroid/packages/apps/Music $ godir SystemClock.java
~/mydroid/frameworks/base/core/java/android/os $

m

不管当前处在哪个子目录下,想到源码根目录下执行make命令,不需要先返回到源码根目录再执行make,只需要在当前目录下执行一个m命令就可以了。

mm

在某个子模块目录下,编译这个子模块。例如,编译Music

~/mydroid/packages/apps/Music $ mm

mmm

在源码根目录下编译指定的子模块。例如,编译Music

~/mydroid $ mm packages/apps/Music

你可能感兴趣的:(android,技巧)