1.so库拷贝到相应目录

创建app/src/main/jniLibs目录,把so库拷贝到jniLibs目录。

2.Android上层调用so库

JavaCallJNI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11

public class JavaCallJNI {

    static  {
        System.loadLibrary("JNITest");
    }


    public static native int showValue(int value);

}

3.JNI接口定义

com_ksnowlv_hellojniforjava_JavaCallJNI.h

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

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

#include <jni.h>

/* Header for class com_ksnowlv_hellojniforjava_JavaCallJNI */

#ifndef _Included_com_ksnowlv_hellojniforjava_JavaCallJNI

#define _Included_com_ksnowlv_hellojniforjava_JavaCallJNI

#ifdef __cplusplus

extern "C" {

#endif
/*
 * Class:     com_ksnowlv_hellojniforjava_JavaCallJNI
 * Method:    showValue
 * Signature: (I)I
 */

JNIEXPORT jint JNICALL Java_com_ksnowlv_hellojniforjava_JavaCallJNI_showValue
  (JNIEnv *, jclass, jint);
  
  #ifdef __cplusplus
}

#endif

#endif

4..c实现文件

com_ksnowlv_hellojniforjava_JavaCallJNI.c内容如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15

#include "com_ksnowlv_hellojniforjava_JavaCallJNI.h"
#include "PersonExtension.hpp"
#include "JNILog.h"


JNIEXPORT jint JNICALL Java_com_ksnowlv_hellojniforjava_JavaCallJNI_showValue
  (JNIEnv * enc, jclass cls , jint value) {

    LOGI("jni value = %d",value);
	value = value + 1;
	LOGI("jni value +1 = %d",value);
    return value;
  }
  

5.Android中调用

1
2
      int value =  JavaCallJNI.showValue(1);
      Log.e("#### value ","" + value);