我正在尝试使用jni构建一个android应用程序。以下是我的c++代码结构:
main
-cpp
-native-lib.cpp
-dependency.h
-dependency.cpp
在我的 native-lib.cpp
我包括 dependency.h
. 在我的cmake中,我正在为创建一个共享库(.so) dependency
以及 native-lib
这样我就可以把它们加载到我的java代码中。
但是我无法构建代码 native-lib
因为它依赖于 dependency
. 为了快速修复,我正在构建 dependency
先将其链接到 native-lib
然后在java端加载这两个。但是我知道这样做是错误的,因此我想知道在jni中添加多个依赖项的正确方法。
以下是我的简历:
cmake_minimum_required(VERSION 3.4.1)
include_directories(Include)
add_library( # Sets the name of the library.
dependency
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
dependency.cpp )
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp )
# Once the so is being built I'm adding it myself and linking it.
# I don't want to do this but link it automatically
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs/armeabi-v7a/libdependency.so)
以下是java代码:
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("dependency");
System.loadLibrary("native-lib");
}
// Some other functions
}
ps:不添加 target_link_libraries
链接 libdependency.so
认为java LoadLibrary
将实时链接它,而不是像本机库那样生成未定义的引用
1条答案
按热度按时间zmeyuzjn1#
在android上,您只需要显式地
loadLibrary("dependency")
如果平台早于api 21。至于cmake,应该可以工作,但是如果没有正确更新cmake缓存,您可能需要清理项目。