如何在 AOSP 项目中使用我预构建的 c++ static/shared 库

How to use my prebuilt c++ static/shared library in AOSP project

我想使用我的静态或共享预构建库 libmylib.so or libmylib.a。我的构建目标是 Hikey960 设备。我成功构建了纯 aosp 项目并闪存到 Hikey960 设备。接下来我想使用我的 libmylib.so.

修改 /frameworks/av/media/libaudioclient/AudioTrack.cpp

设置 1

我将 mylib 目录创建到 /system 并复制了预构建库。

|-- Andoird.mk
|-- arm64-v8a
|   |-- shared
|   |   `-- Release
|   |       `-- libmylib.so
|   `-- static
|       `-- Release
|           `-- libmylib.a
|-- armeabi
|   |-- shared
|   |   `-- Release
|   |       `-- libmylib.so
|   `-- static
|       `-- Release
|           `-- libmylib.a
|-- armeabi-v7a
|   |-- shared
|   |   `-- Release
|   |       `-- libmylib.so
|   `-- static
|       `-- Release
|           `-- libmylib.a
|-- include
|   `-- mylib.h
|-- x86
|   |-- shared
|   |   `-- Release
|   |       `-- libmylib.so
|   `-- static
|       `-- Release
|           `-- libmylib.a
`-- x86_64
    |-- shared
    |   `-- Release
    |       `-- libmylib.so
    `-- static
        `-- Release
            `-- libmylib.a

第 2 步

我在 /system/mylib 中创建了 Android.mk 文件,如下所示

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mylib
LOCAL_SRC_FILES := system/mylib/$(TARGET_ARCH_ABI)/shared/Release/libmylib.so
LOCAL_EXPORT_C_INCLUDES:= include
include $(PREBUILT_SHARED_LIBRARY)

步骤 3

我在 frameworks/av/media/libaudioclient/Android.bp 中向 shared_libs: [ 插入了 libmylib 行,如下所示

cc_library_shared {
    name: "libaudioclient",

    aidl: {
        export_aidl_headers: true,
        local_include_dirs: ["aidl"],
        include_dirs: [
            "frameworks/av/media/libaudioclient/aidl",
        ],
    },

    srcs: [
        // AIDL files for audioclient interfaces
        // The headers for these interfaces will be available to any modules that
        // include libaudioclient, at the path "aidl/package/path/BnFoo.h"
        ":libaudioclient_aidl_private",
        ":libaudioclient_aidl",

        "AudioEffect.cpp",
        "AudioRecord.cpp",
        "AudioSystem.cpp",
        "AudioTrack.cpp",
        "AudioTrackShared.cpp",
        "IAudioFlinger.cpp",
        "IAudioFlingerClient.cpp",
        "IAudioPolicyService.cpp",
        "IAudioPolicyServiceClient.cpp",
        "IAudioTrack.cpp",
        "IEffect.cpp",
        "IEffectClient.cpp",
        "ToneGenerator.cpp",
        "PlayerBase.cpp",
        "RecordingActivityTracker.cpp",
        "TrackPlayerBase.cpp",
    ],
    shared_libs: [
        "libaudiofoundation",
        "libaudioutils",
        "libaudiopolicy",
        "libaudiomanager",
        "libbinder",
        "libcutils",
        "libdl",
        "liblog",
        "libmedia_helper",
        "libmediametrics",
        "libmediautils",
        "libnblog",
        "libprocessgroup",
        "libutils",
        "libvibrator",
        "libmylib",
    ],
    export_shared_lib_headers: ["libbinder"],

    local_include_dirs: ["include/media", "aidl"],
    header_libs: [
        "libaudioclient_headers",
        "libbase_headers",
        "libmedia_headers",
    ],
    export_header_lib_headers: ["libaudioclient_headers"],

    // for memory heap analysis
    static_libs: [
        "libc_malloc_debug_backtrace",
    ],
    cflags: [
        "-Wall",
        "-Werror",
        "-Wno-error=deprecated-declarations",
    ],
    sanitize: {
        misc_undefined : [
            "unsigned-integer-overflow",
            "signed-integer-overflow",
        ],
    },
}

输出

很遗憾,构建失败:

error: frameworks/av/media/libaudioclient/Android.bp:39:1: "libaudioclient" depends on undefined module "libmylib"
06:32:39 soong bootstrap failed with: exit status 1

我认为在构建 aosp 时不包括我预构建的库文件。如何将我的库包含到 aosp 构建中。我应该在哪里指定 mylib?

而且我认为现在使用 soong 作为构建工具而不是 aosp 中的 nkd-build 所以我应该将上面创建的 Android.mk 文件更改为 Android.bp 吗?

如果你知道我的错误步骤,请告诉我该怎么做。谢谢。

关于Android.mk vs Android.bp 文件,学习Soong 可能是个不错的选择,因为Google 正在慢慢地将make 文件转换为新的构建系统。

如果您想开始使用 Soong,可以从这里开始:


关于添加自定义库。由于 Android 的项目高音定制应该在供应商图像中完成。如果你以后想更新 Android 框架,你会遇到更少的问题。 https://android-developers.googleblog.com/2017/05/here-comes-treble-modular-base-for.html

为了向供应商添加自定义库,您需要做几件事。 通过这些步骤,我成功地添加了预构建的共享 (.so) 库。仍然不知道如何将预构建静态 (.a) 库添加到系统。

  1. 为了将库添加到供应商分区,您需要创建列出所有添加的库的文本文件。由于 Android N (7.0) 或更高版本,应用程序只能访问特定 NDK 库白名单上的库。

    示例文件:

    • system/core/rootdir/etc/public.libraries.android.txt 主 Android
    • system/core/rootdir/etc/public.libraries.wear.txt 用于 Android 佩戴设备
    • system/core/rootdir/etc/public.libraries.iot.txt 用于 Android 物联网设备

我们可以为供应商创建对应文件,并在以下路径中创建它:/vendor/etc/public.libraries.txt

  1. Next 自 Android 8.0 (Oreo) 以来,vendor 中的本机库必须正确标记,以便应用程序可以访问它。我们需要更新 SELinux 文件并为资源文件添加适当的上下文,我们的库。

    • 创建目录vendor/[your_vendor]/sepolicy并添加"file_contexts"文件

    • 在 "file_contexts" -> vendor/lib(64)?/[your_lib_name]\.so u:object_r:same_process_hal_file:s0.

    • 中为我们的文件资源添加标签

    File context label follows form of user:role:type:sensitivity. If access is required by any apps (including third party apps), the library must be labeled as same_process_hal_file. This rule is defined in system/sepolicy/public/file.te

    • 记得在输入后添加新行。否则,下一个 "file_contexts" 文件的第一行将在构建期间连接起来,您将收到错误消息。

    After build you can check if your "file_contexts" was added properly. Output of all file_contexts files is in: out/target/product/generic_x86_64/obj/ETC/file_contexts.bin_intermediates/file_contexts.device.tmp. All entries are preceded with comment and path to file_contexts. In our case you should find in file comment "vendor/[your_vendor]/sepolicy/file_contexts".

  2. 在 [your_vendor] 目录中你应该有你的 product.mk 文件。您可以在 /device 中找到此文件的示例。更新您的 product.mk 文件并添加 BOARD_VENDOR_SEPOLICY_DIRS。 →

    BOARD_VENDOR_SEPOLICY_DIRS += \ vendor/[your_vendor]/sepolicy \

    In SELinux documentation it is stated that Vendor sepolicy are located under BOARD_SEPOLICY_DIRS paths, but in system/sepolicy/Android.mk we can read that this field is deprecated :

    "BOARD_SEPOLICY_DIRS was used for vendor/odm sepolicy customization before. It has been replaced by BOARD_VENDOR_SEPOLICY_DIRS (mandatory) ..."

  3. 将库作为产品包添加到您的 product.mk 文件中。

PRODUCT_PACKAGES += \ {your_library_name} \

  1. 构建并测试

现在您的库应该在 AOSP 中可见。

如果你想在供应商镜像中使用这个库,你需要在库.mk / 中指定它。根据 VNDK.bp 文件。

查看此 link 了解更多信息:

对我帮助很大!

我一直在努力解决的一件事是,我假设 64 位版本的库就足够了,但在我们的例子中,构建失败了,因为缺少 32 位版本的库。所以我创建了我们库的 32 位和 64 位版本,并将其包含到系统中,如下所示:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := [your_library_name]
LOCAL_VENDOR_MODULE := true
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
LOCAL_MODULE_SUFFIX := .so
LOCAL_SRC_FILES := arm64-v8a/[your_library_name].so
LOCAL_MULTILIB := 64
include $(BUILD_PREBUILT)

include $(CLEAR_VARS)
LOCAL_MODULE := [your_library_name]
LOCAL_VENDOR_MODULE := true
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
LOCAL_MODULE_SUFFIX := .so
LOCAL_SRC_FILES := armeabi-v7a/[your_library_name].so
LOCAL_MULTILIB := 32
include $(BUILD_PREBUILT)

此外,在我们的例子中,库将包含在 system 分区中,无论它在 vendor/external/[your_library_name] 下,添加 LOCAL_VENDOR_MODULE := true 修复了这个问题。