JNI初学记录

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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值