Andorid 使用bp或者mk编译C文件生成so
2023-12-22 12:35:24
在Aosp源码里编译C文件生成so
使用mk编译
文件夹列表
CMkDemo/Android.mk
CMkDemo/cpp/SerialPort.c
CMkDemo/cpp/SerialPort.h
Android.mk 内容如下
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
# All of the source files that we will compile.
LOCAL_SRC_FILES:= \
cpp/SerialPort.c
# All of the shared libraries we link against.
LOCAL_SHARED_LIBRARIES := \
libutils liblog
LOCAL_HEADER_LIBRARIES += jni_headers
# No static libraries.
LOCAL_STATIC_LIBRARIES :=
# Also need the JNI headers.
#LOCAL_C_INCLUDES += \
# $(JNI_H_INCLUDE)
# This is the target being built.
LOCAL_MODULE:= lib_cmkdemo
LOCAL_SYSTEM_EXT_MODULE := true
LOCAL_MULTILIB := 64
# No special compiler flags.
LOCAL_CFLAGS +=
include $(BUILD_SHARED_LIBRARY)
最终生成 ./out/target/product/<device_name>/system/system_ext/lib64/lib_cmkdemo.so
如果去掉 LOCAL_MULTILIB := 64 ,会生成
./out/target/product/<device_name>/system/system_ext/lib/lib_cmkdemo.so
./out/target/product/<device_name>/system/system_ext/lib64/lib_cmkdemo.so
使用bp编译
文件夹列表
CBpDemo/Android.bp
CBpDemo/cpp/SerialPort.c
CBpDemo/cpp/SerialPort.h
Android.bp 内容如下
cc_library_shared {
name: "lib_cbpdemo",
system_ext_specific: true,
compile_multilib: "64",
srcs: [
"cpp/*.c",
"cpp/*.cpp",
],
include_dirs: [
],
header_libs: [
"jni_headers",
],
shared_libs: [
"liblog",
]
}
- 行首空一行,否则编译报错
error: missing separator.
; - cc_library_shared { } :指明编译的是静态so库;
- name: “lib_cbpdemo” :指明 module 名是 lib_cbpdemo ,最终会生成 lib_cbpdemo.so ;
- system_ext_specific: true :编译产物在 system_ext 分区,本例对应 ./out/target/product/<device_name>/system/system_ext/lib64/lib_cbpdemo.so ;
- compile_multilib :指明编译的是 32 还是 64 位的;
- srcs :源码文件夹或者路径,多个层级可以用类似写法 src/**/*.c ;
- include_dirs :引用外部的源文件,要写完整路径;
- header_libs :
- shared_libs :引用的其他静态 so 库,本例引用的是 liblog.so ;
编译生成的so给其他module引用
场景: 需要把C文件集成到 Test.apk 的源码里,编译生成的 Test.apk 里有对应的 lib_xx.so ,
Android.mk
Test.apk 的 Android.mk ,
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_PACKAGE_NAME := Test
LOCAL_MODULE_TAGS := optional
LOCAL_CERTIFICATE := platform
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_DEX_PREOPT := false
LOCAL_SYSTEM_EXT_MODULE := true
LOCAL_PROGUARD_FLAG_FILES := proguard.flags
LOCAL_PRIVATE_PLATFORM_APIS := true
LOCAL_PRIVILEGED_MODULE := true
LOCAL_JAVA_LIBRARIES := \
xxx-framework \
LOCAL_STATIC_JAVA_LIBRARIES += \
SettingsLib
include $(BUILD_PACKAGE)
把 C 文件放到 src/com/test/cpp/ 下,添加 patch 如下,
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_PACKAGE_NAME := Test
LOCAL_MODULE_TAGS := optional
LOCAL_CERTIFICATE := platform
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_DEX_PREOPT := false
LOCAL_SYSTEM_EXT_MODULE := true
LOCAL_PROGUARD_FLAG_FILES := proguard.flags
LOCAL_PRIVATE_PLATFORM_APIS := true
LOCAL_PRIVILEGED_MODULE := true
LOCAL_JAVA_LIBRARIES := \
xxx-framework \
LOCAL_STATIC_JAVA_LIBRARIES += \
SettingsLib
############### patch begin ################
LOCAL_JNI_SHARED_LIBRARIES:= lib_cmkdemo
############### patch end ################
include $(BUILD_PACKAGE)
############### patch begin ################
#LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
# This is the target being built.
LOCAL_MODULE:= lib_cmkdemo
LOCAL_SYSTEM_EXT_MODULE := true
# All of the source files that we will compile.
LOCAL_SRC_FILES:= \
src/com/test/cpp/SerialPort.c
# Also need the JNI headers.
LOCAL_C_INCLUDES += \
$(JNI_H_INCLUDE)
# All of the shared libraries we link against.
LOCAL_SHARED_LIBRARIES := \
libutils liblog
# No static libraries.
LOCAL_STATIC_LIBRARIES :=
#LOCAL_MULTILIB := 64
# No special compiler flags.
LOCAL_CFLAGS +=
include $(BUILD_SHARED_LIBRARY)
############### patch end ################
Android.bp
Test.apk 的 Android.bp ,
android_app {
name: "Test",
certificate: "platform",
srcs: ["src/**/*.java"],
dex_preopt: {
enabled: false,
},
system_ext_specific: true,
optimize: {
proguard_flags_files: ["proguard.flags"],
},
platform_apis: true,
privileged: true,
libs: [
"xxx-framework",
],
static_libs: ["SettingsLib"],
}
把 C 文件放到 src/com/test/cpp/ 下,添加 patch 如下,
android_app {
name: "Test",
certificate: "platform",
srcs: ["src/**/*.java"],
dex_preopt: {
enabled: false,
},
system_ext_specific: true,
optimize: {
proguard_flags_files: ["proguard.flags"],
},
platform_apis: true,
privileged: true,
libs: [
"xxx-framework",
],
static_libs: ["SettingsLib"],
//############## patch begin ################
jni_libs: ["lib_cbpdemo"],
//############## patch end ################
}
//############## patch begin ################
//LOCAL_PATH:= $(call my-dir)
cc_library_shared {
// This is the target being built.
name: "lib_cbpdemo",
system_ext_specific: true,
// All of the source files that we will compile.
srcs: ["src/com/test/cpp/SerialPort.c"],
// Also need the JNI headers.
include_dirs: JNI_H_INCLUDE,
// All of the shared libraries we link against.
shared_libs: [
"libutils",
"liblog",
],
// No static libraries.
static_libs: [],
//LOCAL_MULTILIB := 64
// No special compiler flags.
cflags: [],
}
//############## patch end ################
文章来源:https://blog.csdn.net/weixin_44021334/article/details/135147882
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!