Android --NDK(C调Java)

Java代码:

package com.picovr.javacall;

import android.util.Log;


public class NativeMethord {

    static {

        System.loadLibrary("native_jni");
    }

    public void helloFromJava() {

        Log.i("NativeMethord", "helloFromJava");

    }

    public int add_number(int x, int y) {

        Log.i("NativeMethord", "add_number add :" + x + "  y:" + y);
        return x + y;
    }
}

C代码:

JNIEXPORT jint JNICALL Java_com_picovr_javacall_NativeMethord_addNumberCallback
        (JNIEnv *env, jobject jobj) {

    //1.得到字节码
    jclass jclass1 = (*env)->FindClass(env, "com/picovr/javacall/NativeMethord");

    //2.得到方法ID
    jmethodID jmethodID1 = (*env)->GetMethodID(env, jclass1, "add_number","(II)I");
    
    //3.实例化类
    jobject jobject1 = (*env)->AllocObject(env, jclass1);

    //调用方法
    jint result = (*env)->CallIntMethod(env, jobject1, jmethodID1, 20, 30);
    LogI("result :%d", result);
}

/*
 *调用java中的helloFromJava方法
 */
JNIEXPORT jint JNICALL Java_com_picovr_javacall_NativeMethord_getStringCallback
        (JNIEnv *env, jobject jobj) {

    //1.得到字节码
    jclass jclass1 = (*env)->FindClass(env, "com/picovr/javacall/NativeMethord");

    //2.得到方法ID
    jmethodID jmethodID1 = (*env)->GetMethodID(env, jclass1, "helloFromJava","()V");

    //3.实例化类
    jobject jobject1 = (*env)->AllocObject(env, jclass1);

    //调用方法
    (*env)->CallVoidMethod(env, jobject1, jmethodID1);
    LogI("Java_com_picovr_javacall_NativeMethord_getStringCallback");
}

你可能感兴趣的:(Android --NDK(C调Java))