Use Clion to develop Android AOSP native code (complete operation steps)

1. Preparation

First prepare a native project for demonstration, named clion_demo.
Project directory: aosp/vendor/clion_demo

1. File directory structure

aosp/vendor/clion_demo$ tree
.
├── Android.bp
└── clion_demo.cpp

2. Android.bp file

Source code file: clion_demo.cpp
Target executable file: clion_demo

cc_binary {
    
    
    name: "clion_demo",

    shared_libs: [
        "libbase",
        "libprocessgroup",
        "libcutils",
        "libutils",
        "libbinder",
        "liblog",
        "libziparchive",
    ],

    srcs: [
        "clion_demo.cpp",
    ],
}

3. Native source file: clion_demo.cpp

Some methods in the Android native source code are referenced in the code, the purpose is to make the generated CMake project include more include.
If the relevant class or method is not referenced in the code, the header file of the corresponding library will not be included. There is a comparison below.
If only one printf is called in the code, the generated CMake project will contain

#pragma GCC diagnostic warning "-Wunused"
#define LOG_TAG "clion_demo"

#include <string>

#include <android-base/file.h>
#include <android-base/stringprintf.h>
#include <utils/Log.h>

int main(int /* argc */, char **/* argv */)
{
    
    
    std::string msg = android::base::StringPrintf("Hello AOSP~");
    ALOGD("The message is: %s.", msg.data());
    return 0;
}

( The following compilation and running steps are not necessary to prepare the CLion project, but only to verify that the code is compiled correctly )
Execute compilation directly in the project directory:

$ mm

After the compilation is complete, the executable file generates the directory:

aosp/out/target/product/hqcos/system/bin/clion_demo

Push to the development board to run the test:

adb push clien_demo /system/bin/
$ adb shell
# clion_demo                                                      
# logcat *:S clion_demo
03-11 19:54:09.307 28971 28971 D clion_demo: The message is: Hello AOSP~.

4. Summary of preparatory work

Prepare a project compiled with Android.bp.
Existing projects can also be used.
Using a simple demo program is equivalent to generating an empty template project, and the program can be developed from scratch.

2. Generate CMake project

1. Turn on the compilation switch

Through environment variables, open the compilation and development that generates CMakeLists.txt

$ export SOONG_GEN_CMAKEFILES=1
$ export SOONG_GEN_CMAKEFILES_DEBUG=1

2. Compile the project

$ mm

3. Import the generated CMakeLists.txt in CLion

After the compilation is complete, find the generated CMakeLists.txt file and save it in the following directory:

aosp_dir/out/development/ide/clion/

The relative path of each project in the directory is consistent with the root directory of aosp.
For example:
the source code directory is:

aosp/system/core/liblog

The corresponding CMakeLists.txt file is saved in:

aosp_dir/out/development/ide/clion/system/core/liblog/

There will be multiple directories under this directory, corresponding to different hardware platforms, such as arm64, x86, etc. Depends on configuration in bp file.

4. View the imported project in CLion

In the project view on the left, you can see the imported CMakeLists.txt and clion_demo.cpp files, and a large number of include header files below.
Expand some header files, and you can see that it includes C language library, C++ library (a few include above, the content is too much to shrink) and many libraries in the Android source code.
At this time, in the editing window, you can click to jump, or you can automatically prompt for completion when typing.
insert image description here
In contrast, if there are fewer libraries referenced in the source code, there will be relatively fewer includes in the generated project.
For example, look at the source code:

// clion_empty.cpp
#pragma GCC diagnostic warning "-Wunused"
#include <stdio.h>
int main(int /* argc */, char **/* argv */)
{
    
    
    printf("hello LION!");
    return 0;
}

In the figure below, the left side is the project generated by clion_demo.cpp above, and the right side is the project generated by clion_empty.cpp. It can be seen that the number of include is significantly different.
Even with such a simple file as clion_empty.cpp, in addition to the C/C++ library, most of the basic Android native and basic libraries in the source code have been included.
In the development process, on this basis, add the required dependent libraries, and directly modify the CMakeLists.txt file.
insert image description here

3. Summarize

1. Operation steps

  1. There is already a project that can be compiled normally with Android.bp.
  2. Turn on the environment variable switch.
  3. Compile the project in aosp and generate the CMakeLists.txt file.
  4. Import and use in CLion.

2. Motivation

CLion is better than vs code, CLion only supports CMake projects.

3. Principle

AOSP supports automatic generation of CMakeLists.txt file during compilation, just import it into CLion. The default switch is off.

4. Restrictions

1) Only projects compiled with Android.bp can be used, and projects compiled with Android.mk files cannot be used.
2) It can only be imported project by project, and does not support importing the entire AOSP project. You need to manually create a CMake project and import the required projects.

Reference document: aosp/build/soong/docs/clion.md

Guess you like

Origin blog.csdn.net/yinminsumeng/article/details/129522402