我已经构建了 arm android 共享库 (libtest.so)。我对重用一个函数很感兴趣(它没有太多依赖性——它只是创建类实例并调用两个方法)。我想调用该函数(它需要一个 std::string 参数)并获得返回值。
有可能做这样的事情吗?我没有任何头文件。
我试过这个Android.mk,我把libtest.so放在/jni
和/libs/armeabi
, /lib/armeabi
。此时我的 cpp
文件可以编译,但是现在呢?如果可能,我如何从 libtest.so 调用函数?我从 objdump 知道它的名字
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:= libtest
LOCAL_SRC_FILES := libtest.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.cpp
LOCAL_SHARED_LIBRARIES := libtest
include $(BUILD_SHARED_LIBRARY)
编辑:
我已尝试使用此 android.mk 添加来自 hello-jni 示例的预构建库:
include $(CLEAR_VARS)
LOCAL_MODULE:= libhello-jni
LOCAL_SRC_FILES := libhello-jni.so
include $(PREBUILT_SHARED_LIBRARY)
它成功了,但是 libtest.so
的相同代码显示了以下错误(启动时)
UnsatisfiedLinkError: Cannot load libtest.so: FindLibrary returned null
libtest.so 位于 libhello-jni.so 旁边的文件夹中(在设备上位于 /data/data/[package]/lib
)。可能有什么问题?
最佳答案
我有一个应用程序,我可以在其中执行类似于您需要的操作(或者可能正是您需要的操作)。
我有 *.so 文件形式的预编译库。 (例如 lib1.so、lib2.so 等), 它带有一些标题。
我创建了一个模块,它通过包含它们的 header 和 *.so 文件来使用预编译库。在示例中,我将其称为“libtestwrapper”。该模块定义了自己的源文件并可选择包含。可以为第二个模块导出模块功能(如果您提供头文件),如后所述。
我创建了第二个模块 (newModule),它通过将第一个模块 (libtestwrapper) 添加到“LOCAL_SHARED_LIBRARIES”来使用它。这使您之前导出的头文件(在“libtestwrapper”中)可供“newModule”使用。
这是我的 Android.mk 的内容:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libtestwrapper
LOCAL_SRC_FILES := libtestUsage.c # Use the methods of libtest.h here
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include # This is where libtest.h should be
# provide this line if you intend to export any header files to another module
#LOCAL_EXPORT_C_INCLUDES += $(LOCAL_PATH)/include # you may also use a different directory than 'include'
LOCAL_LDLIBS := -L$(LOCAL_PATH)/dir_with_libtest_so -libtest # -llog etc.
#optionally add any as needed: -llog -ljnigraphics -lz -ldl -lgcc
# '-libtest' corresponds to 'libtest.so' - the names must match
# -llog is for logcat for example
include $(BUILD_SHARED_LIBRARY)
# Optional:
# Define a second module wich is making use of the first one (i.e. libtestwrapper)
include $(CLEAR_VARS)
LOCAL_MODULE := newModule # this module will be making use of the first one (if needed)
# Add local source files. If the files are stored in directories
# you have to provide a relative path starting inside the 'jni' directory.
# The example is for this structure: jni/dirToSourceFiles1/*.cpp
LOCAL_SRC_FILES := dirToSourceFiles1/SourceFile1.cpp dirToSourceFiles1/SourceFile2.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)/newModule_include # path where the headers of this module are stored
LOCAL_SHARED_LIBRARIES += libtestwrapper # make use of the previous module
# Optionally add this line if any other libs should be used
#LOCAL_LDLIBS := -llog -ljnigraphics -lz -ldl -lgcc
include $(BUILD_SHARED_LIBRARY)
关于android - arm共享库的复用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22441029/