Android.bp Detailed Explanation + Essentials for Getting Started

Detailed explanation of Android. BP + essential for getting started

Article Directory

  • Detailed explanation of Android. BP + essential for getting started
    • I. Foreword
    • II. Template of Android. BP Document
        1. Compile the.jar package
        1. Compile apk
        • (1) Compile apk with apk?
        • (2) Compile apk with Java source code
        1. Compile dynamic library.so
        1. Compile the static library.a
        1. Summary of Android. Mk Compilation File
      • BuildType Summary for Compile Type:
      • Android system source code compilation Android. BP file mode:
    • III. Specific examples of Android. BP.
    • Four, the main attributes of Android. BP.
    • V. Summary
        1. Summary of the simple use of Android. BP.
        1. Detailed explanation of Android. BP.
        1. Other knowledge of Android. BP.
        • (1) Complete file of the comparison relationship between all bp attributes and mk attributes of Android:
        • (2) The module type and attribute definition strings compiled by the Android. BP file are in uppercase.
        • (3) Others

I. Foreword

The Android. BP is intended to replace the Android. Mk with a script file.

If you have studied Android.mk, it should be very easy to learn Android.bp. If you are not very familiar with it, you can learn to understand it.

Android. BP and Android. Mk are the same role, in the system source code used to compile the class library.jar, application files.apk, dynamic library.so, static library.a role. The key is the module type definition and the different attribute definitions.

The Android. BP file describes the modules that need to be built with terse json-like declarations.

A Android. BP file is a build script file introduced in Android 7.0 and later that describes and manages the compilation process for a project.

The Android. BP file is written using the Starlark syntax, which is a lightweight, Python-based scripting language.

The Android. BP file is used to define the modules and build rules and is more flexible and maintainable than the previous Android. Mk file.

Detailed introduction to Android. Mk-essential for getting started:

https://blog.csdn.net/wenzhi20102321/article/details/135631544

II. Template of Android. BP Document

1. Compile the.jar package

java_library {
    name: "mylibrary",
    srcs: ["src/**/*.java"],
    static_libs: ["lib1", "lib2"],
    javac_flags: ["-source 1.8", "-target 1.8"],
    aaptflags: ["--auto-add-overlay"],
    manifest: "AndroidManifest.xml",
    resource_dirs: ["res"],
    dex_preopt: {
        enabled: true,
    },
    dex_preopt_config: "dexpreopt.config",
    dex_preopt_uses_art: true,
    dex_preopt_omit_shared_libs: true,
    optional: true,
} 

2. Compile apk

(1) Compile apk with apk?

It is to add the system signature to the ordinary apk to become the function of the system apk.

From the latest Android 13 source code, the BP file does not see the compilation type corresponding to the mk file.

Specifically, you can view the string relationship file corresponding to the Android. Mk and Android. BP attribute conversion:

release\build\soong\androidmk\androidmk\android.go

The follow is that compiled module type definition in the BP films

//mkandbpCorrespondence between file compilation types
var moduleTypes = map[string]string{
    "BUILD_SHARED_LIBRARY":        "cc_library_shared",
    "BUILD_STATIC_LIBRARY":        "cc_library_static",
    "BUILD_HOST_SHARED_LIBRARY":   "cc_library_host_shared",
    "BUILD_HOST_STATIC_LIBRARY":   "cc_library_host_static",
    "BUILD_HEADER_LIBRARY":        "cc_library_headers",
    "BUILD_EXECUTABLE":            "cc_binary",
    "BUILD_HOST_EXECUTABLE":       "cc_binary_host",
    "BUILD_NATIVE_TEST":           "cc_test",
    "BUILD_HOST_NATIVE_TEST":      "cc_test_host",
    "BUILD_NATIVE_BENCHMARK":      "cc_benchmark",
    "BUILD_HOST_NATIVE_BENCHMARK": "cc_benchmark_host",

    "BUILD_JAVA_LIBRARY":             "java_library_installable", // will be rewritten to java_library by bpfix
    "BUILD_STATIC_JAVA_LIBRARY":      "java_library",
    "BUILD_HOST_JAVA_LIBRARY":        "java_library_host",
    "BUILD_HOST_DALVIK_JAVA_LIBRARY": "java_library_host_dalvik",
    "BUILD_PACKAGE":                  "android_app",
    "BUILD_RRO_PACKAGE":              "runtime_resource_overlay",

    "BUILD_CTS_EXECUTABLE":          "cc_binary",               // will be further massaged by bpfix depending on the output path
    "BUILD_CTS_SUPPORT_PACKAGE":     "cts_support_package",     // will be rewritten to android_test by bpfix
    "BUILD_CTS_PACKAGE":             "cts_package",             // will be rewritten to android_test by bpfix
    "BUILD_CTS_TARGET_JAVA_LIBRARY": "cts_target_java_library", // will be rewritten to java_library by bpfix
    "BUILD_CTS_HOST_JAVA_LIBRARY":   "cts_host_java_library",   // will be rewritten to java_library_host by bpfix
}

//Precompiled part
var prebuiltTypes = map[string]string{
    "SHARED_LIBRARIES": "cc_prebuilt_library_shared",
    "STATIC_LIBRARIES": "cc_prebuilt_library_static",
    "EXECUTABLES":      "cc_prebuilt_binary",
    "JAVA_LIBRARIES":   "java_import",
    "APPS":             "android_app_import",
    "ETC":              "prebuilt_etc",
} 

The front is the definition of the compiled type in the Android. Mk, and the right is the definition of the compiled module type in the Android. BP.

This function uses the BUILD \ _ PREBUILT type Android. Mk, and no replaceable type is found here.

After some exploration, it is found that the Android. BP can use the type of the keyword “android \ _ app \ _ import” “to precompile apk.

Compile the code sample:

android_app_import {
    name: "FileManager",
    apk: "FileManager.apk",
    privileged: true,
    //Use system signature
    certificate: "platform",
    //No need to overwrite signature,Use the original packaged signature,and the above can only2Choose one
    //presigned:true, 

} 
(2) Compile apk with Java source code
android_app {
    name: "myapp",
    srcs: [
        "path/to/your/app/src/**/*.java",
    ],
    resource_dirs: [
        "path/to/your/app/res",
    ],
    manifest: "path/to/your/app/AndroidManifest.xml",

    //Load class library
    static_libs: [
        "mylibrary",
    ],

    //Is the system signed?
    certificate: "platform",
    platform_apis: true,
    //Whether to generatepriv-appTable of contents
    privileged: true,

} 

3. Compile dynamic library.so

cc_library_shared {
    name: "mylibrary",
    srcs: [
        "path/to/your/library.c",
    ],
    shared_libs: [
        "lib1",
        "lib2",
    ],
    include_dirs: [
        "path/to/your/include",
    ],
    export_include_dirs: [
        "path/to/your/export_include",
    ],
    target: {
        android: {
            shared_libs: [
                "lib3",
                "lib4",
            ],
        },
    },
} 

In the example above, a CC \ _ library \ _ shared module named “mylibrary” is defined that contains the dynamic library source files (library. C) to be compiled.

Use the srcs attribute to specify the path to the source file.

Use the shared \ _ libs attribute to specify the other dynamic libraries (lib1, lib2) that this dynamic library depends on. These dependent libraries can be system libraries or other custom libraries.

Use the include \ _ dirs attribute to specify the path to the header directory for compilation.

Use the export \ _ include \ _ dirs attribute to specify the path to the directory of header files required to use the library in other modules.

The additional system libraries (lib3, lib4) required for linking this library on the Android platform are specified by the target. Android. Shared \ _ libs attribute.

Please modify and configure the relevant paths and properties according to your actual project path and configuration requirements to ensure consistency with your project.

4. Compile the static library.a

cc_library_static {
    name: "mylibrary",
    srcs: [
        "path/to/your/library.c",
    ],
    include_dirs: [
        "path/to/your/include",
    ],
    export_include_dirs: [
        "path/to/your/export_include",
    ],
} 

In the example above, a CC \ _ library \ _ static module named “mylibrary” is defined that contains the static library source files (library. C) to be compiled.

Use the srcs attribute to specify the path to the source file.

Use the include \ _ dirs attribute to specify the path to the header directory for compilation.

Use the export \ _ include \ _ dirs attribute to specify the path to the directory of header files required to use the library in other modules.

Please modify and configure the relevant paths and properties according to your actual project path and configuration requirements to ensure consistency with your project.

5. Summary of Android. Mk Compilation File

Compilation template:

BuildType { //(1)compile type
    name: "mylibrary", // (2)module name
    srcs: [
        "path/XXX", //(2)compiled source code
    ],
    //Set different properties and different values ​​based on type
}

BuildType2 { //(1)compile type
    name: "mylibrary2", // (2)module name
    srcs: [
        "path/XXX", //(2)compiled source code
    ],
}
。。。//the same onebpFiles can compile multiple modules。 

All Android. BP are adapted based on the above template.

Compile a type module, and add the attribute definition and related value in the middle. The setting of the attribute is the colon “:”.

Code comments. As with Java code, use//or/\ ** comment \ **/to indicate the identity of the comment.

There may be hundreds of lines in an actual BP file, which is usually the case when multiple modules are compiled.

Don’t be dizzy, just look at the code of your relevant module.

Each module has a module name name, and it is enough to find the adaptation modification of the beginning and end parts.

BuildType Summary for Compile Type:

encoding type andAndroid.mkandAndroid.bpcorresponding keywords in
1、apkdocument,mk:BUILD_PREBUILT,bp:android_app_import(There is no corresponding relationship with before!)
2、appcode,mk:BUILD_PACKAGE,bp:android_app
3、dynamic library,mk:BUILD_SHARED_LIBRARY,bp:cc_library_shared
4、static library,mk:BUILD_STATIC_LIBRARY,bp:cc_library_static
5、The code is compiled intoJarBag,mk:BUILD_JAVA_LIBRARY,bp:java_library_installable
6、jarPackage compiled into system,mk:BUILD_MULTI_PREBUILT,bp:java_import(There is no corresponding relationship with before!) 

Other compilation types can be viewed in android. Go files.

Android system source code compilation Android. BP file mode:

(1) In the source code release directory, enter make-j64 “name name”

(2) CD to the Android. BP module directory and enter “mm”

III. Specific examples of Android. BP.

 //(1)PrecompiledjarBag,loadjarBag
java_import {
  name: "libs",
  jars: ["app/libs/rxjava.jar", "app/libs/rxandroid.jar"","app/libs/zxing.jar"],
}


android_app {

    name: "SkgSettings",
    platform_apis: true,
    privileged: true,

    kotlincflags: ["-Xjvm-default=enable"],
    optimize: {
        proguard_flags_files: ["app/src/main/proguard.flags"],
    },



    libs: [
       // Soong fails to automatically add this dependency because all the
       // *.kt sources are inside a filegroup.
       "kotlin-annotations",
    ],

    static_libs: [
        "androidx.appcompat_appcompat",
        "kotlin-stdlib",
        "libs",
    ],

    manifest: "app/src/main/AndroidManifest.xml",

    srcs: ["app/src/main/java/**/*.java",
           "app/src/main/java/**/*.kt",
       "SystemUpdaterSDK/src/main/java/**/*.java",
       "SystemUpdaterSDK/src/main/aidl/**/*.aidl"],
    aidl: {
        local_include_dirs: ["SystemUpdaterSDK/src/main/aidl/"],
    },
    resource_dirs: ["app/src/main/res"],

} 

Four, the main attributes of Android. BP.

The following are some common predefined properties (module properties are not defined below):

  1. Name: Defines the name of the module, which is usually a unique identifier.
name: "my_module", 
  1. Srcs: specifies the source file of the module, which can be a file list.
srcs: ["file1.java", "file2.java"], 
  1. Deps: Specifies the dependency of the module, that is, the list of modules that depend on other modules.
deps: ["dependency_module1", "dependency_module2"], 
  1. Visibility: Specify the visibility of the module and determine which modules can access it.
visibility: ["//my/module:visible_module"], 
  1. Cflags, cppflags, ldflags: flags used to specify C/C + + compilation and linking.
cflags: ["-Wall", "-O2"],
cppflags: ["-DDEBUG"],
ldflags: ["-L/path/to/lib", "-lmylib"], 
  1. Shared \ _ libs, static \ _ libs: specifies the dependency between the dynamic link library and the static link library of the module.
shared_libs: ["lib1", "lib2"],  //Compile dependent dynamic librarieslib1andlib2
static_libs: ["lib3", "lib4"],  //Compile dependent static librarieslib3andlib4 
  1. Host \ _ supported, device \ _ supported: Specifies whether the module supports host build and target device build.
host_supported: true,
device_supported: true, 
  1. Installable: Specifies whether the module can be installed into the system image.
installable: true, 
  1. Product \ _ specific: specify to compile it and put it in the/product/directory (the default is to put it in the/system directory)
product_specific: true 
  1. Vendor: specify to compile it and put it in the/vendor/directory (the default is to put it in the/system directory)
vendor: true, 

These are some of the common premade attributes in Android. BP files. Each attribute serves a different purpose, and developers can use them depending on the type and requirements of the module. In addition, the Android build system supports a number of other properties that can be customized for specific build tasks and module types.

For more properties and their detailed descriptions, see the official documentation for the Android build system:

https://source.android.google.cn/docs/setup/build?hl=zh-cn

V. Summary

1. Summary of the simple use of Android. BP.

BuildType { //(1)compile type
    name: "mylibrary", // (2)module name
    srcs: [
        "path/XXX", //(2)compiled source code
    ],
    //Set different properties and different values ​​based on type
} 

The key is to determine the type of compilation and define the attributes.

Comments are used as in Java code, using double slashes//or/\ ** comments \ **/

2. Detailed explanation of Android. BP.

Here are some of the more official introductions of the Android. BP. If you are interested, you can have a look at the details.

A Android. BP file is a build script file introduced in Android 7.0 and later that describes and manages the compilation process for a project. Here are some details of the Android. BP file:

Overview: Android. BP files are written using the Starlark syntax, a lightweight, Python-based scripting language. The Android. BP file is used to define the modules and build rules and is more flexible and maintainable than the previous Android. Mk file.

Module Definition: Multiple modules can be defined in the Android. BP file, and each module has a unique module name. A module may be an executable file, a static library, a shared library, etc. Modules are defined by CC \ _ library, CC \ _ binary, and so on.

Source file definition: The srcs parameter is used in the Android. BP file to specify the source file for the module. You can use wildcards to match multiple files, such as \ [ “\ *.cpp” ] for all CPP files.

Compilation options: Compilation options can be specified in the Android. BP file, such as compiler and compilation flags. Set by parameters such as cflags, cppflags, and ldflags.

Dependencies: Module dependencies can be specified in the Android. BP file, that is, a module depends on other modules. Dependent shared or static libraries are specified by parameters such as shared \ _ libs and static \ _ libs.

Target file generation: The name and path of the generated target file can be specified in the Android. BP file. Set by parameters such as name, installable, and so on.

Other functions: The Android. BP file also supports other functions, such as specifying the source files to be compiled, excluding certain source files, specifying the compiler, linker, and so on. You can learn more about the functionality and usage by consulting the official documentation of the Android. BP file or the associated tutorial.

In summary, a Android. BP file is a build script file introduced in Android 7.0 and later to manage the compilation of a project. By writing a Android. BP file, you can define the compile options, dependencies, and generated object files for the module. This provides more flexibility in managing and organizing your project’s code and resources.

3. Other knowledge of Android. BP.

(1) Complete file of comparison relationship between all BP attributes and mk attributes of Android:

System source code directory: build/Soong/androidmk/androidmk/android. Go

http://aospxref.com/android-13.0.0_r3/xref/build/soong/androidmk/androidmk/android.go

(2) The module type and attribute definition strings compiled by the Android. BP file are in uppercase.

Android. Mk files compile module types and attribute definitions in uppercase.

An example comparison is as follows:

//Android.mk Example
LOCAL_PATH := (call my-dir)

include(CLEAR_VARS)
# Set the module name orapkname
LOCAL_MODULE := mylibrary
#Set different properties and different values ​​based on type
...

//Android.bp Example
BuildType { //(1)compile type
    name: "mylibrary", // (2)module name
    srcs: [
        "path/XXX", //(2)compiled source code
    ],
    ...//Set different properties and different values ​​based on type
} 
(3) Others

Overall, this is a more comprehensive introduction to the entry level of Android. BP and related content.

Specific development may be more complex than the above, such as the compilation of dynamic libraries, which are very different.

Compiling complex dynamic libraries before depends on too many things, and there will be a lot of problems.

May use some not commonly used attributes, and some attributes can not be found on the Internet, the official also did not introduce!

If this happens when compiling the system source code, you can search the code of the whole system globally, and you can refer to other modules to use the specific definition and value of this attribute. For example, in the case of the above pre-compiled apk, Baidu and ordinary search may not be able to search for the relevant content. By searching the code under the package directory globally through keywords, you can find the relevant types and specific parameter settings for reference.