英文:
Include multiple library dependency in same project JNI
问题
我正在尝试使用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:
cmake_minimum_required(VERSION 3.4.1)
include_directories(Include)
add_library(
dependency
SHARED
dependency.cpp
)
add_library(
native-lib
SHARED
native-lib.cpp
)
target_link_libraries(
native-lib
${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs/armeabi-v7a/libdependency.so
)
这是Java代码:
public class MainActivity extends AppCompatActivity {
static {
System.loadLibrary("dependency");
System.loadLibrary("native-lib");
}
// 其他一些函数
}
注:不添加 target_link_libraries
来链接 libdependency.so
,认为Java的 LoadLibrary
会在运行时链接它,这样做不会构建成功,因为 native-lib
会有未定义的引用。
英文:
I'm trying to build an android app using JNI. The following is my C++ code structure:
main
-cpp
-native-lib.cpp
-dependency.h
-dependency.cpp
In my native-lib.cpp
I'm including dependency.h
. In my Cmake, I'm creating a shared library (.so) for dependency
and native-lib
so that I can load them in my Java part of the code.
While building the code however I cannot build native-lib
because it has a dependency on dependency
. For a quick fix, I'm building the dependency
first and then linking it to native-lib
and then loading the two in my java side. But I know it is the wrong way of doing things, hence I wanted to know the right way of adding multiple dependencies in JNI.
The following is my Cmake:
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)
Here is the Java code:
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: Not adding target_link_libraries
to link libdependency.so
thinking that Java LoadLibrary
will link it in real-time does not build as native-lib would have undefined references
答案1
得分: 1
在Android上,只有在平台版本低于API 21时,您才需要明确地调用loadLibrary("dependency")
。至于 CMake,
target_link_libraries(native-lib dependency)
应该可以工作,但是如果CMake缓存没有得到正确更新,您可能需要清理项目。
英文:
On Android, you only need to explicitly loadLibrary("dependency")
if the platform is older than API 21. As for CMake,
target_link_libraries(native-lib dependency)
should work, but you may need to clean the project if the CMake caches don't get properly updated.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论