Android studio自带例子编译出现:
错误1:Error:FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
Error while executing 'E:\adt-bundle-windows-x86_64-20131030\sdk\cmake\3.6.3155560\bin\cmake.exe' with arguments {--build C:\Users\dell\AndroidStudioProjects\MyJNIExample1\app\.externalNativeBuild\cmake\debug\mips64 --target native-lib}
[1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o
[2/2] Linking CXX shared library ..\..\..\..\build\intermediates\cmake\debug\obj\mips64\libnative-lib.so
FAILED: cmd.exe /C "cd . && E:\adt-bundle-windows-x86_64-20131030\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=mips64el-none-linux-android --gcc-toolchain=E:/adt-bundle-windows-x86_64-20131030/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/windows-x86_64 --sysroot=E:/adt-bundle-windows-x86_64-20131030/sdk/ndk-bundle/sysroot -fPIC -isystem E:/adt-bundle-windows-x86_64-20131030/sdk/ndk-bundle/sysroot/usr/include/mips64el-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fintegrated-as -Wa,--noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a --sysroot E:/adt-bundle-windows-x86_64-20131030/sdk/ndk-bundle/platforms/android-21/arch-mips64 -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\mips64\libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -llog -lm "E:/adt-bundle-windows-x86_64-20131030/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/mips64/libgnustl_static.a" && cd ."
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o: In function `Java_com_example_dell_myjniexample1_MainActivity_getCount':
C:\Users\dell\AndroidStudioProjects\MyJNIExample1\app\src\main\cpp/native-lib.cpp:7: undefined reference to `HelloImp::getCount()'
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
解决:配置CMakeList.txt, 加入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). # Associated headers in the same location as their source # file are automatically included. src/main/cpp/native-lib.cpp src/main/cpp/HelloImp.cpp)错误2:调试时出现
06-14 16:44:10.346 D/AndroidRuntime(13618): Shutting down VM
06-14 16:44:10.347 E/AndroidRuntime(13618): FATAL EXCEPTION: main
06-14 16:44:10.347 E/AndroidRuntime(13618): Process: com.example.dell.myjniexample1, PID: 13618
06-14 16:44:10.347 E/AndroidRuntime(13618): java.lang.UnsatisfiedLinkError: No implementation found for java.lang.String com.example.dell.myjniexample1.MainActivity.stringFromJNI() (tried Java_com_example_dell_myjniexample1_MainActivity_stringFromJNI and Java_com_example_dell_myjniexample1_MainActivity_stringFromJNI__)
解决:由于C和C++,定义有所不同
JNI接口 都是以C的方式定义的,现在使用C++实现,函数定义前是否需要加上extern "C"呢?为此定义了一个头文件,在CPP文件中include该头文件,头文件加上如下代码片断:
#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif