When we make use of jni on android, it's a error-prone task to manually write the native function name for a given java native function. Though there are rules for us to follow, no one would like to memorize that. javah to the rescue.
For example, for the following java class with a native method.
1 packagecom.rmd.jni;

2

3 importandroid.app.Activity;

4 importandroid.os.Bundle;

5

6 public classMain extendsActivity

7 {

8 /** Called when the activity is first created. */

9 @Override

10 public voidonCreate(Bundle savedInstanceState)

11 {

12 super.onCreate(savedInstanceState);

13 setContentView(R.layout.main);

14 }

15

16 publicnativebooleanJniMethod(inta);

17 }


We can follow steps below to generate a native header file:
  1. cd to project root folder
  2. Compile the project, e.g., using ant debug command
  3. run javah -jni -classpath bin/classes -d jni com.rmd.jni.Main (this command generates a native header file in jni directory, for com.rmd.jni.Main class)

And we get a header file com_rmd_jni_Main.h with content below:

1 /* DO NOT EDIT THIS FILE - it is machine generated */

2 #include <jni.h>

3 /* Header for class com_rmd_jni_Main */

4

5 #ifndef _Included_com_rmd_jni_Main

6 #define _Included_com_rmd_jni_Main

7 #ifdef __cplusplus

8 extern "C"{

9 #endif

10 /*

11 * Class: com_rmd_jni_Main

12 * Method:JniMethod

13 * Signature: (I)Z

14 */

15 JNIEXPORT jboolean JNICALL Java_com_rmd_jni_Main_JniMethod

16 (JNIEnv *, jobject, jint);

17

18 #ifdef __cplusplus

19 }

20 #endif

21 #endif


There are cases that we need to provide a method signature of a java function to the GetMethodID function in native code. javap can help us.
Consider the java class below, within which exists a JniCallback method. And this method is meant to be called from native code.

1 packagecom.rmd.jni;

2

3 importandroid.app.Activity;

4 importandroid.os.Bundle;

5

6 public classMain extendsActivity

7 {

8 /** Called when the activity is first created. */

9 @Override

10 public voidonCreate(Bundle savedInstanceState)

11 {

12 super.onCreate(savedInstanceState);

13 setContentView(R.layout.main);

14 }

15

16 publicbooleanJniCallback(inta);

17 }

After it's compiled, we can use javap -classpath bin/classes -s -p com.rmd.jni.Main command to find out the signature of JniCallback method. Shown below.
Compiled from "Main.java"
public class com.rmd.jni.Main extends android.app.Activity{
public com.rmd.jni.Main();
Signature: ()V
public void onCreate(android.os.Bundle);
Signature: (Landroid/os/Bundle;)V
public native boolean JniMethod(int);
Signature: (I)Z
}
Reference:
Java Native Interface Programming

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 为什么C和C++牛人多?
  2. 一个Java Application运行后,在系统中是作
  3. Java借助Runtime调用外部程序阻塞的代码
  4. 为泛型类的泛型方法的属性赋值 - Java
  5. Bootstrap3.0学习第二十二轮(JavaScript
  6. 堆排序(最大堆)
  7. 二维数组空指针异常
  8. java基础IO删除文件夹文件
  9. 泛型的通配符扩展
  10. 使用Java发出DNS请求并对响应进行计时