Getting started with Android JNI

Source: Internet
Author: User

Getting started with Android JNI

Android JNI (Java Native Interface) is a language used to implement mutual calls between java and other languages. Of course, Android is used for Calling C and C ++. In Linux, the final file called by Java in Mac is the. so file, which is the. dll file in Window.

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

Create an Android project HelloNDK. Create a new class GetString

 

public class GetString {public static native String GetString();public static native String GetString(int a,int b);public static native String getWord();}
The procedure is as follows:

 

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

2. Use javah to generate a. h file;

3. Compile a c file containing the. h file

4. Compile the c file

5. Use the compiled so file.

How does one implement the Android Project? In fact, in the bin/classes/directory of the project (classes is hidden), the first step is compiled, so we will perform the second step in this directory. The command is:

Javah-jni com. example. hellondk. GetString (package + class)

The header file com. example. hellondk. GetString. h will be generated. If you have configured the NDK directory, right-click the project and choose Add Native Support in Android Tool to view the project name finish.

The jni folder is added to the project. There are Android. mk files and. cpp files with the same project name.

Copy the generated. h file to the jni folder.

Compile the c file first. What is written 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 very scary, but you can see that the Java and C methods are matched in this way. Other parameters such as JNIEnv will be discussed later

 

The next step is to implement these methods in the. cpp file. Copy 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;};

Simple return output.

 

How can I use it now? In fact, we can also see that the libHelloNDK. so library file is generated in the armeabi folder under the libs directory of the project. This is the. so library file we want to use.

In MainActivity

 

static {System.loadLibrary("HelloNDK");}

You can. Next, you can directly use the GetString method to implement Java --> C.

 

 

 

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.