Android NDK/JNI: Building a shared library that depends on other shared libraries

后端 未结 3 550
走了就别回头了
走了就别回头了 2021-02-01 06:29

I am writing an android app that wants to make JNI calls into a shared library built in using the NDK. The trick is this shared library calls functions provided by OTHER shared

3条回答
  •  误落风尘
    2021-02-01 07:10

    Not sure if this is exactly where you are at, but here's what I know about these sorts of things.

    1. Make each prebuilt libary its own separate Makefile. Multiple targets in Android.mk tends to get wonky. Sad.
    2. Include each make file using $(call import-add-path) and $(call import-module)
    3. Export as much as you can from the prebuilt's make files, using the LOCAL_EXPORT_ family of variables.

    Prebuilt Shared Library Android.mk

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    
    LOCAL_MODULE := my_module_name
    
    MY_LIBRARY_NAME := shared_library_name
    
    ### export include path
    LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
    
    ### path to library
    LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/lib$(MY_LIBRARY_NAME).so
    
    ### export dependency on the library
    LOCAL_EXPORT_LDLIBS := -L$(LOCAL_PATH)/libs/$(TARGET_ARCH_ABI)/
    LOCAL_EXPORT_LDLIBS += -l$(MY_LIBRARY_NAME)
    
    include $(PREBUILT_SHARED_LIBRARY)
    

    This is assuming that the prebuilt libaries live in a dir structure like this

    + SharedProjectFolderName
    +--- Android.mk
    +--- include/
    +-+- libs/$(TARGET_ARCH_ABI)/
      |- libshared_library_name.so
    

    If you are not building for multiple ABI, I guess you can leave that bit out

    The Project's Android.mk

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    
    LOCAL_MODULE := my_jni_module
    
    ## source files here, etc...
    
    ### define dependency on the other library
    LOCAL_SHARED_LIBRARIES := my_module_name
    
    include $(BUILD_SHARED_LIBRARY)
    
    $(call import-add-path,$(LOCAL_PATH)/path/to/myLibraries/)
    $(call import-module,SharedProjectFolderName)
    $(call import-module,AnotherSharedProject)
    

    I recommend you put all shared libraries in one folder. When you say $(call import-module,SharedProjectFolderName) it looks for a folder containing an Android.mk along the search path you told it (import-add-path)

    By the way, you probably shouldn't specify LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib. It should be finding the proper libs from NDK by itself. Adding more linker paths will probably confuse it. The proper way is to export the linker paths as flags from the sub-modules.

    ALSO, you can use ndk-build V=1 to get a ton of info on why it can't find paths, etc

提交回复
热议问题