创建JNI头文件

1. 创建JNI头文件

  在工程目录下输入:

javah -classpath bin/classes -d jni com.example.rgbir.rgbirJNI

   遇到的问题

error: cannot access com.example.rgbir.rgbirJNI
class file for com.example.rgbir.rgbirJNI not found
javadoc: error - Class com.example.rgbir.rgbirJNI not found.
Error: No classes were specified on the command line. Try -help.

解决方法:点击运行工程,重新在命令行输入命令即可。

原因: 重新生成class文件

2. 配置NDK路径

  参考文章: 创建eclipse针对NDK的联合编译环境。

3. 屏蔽语法检查错误

对代码分析(code analysis)进行屏蔽

位置: project-> properties ->c/c++ general ->code analysis

在选择“using project setting”下面的syntax and semantic error 进行屏蔽

4. 解决JNI层函数头文件《jni.h》 下的黄线

  因为CDT索引器没有找到路径,而NDK知道。

  进入项目属性页面,“c/c++ general” -》 “Paths and symbols”,右边“add...” 输入JNI相关头文件所在目录。比如“${env_var:Android_NDK}/platforms/android-18/arch-arm/usr/include”.

5 C++包含。h头文件

JNI文件夹下的c++程序包含。h文件, 。h文件中所有函数声明必须在如下宏中,否则会报 undefined reference to 的错误。

复制代码
#ifdef __cplusplus 
extern "C" { 
#endif

//一段代码

#ifdef __cplusplus 
} 
#endif
复制代码

6. java.lang.UnsatisfiedLinkError:Native method not found

 so文件编译生成后,运行时,有时候会遇到java.lang.UnsatisfiedLinkError: Native method not found问题。
   a. JNI方法头部大小写问题     
      在C++中,方法名:Java_com_XXX,而不是java_com_XXX。建议直接从生成的.h头文件直接复制方法名到C或者C++文件中。
   b. C++文件问题
        如果是C++文件(.cpp或者.cc),要使用extern "C" {   } 把本地方法括进去
   c. 往JNI方法中传值问题
         如,调用native方法sendSomeThing(Object object),如果传入的object为null,有可能会报上面错误。

7. SD卡中创建文件、写入文件、删除文件操作的支持

 在AndroidManifest.xml中加入访问SDCard的权限.如果支持网络, 麦克风等一样处理

原文地址:https://www.cnblogs.com/xubin-123/p/4307237.html