这里有一个简单的例子,它完成了我认为您正在尝试的事情。
两个库正在构建并打包到应用程序中,其中第二个库依赖于第一个库。我选择将lib1和lib2作为当前项目的一部分,但它们确实可以位于任何地方。
应用程序/CMakeLists。txt文件
cmake_minimum_required(VERSION 3.4.1)
add_subdirectory(src/main/cpp/lib1)
add_subdirectory(src/main/cpp/lib2)
app/src/main/cpp/lib1/CMakeLists。txt文件
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
native-lib1
SHARED
native-lib1.cpp )
target_include_directories(native-lib1 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
app/src/main/cpp/lib2/CMakeLists。txt文件
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
native-lib2
SHARED
native-lib2.cpp )
find_library(log-lib log)
# native-lib2 depends on log and native-lib1
target_link_libraries(native-lib2 ${log-lib} native-lib1)
app/src/main/cpp/lib1/native-lib1。H
#pragma once
int foo();
app/src/main/cpp/lib1/native-lib1。cpp公司
#include "native-lib1.h"
int foo()
{
return 42;
}
app/src/main/cpp/lib2/native-lib2。cpp公司
#include <jni.h>
#include <android/log.h>
#include <string>
#include "native-lib1.h"
extern "C" JNIEXPORT jstring JNICALL Java_com_example_cmaketwolibs_MainActivity_stringFromJNI(
JNIEnv *env, jobject thiz) {
// Call function from native-lib1
__android_log_print(ANDROID_LOG_WARN,
"native-lib2", "Calling native-lib1\'s foo(): %d",
foo());
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}