如何在Android Studio 3.1.3上的“ ndk”项目中添加“ android”库?

时间:2018-07-24 12:32:06

标签: cmake android-ndk

在编写与音频相关的应用程序时遇到了这个问题:

Build command failed.
Error while executing process C:\Android_SDK\cmake\3.6.4111459\bin\cmake.exe with arguments {--build D:\workspace\AndroidAudioNDK\app\.externalNativeBuild\cmake\debug\x86_64 --target native-lib}
[1/1] Linking CXX shared library ..\..\..\..\build\intermediates\cmake\debug\obj\x86_64\libnative-lib.so
FAILED: cmd.exe /C "cd . && C:\android-ndk-r15c\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe  --target=x86_64-none-linux-android --gcc-toolchain=C:/android-ndk-r15c/toolchains/x86_64-4.9/prebuilt/windows-x86_64 --sysroot=C:/android-ndk-r15c/sysroot -fPIC -isystem C:/android-ndk-r15c/sysroot/usr/include/x86_64-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security  -std=c++11 -fexceptions -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a --sysroot C:/android-ndk-r15c/platforms/android-21/arch-x86_64 -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ..\..\..\..\build\intermediates\cmake\debug\obj\x86_64\libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o  -llog -lm "C:/android-ndk-r15c/sources/cxx-stl/gnu-libstdc++/4.9/libs/x86_64/libgnustl_static.a" && cd ."
D:\workspace\AndroidAudioNDK\app\src\main\cpp/native-lib.cpp:35: error: undefined reference to 'AAssetManager_fromJava'
D:\workspace\AndroidAudioNDK\app\src\main\cpp/native-lib.cpp:35: error: undefined reference to 'AAssetManager_open'
D:\workspace\AndroidAudioNDK\app\src\main\cpp/native-lib.cpp:35: error: undefined reference to 'AAsset_openFileDescriptor'
D:\workspace\AndroidAudioNDK\app\src\main\cpp/native-lib.cpp:44: error: undefined reference to 'AMediaExtractor_new'
...

我找到了解决此问题的方法:adding the android lib to LOCAL_LDLIBS,但是如何在最新的android studio(3.1.3)中做到这一点?我已经尝试了该答案下的其他解决方案,但它们对我都不起作用:构建错误仍然存​​在。

2 个答案:

答案 0 :(得分:1)

找到了解决方案!

在CMakeList.txt中: CMakeList.txt

修改为

cmake_minimum_required(VERSION 3.4.1)

add_library( native-lib SHARED
             src/main/cpp/native-lib.cpp )

target_link_libraries( native-lib
                       android
                       log )

然后错误消失了。

参考:

答案 1 :(得分:1)

LOCAL_LDLIBS用于ndk构建,但是您正在使用CMake。 one of our samples是一个很好的起点,向您表明您需要使用target_link_libraries