Android windows JNI programming, androidjni

Source: Internet
Author: User

Android windows JNI programming, androidjni
Yesterday I learned JNI programming on windows. JNI is a bridge between java and C language. java can call JNI to call the C language implementation method. The full name of JNI is (Java native interface). In programming, you only need to write the functions that interact with java, other C language calls can directly use the C language-related syntax. If you have less time, start with the question. To use JNI on windroid or linux, you must download the NDK and specify the path. in windows, we also need to install sygwin. Here I will not talk about how to install cygwin, find a etc folder in the cygwin folder you have installed, find a profile file in this file, modify the Path, and add: (ndk Path ), in my understanding, ndk is to build a cross-compiling environment from a pc to android. Of course, there are still a lot of things I don't know about in it, and it is still to be explored. Then we can start using it. Of course, if you use eclipse to write C/C ++, you also need to install the cdt plug-in. After installation, you can use eclipse to compile c/c ++ code. Let's take a look at how to compile the code. First, we will create a jni folder in the android project, create a C-language source file in the jni folder, and wear a class in android, class, you can use the native identifier to create a function. For example: public class DataProvider {// the C language with parameters calls java public int add2 (int x, int y) {return x + y;} // the C language of the function without parameters calls java public void show () {System. out. println ("I was called");} // a non-parametric static function C language calls java public static void show2 () {System. out. println ("I was called again");} // use native to identify the function. The corresponding public native int add (int x, int y) function can be automatically generated ); public native int sayHello (String hello); public native int [] array (int [] arr); public native int callbackadd2 (); public native void callbackshow (); public native static void callbackshow2 (); public native void callbackshow3 ();}
After writing this class, you can use javah (full Class Name of the class). Full class name refers to the package name + class name, such as com. example. testjni. dataProvider to generate a header FILE used in C language, for example:/* do not edit this file-it is machine generated */# include <jni. h>/* Header for class com_example_testjni_DataProvider */# ifndef _ classes # define _ classes # ifdef _ cplusplusextern "C" {# endif/** Class: Example * Method: add * Signature: (II) I */JNIEXPORT jint JNICALL vertex (JNIEnv *, jobject, jint, jint);/** Class: com_example_testjni_DataProvider * Method: sayHello * Signature: (Ljava/lang/String;) I */JNIEXPORT jint JNICALL inline (JNIEnv *, jobject, jstring);/** Class: com_example_testjni_DataProvider * Method: array * Signat Ure: ([I) [I */JNIEXPORT jintArray JNICALL inline (JNIEnv *, jobject, jintArray);/** Class: com_example_testjni_DataProvider * Method: callbackadd2 * Signature :() I */JNIEXPORT jint JNICALL inline (JNIEnv *, jobject);/** Class: com_example_testjni_DataProvider * Method: callbackshow * Signature: () V */JNIEXPORT void JNICALL initialize (JNIEnv *, jobject);/** Class: Initialize * Method: callbackshow2 * Signature: () V */JNIEXPORT void JNICALL initialize (JNIEnv * env, jclass ); /** Class: com_example_testjni_DataProvider * Method: callbackshow3 * Signature: () V */JNIEXPORT void JNICALL Java_com_example_testjni _ DataProvider_callbackshow3 (JNIEnv *, jobject); # ifdef _ cplusplus} # endif, which are automatically generated after javah is used. In the next step, I will implement these functions in C. Before that, I need to create an mk file, which is makefile, when the C language is compiled into a library, you can read makefile for compilation. Mk file: LOCAL_PATH :=$ (call my-dir) include $ (CLEAR_VARS) # LOCAL_MODULE: = hello # LOCAL_SRC_FILES: = hello. c functions. c LOCAL_LDLIBS: =-llog // use the local library include $ (BUILD_SHARED_LIBRARY)

# Include <stdio. h> # include "com_example_testjni_DataProvider.h" // reference header file # include <android/log. h> // The printed log const char * TAG = "clog"; # define LOGD (...) _ android_log_print (ANDROID_LOG_DEBUG, TAG, _ VA_ARGS _); # define LOGI (...) _ android_log_print (ANDROID_LOG_INFO, TAG, _ VA_ARGS _); // use the function in the log header file to print logs to logcat in eclipse. Here, you need to add library references to the mk file, LOCAL_LDLIBS + =-llogJNIEXPORT jint JNICALL Java_com_example_testjni_D AtaProvider_add (JNIEnv * env, jobject o, jint x, jint y) {LOGI ("% d \ n", x); LOGD ("% d \ n", y ); return x + y; // return directly} // int print (jintArray localarray, int I); int print (int * localarray, int I) {LOGD ("array = % d \ n", * (localarray + I); return 0;} // common C language code, which can be directly called/** Class: com_example_testjni_DataProvider * Method: sayHello * Signature: (Ljava/lang/String;) I */JNIEXPORT jint JNICALL Java_com_exam Ple_testjni_DataProvider_sayHello (JNIEnv * env, jobject o, jstring hello) {}/ ** Class: com_example_testjni_DataProvider * Method: array * Signature: ([I) [I */JNIEXPORT jintArray JNICALL Java_com_example_testjni_DataProvider_array (JNIEnv * env, jobject obj, jintArray array) // transmits an int type array in java, java calls C language {// get the array length int length = (* env)-> GetArrayLength (env, array); int I; jint * localarray = (* env)-> GetI NtArrayElements (env, array, 0); for (I = 0; I <length; I ++) {* (localarray + I) + = 5; print (localarray, i);} return array;}/** Class: com_example_testjni_DataProvider * Method: callbackadd2 * Signature: () I */JNIEXPORT jint JNICALL encode (JNIEnv * env, jobject obj) {jclass clazz = (* env)-> FindClass (env, "com/example/testjni/DataProvider"); jmethodID m Id = (* env)-> GetMethodID (env, clazz, "add2", "(II) I"); return (* env)-> CallIntMethod (env, obj, mid, 3, 5);}/** Class: com_example_testjni_DataProvider * Method: callbackshow * Signature: () V */JNIEXPORT void JNICALL evaluate (JNIEnv * env, jobject obj) {jclass clazz = (* env)-> FindClass (env, "com/example/testjni/DataProvider"); jmethodID mid = (* env)-> GetMet HodID (env, clazz, "show", "() V"); // (* env)-> CallVoidMethod (env, obj, mid);}/** Class: callback * Method: callbackshow2 * Signature: () V */JNIEXPORT void JNICALL inline (JNIEnv * env, jclass jc) {jmethodID mid = (* env)-> GetStaticMethodID (env, jc, "show2", "() V"); jobject obj = (* env)-> CallStaticObjectMethod (env, jc, mid); (* env)-> CallVoidMethod (env, obj, mid);}/** Class: callback * Method: callbackshow3 * Signature: () V */JNIEXPORT void JNICALL encode (JNIEnv * env, jobject obj) {jclass clazz = (* env)-> FindClass (env, "com/example/testjni/DataProvider"); jmethodID mid = (* env)-> GetStaticMethodID (env, clazz, "show2", "() V"); (* env)-> CallVoidMethod (env, obj, Mid);} java calls C language to process the value passed by java, and then uses the jni method to return the value. To call java in C language, you need to map the value in C language code, for example: JNIEXPORT void JNICALL initialize (JNIEnv * env, jobject obj) {jclass clazz = (* env)-> FindClass (env, "com/example/testjni/DataProvider "); // obtain the class bytecode jmethodID mid = (* env)-> GetStaticMethodID (env, clazz, "show2", "() V "); // obtain the function id (* env)-> CallVoidMethod (env, obj, mid); // The execution function} has different call functions. For example, the return value is 'int' (CallIntMethod (env, obj, mid, 3, 5); The empty type is CallVoidMethod (env, obj, mid); the following () V is the function signature, it indicates that the return value is a void function without any parameters. (II) I return the int type parameter, which is two int-worthy functions. After the C language is compiled, run the ndk-bulid command under the src directory of the project to generate a C language library, which is also displayed in the eclipse project directory. You need to delete the obj folder before each compilation to clear the cache. Jni can greatly improve the secret of java code, and it can improve the efficiency by using C language to develop jni programs.

Related Article

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.