使用JNI在C中调用java代码
1.定义个类来定义java的native方法
public class DataProvider {
static{
System.loadLibrary("Hello");
}
public native void callCcode();
public native void callCcode2();
public native void callCcode3();
//C调用java空方法
public void helloFromJava(){
System.out.println("hello from java");
}
//C调用java中的带两个int参数的方法
public static int Add(int x,int y){
System.out.println("java "+ (x+y));
return x+y;
}
//C调用java中参数为string的方法
public void printString(String s){
System.out.println("java "+ s);
}
}
2.用javah编译那个native的类.
3.新建jni的文件夹.把刚刚编译生成的.h的头文件给拷贝进来.
4.在jni的文件夹下新建XX.c文件
5.编写c代码
#include <stdio.h>
#include <jni.h>
#include "com_mccxxiv_ndk4_DemoActivity.h"
#include <android/log.h>
#define LOG_TAG "System.out"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
JNIEXPORT void JNICALL Java_com_mccxxiv_ndk4_DataProvider_callCcode
(JNIEnv * env, jobject obj){
// new 出来java的对象 然后调用java对象里面的方法
char* classname = "com/mccxxiv/ndk4/DataProvider";
jclass clazz; //定义一个java中的类
clazz = (*env)->FindClass(env,classname); //该方法返回一个类
if(clazz==0){
LOGI("CAN'T FIND CLAZZ ");
}else{
LOGI("FIND CLASS");
}
//该方法返回指定类中的指定方法
//最后一个参数是方法的签名.
//获取方法的签名是用dos进到工程的classes目录下用javap -s 包名+类名
jmethodID java_method = (*env)->GetMethodID(env,clazz,"helloFromJava","()V");
if(java_method==0){
LOGI("CAN'T FIND java_method ");
}else{
LOGI("FIND java_method");
}
//调用java中无返回值的方法.第二个参数obj为类的对象.
//所以把native的方法放到和方法在同一个类中(DataProvider)
//如果native方法和c要调用的java方法不在同一个类中.在c代码中需调用AllocObject()方法来手动的指定obj对象
//方法的返回值不同调用的CallXXXMethod中的XXX不同.返回值是什么类型XXX就是什么.
//静态的方法也是一样的.XXX就是StaticXXX.少一个obj的参数.
(*env)->CallVoidMethod(env,obj,java_method);
}
JNIEXPORT void JNICALL Java_com_mccxxiv_ndk4_DataProvider_callCcode2
(JNIEnv * env, jobject obj){
char* classname = "com/mccxxiv/ndk4/DataProvider";
jclass clazz;
clazz = (*env)->FindClass(env,classname);
if(clazz==0){
LOGI("CAN'T FIND CLAZZ ");
}else{
LOGI("FIND CLASS");
}
jmethodID java_method = (*env)->GetMethodID(env,clazz,"Add","(II)I");
if(java_method==0){
LOGI("CAN'T FIND java_method ");
}else{
LOGI("FIND java_method");
}
jint result = (*env)->CallIntMethod(env,obj,java_method,5,8);
LOGI("c daima %d",result);
}
JNIEXPORT void JNICALL Java_com_mccxxiv_ndk4_DataProvider_callCcode3
(JNIEnv * env, jobject obj){
char* classname = "com/mccxxiv/ndk4/DataProvider";
jclass clazz;
clazz = (*env)->FindClass(env,classname);
if(clazz==0){
LOGI("CAN'T FIND CLAZZ ");
}else{
LOGI("FIND CLASS");
}
jmethodID java_method = (*env)->GetMethodID(env,clazz,"printString","(Ljava/lang/String;)V");
if(java_method==0){
LOGI("CAN'T FIND java_method ");
}else{
LOGI("FIND java_method");
}
(*env)->CallVoidMethod(env,obj,java_method,(*env)->NewStringUTF(env,"haha from c"));
}
6.在jni文件夹下新建Android.mk文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS) #类似于工具初始化的操作
LOCAL_MODULE := Hello
LOCAL_SRC_FILES := Hello.c
LOCAL_LDLIBS += -llog
include $(BUILD_SHARED_LIBRARY)
7.用ndk-build命令编译
8.在主Activity中调用native方法
public class DemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void click(View view){
DataProvider provider = new DataProvider();
provider.callCcode();
}
public void click2(View view){
DataProvider provider = new DataProvider();
provider.callCcode2();
}
public void click3(View view){
DataProvider provider = new DataProvider();
provider.callCcode3();
}
}