Android Learning JNI, calling C in Java

Source: Internet
Author: User

First you must know what JNI is.

JNI (Java Native Interface) is the Java native Language interface, which provides a way to invoke the underlying language (c + +) in Java. In general, if the project must use the C language to operate the underlying hardware, then the JNI technique must be used.


So now that you're using Java to call C, you have to solve a few problems?

1:java is an object-oriented language, and C is a process-oriented language

The type of object,string in 2:java, the Boolean type, is not in C, how is the C language recognized?


In fact, these jni are done for us, in the JNI header file # include <jni.h> has

typedef unsigned char   Jboolean;       /* Unsigned 8 bits */typedef signed char     jbyte;          /* Signed 8 bits */typedef unsigned short  jchar;          /* unsigned bits */typedef short           jshort;         /* Signed-bits */typedef int             jint;           /* Signed */typedef long long       jlong;          /* Signed-bits */typedef float           jfloat;         /* 32-bit IEEE 754 */typedef double          jdouble;        /* 64-bit IEEE 754 */

/* * Reference types, in C. */typedef void* jobject;typedef jobject jclass;typedef         jobject         jstring;typedef Jobject         jarray;typedef jarray          jobjectarray;typedef jarray          jbooleanarray;typedef jarray          JbyteArray ; typedef jarray          jchararray;typedef jarray          jshortarray;typedef jarray jintarray;typedef          jarray          Jlongarray;typedef jarray          jfloatarray;typedef jarray          jdoublearray;typedef jobject jthrowable         ; typedef jobject         Jweak;

You can see that object objects in Java are in the void*, and this is known to everyone who has learned the C language.

Note: Before you learn JNI, you need to download the NDK installation on Google's website and configure the environment variables.


This time we learned how to invoke the C language using the Java language and then print Hello world!

First step: Build an Android Application project

Step two: Suggest a folder under the project root directory, named Jni

Step three: In the Jni folder, create a C file

Fourth step: In Java code, create a local method: HELLOFROMC

Fifth step: Implement the local function defined in step fourth in hello.c, but the function name must be: java+ Package name +java file + Local method name

such as: JAVA_COM_DEMO_HELLOWORLD_MAINACTIVITY_HELLOFROMC (jnienv* env, Jobject obj)

Sixth step: Convert C string to Java string return

Seventh Step: Create the Android.mk file in the Jni folder, similar to the makefile file

Eighth step: Execute the ndk-build.cmd instruction under the JNI folder

Nineth Step: Loading the dynamic library in Java, calling the local method

Tenth step: Deploy if the following error occurs

The CPU platform is a complement, the default is the arm of the architecture CPU, and I simulator with the X86, I would like to create a new arm simulator, not so troublesome

11th Step: Build a platform that supports X86 and create a new application.mk file under the Jni folder

Can be found to compile again, there is a x86 library


Deploy again to run:



You can see that the Hello_world returned by the C language can print correctly. Indicates that Java calls C language successfully.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Learning JNI, calling C in 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.