无法在Android Studio中使用CMake链接库:无法加载本机库

时间:2017-04-19 16:33:15

标签: android c++ cmake

只要我想通过target_link_libraries(...)添加我自己的库,我的应用程序崩溃就会出现以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: my.process, PID: 3651
java.lang.RuntimeException: Unable to start activity ComponentInfo{my.process/android.app.NativeActivity}: java.lang.IllegalArgumentException: Unable to load native library: /data/app/my.process/lib/x86/libnative-activity.so

src/cpp/main/CMakeLists.txt中的我的CMake文件如下所示:

cmake_minimum_required(VERSION 3.4.1)

# build native_app_glue as a static lib
add_library(app-glue STATIC
            ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)

# now build app's shared lib
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

# Native-Activity
add_library(native-activity SHARED "")

# Native-Library
add_library(native-lib SHARED "")

include("Native/CMakeLists.txt")

# Link the native-activity
target_sources(native-activity PRIVATE "${CMAKE_CURRENT_LIST_DIR}/main.cpp")

target_include_directories(native-activity PRIVATE
                           ${ANDROID_NDK}/sources/android/native_app_glue)

# add lib dependencies
target_link_libraries(native-activity
                      android
                      app-glue
                      log)

target_link_libraries(native-activity native-lib)

并且src/cpp/main/Native/CMakeLists.txt中的CMake文件如下所示:

set_target_properties(native-lib PROPERTIES LINKER_LANGUAGE CXX)

如果我从src/cpp/main/CMakeLists.txt编译工作中的第一个CMake文件中删除以下行:

target_link_libraries(native-activity native-lib)

我做错了什么?

1 个答案:

答案 0 :(得分:1)

对我有用的是使用find_library(...) CMake命令并使用完整路径将库 native-lib 链接到 native-activity

工作的CMake文件如下所示:

cmake_minimum_required(VERSION 3.4.1)

# build native_app_glue as a static lib
add_library(app-glue STATIC
            ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)

# now build app's shared lib
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

# Native-Activity
add_library(native-activity SHARED "")

# Native-Library
add_library(native-lib SHARED "")

include("Native/CMakeLists.txt")

# Link the native-activity
target_sources(native-activity PRIVATE "${CMAKE_CURRENT_LIST_DIR}/main.cpp")

target_include_directories(native-activity PRIVATE
                           ${ANDROID_NDK}/sources/android/native_app_glue)

# add lib dependencies
target_link_libraries(native-activity
                      android
                      app-glue
                      log)

find_library(native-lib my-native-lib)

target_link_libraries(native-activity ${my-native-lib})