A simple example of JNI Learning (zero) in Android.

Source: Internet
Author: User

In Android, JNI enables Java to call the Code implemented by C/C ++. To implement this function, the NDK Toolkit provided by Anrdoid is used, I won't talk about how to configure it here. It's so troublesome. It's been a long time to configure it...

Essentially, the code for Java to call C/C ++ is actually to call the methods provided by C/C ++. Therefore, the first step is to create a class, define an Native method as follows:

JniTest class:

public class JniTest {public native String getTestString();}

We can see that the native keyword is used before this method.

Next, we will compile the java file in the command line to get a class file, as shown below:


Then we can use the javah command file to generate a C header file. In fact, javah is not necessary because it is created to facilitate copying the corresponding method name in Jni, these names are too complex.

Note that the javah command must be called in the root directory of the package. The corresponding class file must be a complete class name, as shown in. It will return to the src directory first, then call the javah command.

In this way, a header file is generated in the src folder, as shown in:


We can see that the name is com_lms_jni_JniTest.h, which is actually the package name + class name. Let's take a look at the content:

/* DO NOT EDIT THIS FILE - it is machine generated */#include 
 
  /* Header for class com_lms_jni_JniTest */#ifndef _Included_com_lms_jni_JniTest#define _Included_com_lms_jni_JniTest#ifdef __cplusplusextern "C" {#endif/* * Class:     com_lms_jni_JniTest * Method:    getTestString * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_lms_jni_JniTest_getTestString  (JNIEnv *, jobject);#ifdef __cplusplus}#endif#endif
 

We can see that there is a method named Java_com_lms_jni_JniTest_getTestString, which is complex enough. If we know this name rule and how to implement this method, we do not need to generate this header file. We can directly write the corresponding C file.

Next, create a corresponding C file in the jni file. The name is worthwhile and does not matter. But for unification, we should call it JniTest. c, as shown below:


Here, we also put com_lms_jni_JniTest.h here. This is actually okay, just for content coordination and unification. Generally, we will put all the files implemented by C/C ++ under the jni folder under the project directory.

The following is the native method implemented in JniTest. c, getTestString, as follows:

#include 
 
  #include 
  
   #include 
   
    JNIEXPORT jstring JNICALL Java_com_lms_jni_JniTest_getTestString  (JNIEnv *e, jobject obj){return (**e).NewStringUTF(e,"Hello from JniTest Function");}
   
  
 

In this c file, we can see that the header file com_lms_jni_JniTest.h is not referenced, but the general C/C ++ library file, such as stido. h and stdlib. h file, etc. Here we notice that we will also reference jni. h file, jni. hware is a very important header file in JNI programming. the correspondence between the data types in Java and the data types in jni is all defined in this file, we will take a look at this jni later. h file.

In JniTest. after the methods are implemented in the c file, the implementation of the C/C ++ side is actually implemented. The next step is to compile the C file into the so file, which is called by Android.

Why is the so file? This is because Android is essentially a linux system, so the JNI library files it calls are all in the so format.

The NDK library provided by Android provides the ndk-build command to implement this compilation process, but before that, we need to create an Android. mk file. This is a simple Make file with the following content:

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := com_lms_jni_HwDemoLOCAL_SRC_FILES := \HwDemo.c \JniTest.c \include $(BUILD_SHARED_LIBRARY)

Here, we will define several variables:

LOCAL_PATH: The value is call my-dir, And my-dir is a macro function. The path of Android. mk is returned, which is the jni folder.

Include $ (CLEAR_VARS). This command will clear all variables starting with LOCAL, such as LOCAL_MODULE, but one exception is its LOCAL_PATH.

LOCAL_MODULE: The so package name to be generated, which is also the name when Java code is loaded in Android.

LOCAL_SRC_FILES: source file to be compiled, for example, HwDemo. c and JniTest. c.

Include $ (BUILD_SHARED_LIBRARY): indicates that a dynamic link library is generated.

After such an Android. mk file is defined, call the ndk-build command in the command line as follows:


After the command is executed, we can see a more so library in libs in the project directory, as shown below:


Here, the implementation of Jni is over, and the next step is how to use this local method in Android.

We have created an Activity and placed only one TextView control in it. Its layout is as follows:

 
     
  
 

Then in the Activity, we need to load this so library, as shown below:

Public class HwDemo extends Activity {static {System. loadLibrary ("com_lms_jni_HwDemo"); // load so library} public native String printHello (); @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); TextView TV = (TextView) findViewById (R. id. tvJni); JniTest jniTest = new JniTest (); // call the JniTest file method TV. setText (jniTest. getTestString ());}}

1) load the so library file using the static code block. Here, the name is the LOCAL_MODULE value defined in Anrdoid. mk.

2) create a JniTest object and call its getTestString () method. The result is as follows:

Download!

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.