1. 解决什么问题

在学习Android源码过程中,因为不熟悉,很多时候需要“大力出奇迹”,比如需要查找某个文件,但是不知道在哪个仓库,只能在安卓根目录下面开始查找;想查找这个方法的实现在哪个源文件中,这些操作都非常地耗时,本文解决这个问题。

2. 查找文件

思路:缓存思路,将所有文件查找一遍,记录到文件,以后查找文件就变成查找文本。

find . -type f > ./android_all_files.list

cat android_all_files.list | grep -v -F ".cache/clangd/index" | grep -v -F "./out/" | grep -v -F "./.bootstrap/" | grep -v -F "./prebuilts" > ./android_all_files_1.list

rm -f android_all_files.list && mv android_all_files_1.list android_all_files.list

# 查找命令
grep "Omx.h" -F -nw android_all_files.list

其中去掉了out等目录的文件,因为这些目录通常不重要。

3. 查找文本

查找特定文件比找文件复杂得多,这个时候需要考虑缩小范围,如在.java中查找某个方法,在Android.bp / Android.mk / device.mk中查找某个模块。

谷歌提供的工具

# source & lunch

$ hmm

Run "m help" for help with the build system itself.

Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
- lunch:      lunch <product_name>-<build_variant>
              Selects <product_name> as the product to build, and <build_variant> as the variant to
              build, and stores those selections in the environment to be read by subsequent
              invocations of 'm' etc.
- tapas:      tapas [<App1> <App2> ...] [arm|x86|arm64|x86_64] [eng|userdebug|user]
              Sets up the build environment for building unbundled apps (APKs).
- banchan:    banchan <module1> [<module2> ...] [arm|x86|arm64|x86_64] [eng|userdebug|user]
              Sets up the build environment for building unbundled modules (APEXes).
- croot:      Changes directory to the top of the tree, or a subdirectory thereof.
- m:          Makes from the top of the tree.
- mm:         Builds and installs all of the modules in the current directory, and their
              dependencies.
- mmm:        Builds and installs all of the modules in the supplied directories, and their
              dependencies.
              To limit the modules being built use the syntax: mmm dir/:target1,target2.
- mma:        Same as 'mm'
- mmma:       Same as 'mmm'
- provision:  Flash device with all required partitions. Options will be passed on to fastboot.
- cgrep:      Greps on all local C/C++ files.
- ggrep:      Greps on all local Gradle files.
- gogrep:     Greps on all local Go files.
- jgrep:      Greps on all local Java files.
- ktgrep:     Greps on all local Kotlin files.
- resgrep:    Greps on all local res/*.xml files.
- mangrep:    Greps on all local AndroidManifest.xml files.
- mgrep:      Greps on all local Makefiles and *.bp files.
- owngrep:    Greps on all local OWNERS files.
- rsgrep:     Greps on all local Rust files.
- sepgrep:    Greps on all local sepolicy files.
- sgrep:      Greps on all local source files.
- godir:      Go to the directory containing a file.
- allmod:     List all modules.
- gomod:      Go to the directory containing a module.
- pathmod:    Get the directory containing a module.
- outmod:     Gets the location of a module's installed outputs with a certain extension.
- dirmods:    Gets the modules defined in a given directory.
- installmod: Adb installs a module's built APK.
- refreshmod: Refresh list of modules for allmod/gomod/pathmod/outmod/installmod.
- syswrite:   Remount partitions (e.g. system.img) as writable, rebooting if necessary.

介绍其中的一些工具:

  • croot:快速返回安卓根目录
  • cgrep:在所有C/C++文件中查找
  • jgrep:在所有Java文件中查找
  • mgrep:在所有Makefile和*.bp中查找
  • sgrep:在所有源代码文件中查找

接下来就容易很多,如果需要查找hal文件的实现,比如IOmx.hal,那么可以查找allocateNode在哪些C++文件中实现:

cgrep "allocateNode" -nw

以上就是全部内容!