Getting Started with Android JNI

Source: Internet
Author: User

The Android JNI (Java Native Interface) is designed to implement the language that Java calls to each other language. Of course on Android is to call with c,c++. In Linux, the last thing Java calls is the. so file, which is the. dll file in window.

So how does Java to C correspond? Which method of calling Java is the method in C? The method of declaring native in the Java class begins.

Create a new Android project HELLONDK. Create a new class GetString

public class GetString {public static native string GetString ();p ublic static native string GetString (int a,int b);p ublic Static native String Getword ();}
The specific steps are as follows:

1. Declare the native () method in Java and compile (JAVAC);

2. Create an. h file with Javah;

3. Write C files that contain. h files

4. Compiling c files

5. Use a successful so file.

How is it implemented in my Android project? In fact, in the bin/classes/directory of the project (classes is hidden) on the first step is compiled, so we in this directory in the second step, the command is:

Javah-jni Com.example.hellondk.GetString (Package + Class)

will generate Com.example.hellondk.getstring.h header files, as long as your NDK directory is configured, then the right-click Project in the Android tool to select Add Native support to see the project name of the site finish.

Next, I see a lot of JNI in the project under the folder. There are android.mk, and there is a. cpp file with the project name.

Copy the. h file that you just generated to the JNI folder.

How to write a C file depends on what it says in the H header file.

* Class:     com_example_hellondk_getstring * Method:    GetString * Signature: () ljava/lang/string; */jniexport Jstring jnicall java_com_example_hellondk_getstring_getstring__  (jnienv *, jclass);/* * Class:     com_example_ hellondk_getstring * Method:    GetString * Signature: (II) ljava/lang/string; */jniexport jstring Jnicall java_com_ Example_hellondk_getstring_getstring__ii  (jnienv *, Jclass, Jint, jint);/* * Class:     com_example_hellondk_ GetString * Method:    Getword * Signature: () ljava/lang/string; */jniexport jstring Jnicall JAVA_COM_EXAMPLE_HELLONDK _getstring_getword  (jnienv *, jclass);

The method name is scary, but just look at the Java and C methods that correspond. JNIEnv and other parameters later

The next step is to implement these methods in the. cpp file. Copy the Past

#include "Com_example_hellondk_getstring.h"

/* Class:     com_example_hellondk_getstring * Method:    GetString * Signature: () ljava/lang/string; */jniexport Jstring jnicall java_com_example_hellondk_getstring_getstring__  (jnienv * env, Jclass) {return env-> Newstringutf ("Hello ndk");};/ * Class:     com_example_hellondk_getstring * Method:    GetString * Signature: (II) ljava/lang/string; */jniexport Jstring jnicall Java_com_example_hellondk_getstring_getstring__ii  (jnienv * env, Jclass, jint A, Jint b) {return env- >newstringutf ("Result a+b=" + (A+b));}; Jniexport jint jnicall java_com_example_hellondk_getint_getint  (jnienv * env, Jclass, jint a) {return A;};

A simple return output.

So how do we use it now? In fact, we can also see in the Project Libs directory generated under the Armeabi folder generated libhellondk.so library files. This is the. So library file we're going to use.

In the Mainactivity

static {system.loadlibrary ("Hellondk");}

You can do it. Then you can directly use the GetString method, so that the realization of the java-->c.



Getting Started with Android JNI

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.