Android learning note _ JNI_c calls java code

Source: Internet
Author: User

1. Compile native METHOD (java2c) and non-native method (c2java ):

Package com. example. provider; public class CallbackJava {// C call the java empty method public void helloFromJava () {System. out. println ("hello from java");} // C calls the public int Add (int x, int y) method with two int parameters in java {int result = x + y; system. out. println ("java result =" + result); return result;} // C calls the public void printString (string s) {System. out. println ("java" + s);} // static method public static void printStaticStr (String s) {System. out. println ("java static->" + s) ;}// let the c code call the corresponding java code public native void callHelloFromJava (); public native void callAdd (); public native void callPrintString (); // call a static c code public native void callPrintStaticStr ();}

2. Generate a signature using the javah command and generate a native method signature using the javap command:

  

3. c code

FindClass: Find the class file in java
GetMethodID: locate the method to be called based on the class and method signature.
# Include <stdio. h> # include <jni. h> # include <malloc. h> # include "com_example_provider_CallbackJava.h"; # include <android/log. h> # define LOG_TAG "System. out. c "# define LOGD (...) _ android_log_print (ANDROID_LOG_DEBUG, LOG_TAG, _ VA_ARGS _) # define LOGI (...) _ android_log_print (ANDROID_LOG_INFO, LOG_TAG, _ VA_ARGS _) jmethodID getMethodId (JNIEnv * env, char * methodname, char * signname) {jclass jclazz = (* env) -> FindClass (env, "com/example/provider/CallbackJava"); if (jclazz = 0) {LOGD ("class not find"); return ;} // 2. Find the method id in the class, the third parameter method name, and the fourth parameter. The jmethodID methodid = (* env)-> GetMethodID (env, jclazz, methodname, signname); if (methodid = 0) {LOGD ("method not find"); return;} return methodid;} JNIEXPORT void JNICALL encode (JNIEnv * env, jobject obj) {// 1. Find the class file in java // The second parameter is the class name (package name) jclass jclazz = (* env)-> FindClass (env, "com/example/provider/CallbackJava"); if (jclazz = 0) {LOGD ("class not find"); return ;} // 2. Find the method id in the class, the third parameter method name, and the fourth parameter. The jmethodID methodid = (* env)-> GetMethodID (env, jclazz, "helloFromJava", "() V"); if (methodid = 0) {LOGD ("helloFromJava method not find"); return ;} // 3. Call method // void (* CallVoidMethod) (JNIEnv *, jobject, jmethodID ,...); (* env)-> CallVoidMethod (env, obj, methodid);} JNIEXPORT void JNICALL inline (JNIEnv * env, jobject obj) {jmethodID mid = getMethodId (env, "Add ", "(II) I"); int result = (* env)-> CallIntMethod (env, obj, mid, 3, 6); LOGD ("c to java add: d % ", result);} JNIEXPORT void JNICALL encode (JNIEnv * env, jobject obj) {LOGD ("c to java callPrintString start"); jmethodID mid = getMethodId (env, "printString ", "(Ljava/lang/String;) V"); (* env)-> CallVoidMethod (env, obj, mid, (* env)-> NewStringUTF (env, "call printString finish"); LOGD ("c to java callPrintString finish");} JNIEXPORT void JNICALL handle (JNIEnv * env, jobject obj) {jclass jclazz = (* env) -> FindClass (env, "com/example/provider/CallbackJava"); if (jclazz = 0) {LOGD ("class not find"); return ;} // call GetStaticMethodID and CallStaticXXXMethod jmethodID methodid = (* env)-> GetStaticMethodID (env, jclazz, "printStaticStr", "(Ljava/lang/String;) V "); if (methodid = 0) {LOGD ("printStaticStr method not find"); return;} (* env)-> CallStaticVoidMethod (env, jclazz, methodid, (* env) -> NewStringUTF (env, "static hello java "));}

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.