当前位置:   article > 正文

Andorid 使用bp或者mk编译C文件生成so_android.mk 编译so库

android.mk 编译so库

在Aosp源码里编译C文件生成so

使用mk编译

文件夹列表

CMkDemo/Android.mk
CMkDemo/cpp/SerialPort.c
CMkDemo/cpp/SerialPort.h
  • 1
  • 2
  • 3

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

最终生成 ./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
  • 1
  • 2
  • 3

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",
    ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 行首空一行,否则编译报错 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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

把 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 ################
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

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"],
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

把 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 ################

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/526372
推荐阅读
相关标签
  

闽ICP备14008679号