1. Introduction

The experience of viewing Android Framework code in Android Studio is great, both in terms of index and interface, but when you track the code and find it in native logic, Android Studio has very poor support for native code, no indexing, no symbol search, no jumping, and so on, which is pretty frustrating. So how can I have fun viewing native code in the IDE? Source Insight also works well on Windows, but due to the fact that it is only supported on Windows and the interface is not very good, after a lot of struggle, we have found a way. Here we will build a silky native code reading environment step by step.

Take a look at the results:

2. CMake

In order for the IDE to index properly, we need the IDE to know the source files, header files, macro definitions, etc. Fortunately, we found that AOSP can generate this data for us during compilation. See androidxref.com/9.0.0_r3/xr…

According to the document, you only need to complete the following steps to automatically generate the corresponding CMake file for each module. As for what a Cmake file is, I won’t go into details here, but you can find out for yourself.

  1. Turn on the following two switches and cmakelists.txt will be automatically generated according to the compilation environment
export SOONG_GEN_CMAKEFILES=1
export SOONG_GEN_CMAKEFILES_DEBUG=1
Copy the code
  1. Start the compilation
make -j16
Copy the code

Or compile only the modules you need

make frameworks/native/service/libs/ui
Copy the code

The generated files are stored in the out directory, for example, the directory corresponding to the liBUI module just compiled is:

out/development/ide/clion/frameworks/native/libs/ui/libui-arm64-android/CMakeLists.txt
Copy the code
  1. Merging multiple modules

After CMake is generated, we see that the CMake file is generated by module. This will cause the IDE to import only one module, and it is not possible to see only one module’s code. What if we include multiple modules? We can in the out/development/ide/clion path to create a new CMakeLists. TXT file, and add the content:

# specify the lowest version of CMake
cmake_minimum_required(VERSION 3.6)
# specify project name, optional
project(aosp)
# Add the modules you need via add_subdirectory, note that the subdirectory must also contain cmakelists.txt
add_subdirectory(frameworks/native)
#add_subdirectory(frameworks/base/core/jni/libandroid_runtime-arm64-android)
Copy the code

This way, we merge multiple modules together and use the IDE to open the total CMake file

3. Import the IDE

Once the CMake file is generated, the rest of the work is done. Now there are many ides that can recognize CMake projects, you can choose according to your personal preference, for example:

  • CLion
  • Eclipse
  • Visual Studio
  • .

Here to CLion as an example to talk about how to import

  1. Open the CLion
  2. Select “New CMake Project from Sources”
  3. The specified containsCMakeLists.txtAs we said in the previous stepout/development/ide/clion(The cmakelists.txt directory contains multiple modules, remember?)
  4. Open Existing Project
  5. Enjoy your journey …

Of course, CLion also has a disadvantage, charge!! How can we use it for free

4. Some problems encountered

  • The generated cmakelists. TXT file may specify an absolute path, such as:set(ANDROID_ROOT /Volumes/AndroidSource/M1882_QOF7_base)If you copy cmakelists. TXT to another project, remember to correct the path
  • Note to Mac users, if your cmakelists. TXT is generated from the Linux platform, the generated cmakelists. TXT is specified in the c++ compilerset(CMAKE_CXX_COMPILER "${ANDROID_ROOT}/prebuilts/clang/host/linux-x86/clang-3977809/bin/clang++")The compiler specified here is linux-x86, remember to replace withdarwin-x86If clang++ does not exist in the corresponding directory, copy it from the AOSP source code
  • If the source file listed in CMake is not found in the project, CLion will stop indexing. If inconsistencies occur, remove the source file declaration from CMake

If you have any other problems, please feel free to contact us. Thank you

5. To summarize

To do a good job, you must sharpen your tools. The index created in this way covers all AOSP modules, but it also sets FLAGS and macros depending on the compilation environment.