[Android AIDL Series 2] Compile .aidl files in Android source code

Prepare the aidl file first, and the file directory results are as follows.

For the content of the aidl file, see: [Android AIDL Series 1: Manually compile the aidl file to generate Java, C++(android), C++(ndk), Rust interfaces]

$ tree
.
├── Android.bp
└── com
    └── my
        └── pkg
            ├── IMyServiceCallback.aidl
            └── IMyServiceInterface.aidl

Compile in the Android source code, the key is the Android.bp file. The aidl_interface compilation command is applicable, and can support 4 back-end language interfaces: Java, cpp, ndk, rust. Select Enable (enabled: true) as needed to generate an interface file for the corresponding language.

Compile and generate the back-end interface of ndk, and close other back-end language interfaces.

aidl_interface {
    
    
    name: "MyServiceAidlInterface",
    srcs: [ "com/my/pkg/*.aidl", ],
    unstable: true,
    backend: {
    
    
        java: {
    
    
            enabled: false,
            platform_apis: true,
        },
        cpp: {
    
    
            enabled: false,
        },
        ndk: {
    
    
            enabled: true,
            vndk: {
    
    
                enabled: false,
            },
        },
    // Android 12以下不支持编译生成rust后端
    //    rust: {
    
    
    //         // By default, the Rust backend is not enabled
    //         enabled: true,
    //    },
    },
}

file path in source code

The paths of .bp files and .aidl files in the Android source code are as follows, and this path is optional. The description is to find the generated interface file in the next step.
$android_source/vendor/service/my_service

Generate interface file path:
androidsource / out / soong / . intermediates / vendor / service / myservice This path is automatically generated by the system when compiling. The first half ( android_source/out/soong/.intermediates/vendor/service/my_service This path is automatically generated by the system at compile time. The first half (androidsource/out/soong/.intermediates/vendor/service/mysThe path of er v i ce is automatically generated by the system at compile time. The first half ( android_source/out/soong/.intermediates/) is the directory where the intermediate files generated by Android source code compilation are located, and the second half (vendor/service/my_service) is the directory of the corresponding .aidl file in the source code.

After compiling, 4 folders are generated, of which 2 folders with "-source" suffix contain header files and source code files, and the other two folders contain .o files during compilation and compiled dynamic libraries. so file.
The source codes in the MyServiceAidlInterface-ndk-source and MyServiceAidlInterface-ndk_platform-source directories are the same, and there is no difference in diff. The resulting binaries are different. (TODO The relationship between this and platform_apis is still uncertain. The backend of ndk does not have the property of platform_apis. Or is platform_apis must be enabled for ndk?)

Classification source code binary
MyServiceAidlInterface-ndk MyServiceAidlInterface-ndk-source MyServiceAidlInterface-ndk
MyServiceAidlInterface-ndk_platform MyServiceAidlInterface-ndk_platform-source MyServiceAidlInterface-ndk_platform

ndk generated results

# 生成以下4个文件夹
$ ls -1
MyServiceAidlInterface-ndk
MyServiceAidlInterface-ndk-source
MyServiceAidlInterface-ndk_platform
MyServiceAidlInterface-ndk_platform-source

$ find . -name *.cpp
./MyServiceAidlInterface-ndk-source/gen/com/my/pkg/IMyServiceCallback.cpp
./MyServiceAidlInterface-ndk-source/gen/com/my/pkg/IMyServiceInterface.cpp
./MyServiceAidlInterface-ndk_platform-source/gen/com/my/pkg/IMyServiceCallback.cpp
./MyServiceAidlInterface-ndk_platform-source/gen/com/my/pkg/IMyServiceInterface.cpp

$ find . -name *.h
./MyServiceAidlInterface-ndk-source/gen/include/aidl/com/my/pkg/BpMyServiceCallback.h
./MyServiceAidlInterface-ndk-source/gen/include/aidl/com/my/pkg/BpMyServiceInterface.h
./MyServiceAidlInterface-ndk-source/gen/include/aidl/com/my/pkg/IMyServiceInterface.h
./MyServiceAidlInterface-ndk-source/gen/include/aidl/com/my/pkg/BnMyServiceInterface.h
./MyServiceAidlInterface-ndk-source/gen/include/aidl/com/my/pkg/BnMyServiceCallback.h
./MyServiceAidlInterface-ndk-source/gen/include/aidl/com/my/pkg/IMyServiceCallback.h
./MyServiceAidlInterface-ndk_platform-source/gen/include/aidl/com/my/pkg/BpMyServiceCallback.h
./MyServiceAidlInterface-ndk_platform-source/gen/include/aidl/com/my/pkg/BpMyServiceInterface.h
./MyServiceAidlInterface-ndk_platform-source/gen/include/aidl/com/my/pkg/IMyServiceInterface.h
./MyServiceAidlInterface-ndk_platform-source/gen/include/aidl/com/my/pkg/BnMyServiceInterface.h
./MyServiceAidlInterface-ndk_platform-source/gen/include/aidl/com/my/pkg/BnMyServiceCallback.h
./MyServiceAidlInterface-ndk_platform-source/gen/include/aidl/com/my/pkg/IMyServiceCallback.h

The compilation result of the Java backend interface

$ ls -1
MyServiceAidlInterface-java
MyServiceAidlInterface-java-source

$ find . -name *.java
./MyServiceAidlInterface-java-source/gen/com/my/pkg/IMyServiceInterface.java
./MyServiceAidlInterface-java-source/gen/com/my/pkg/IMyServiceCallback.java

The situation of generating cpp and rust backends is similar and will not be repeated.

Guess you like

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