Android JNI's first step -- Starting from HelloWorld, androidjni

Source: Internet
Author: User

Android JNI's first step -- Starting from HelloWorld, androidjni
Step 1: Configure two sdks for the ndk runtime environment:

Com. android. ide. eclipse. ndk_23.0.2.1259578.jar

Android-ndk-r10 (of course it can also be some other similar toolkit, such as android-ndk-r8)

The former is a required toolkit for building an NDK environment. You need to place it in the eclipse plugins folder. Then restart eclipse.

The former toolkit download connection

The latter tool kit is required to run the JNI program for eclipse and place it in a path you are used. After being placed, configure the environment variables, such:



Add the NDK path:

Select the preferences option in the window toolbar to enter the Preferences dialog box, as shown below:


The first toolkit mentioned above is the 3 "NDK" in the production. If there is no accident, this option will be available after the first toolkit is added. Step 2 is to add our second toolkit.

After the above steps, our development environment has been set up.


Step 2: Create an Android project and create an Android project:

Creating a project is no different from a normal Android project.


Add local support:

After creating a project, we need to add local support for it.


After this step is completed, the Android. mk configuration file and the corresponding cpp file are automatically generated. mk file content:

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := William_Hello_JNILOCAL_SRC_FILES := William_Hello_JNI.cppinclude $(BUILD_SHARED_LIBRARY)
In the LOCAL_MODULE: = William am_hello_jni file, William am_hello_jni is the Library to be called in Java code, which will be described later.

Of course, the premise is that you have added the first toolkit.


Write C ++ code:

Next, you can try to write your C ++ code (write it in the generated William am_hello_jni.cpp ). My example is as follows:

#include <jni.h>#include <string.h>extern "C" {JNIEXPORT jstring JNICALL Java_com_demo_jni_MainActivity_sayhello(JNIEnv * env, jobject obj);}JNIEXPORT jstring JNICALL Java_com_demo_jni_MainActivity_sayhello(JNIEnv * env, jobject obj) {return env->NewStringUTF("Hello From cpp.");}

The above C ++ code shows a method: JNIEXPORT jstring JNICALL Java_com_demo_jni_MainActivity_sayhello (JNIEnv * env, jobject obj );

The components are described as follows:

JNIEXPORT fixed part, indicating JNI external Import

Jstring indicates the return type.

The fixed part of JNICALL, as the name suggests, indicates that this method is provided to the JNI layer for calling.

The fixed part of Java, indicating that the subsequent part is the content of Java code

Com_demo_jni is the package name in Java code.

MainActivity indicates the Class Name of the corresponding method

Sayhello is the method name that corresponds to the Java code.


Write Java code:

The first step of Java code is to introduce the library in C/C ++. The library here is the value of LOCAL_MODULE In the Android. mk file generated when we add local support.

For example, in my project:

static {System.loadLibrary("William_Hello_JNI");}
The complete main function is as follows:

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);EditText showJNI = (EditText) findViewById(R.id.activity_main_jni_show);showJNI.setText(sayhello());}public native String sayhello();static {System.loadLibrary("William_Hello_JNI");}}

Run:



Download source code:

Source code: http://download.csdn.net/detail/u013761665/8369031


Reference

Reference blog: http://blog.csdn.net/yf210yf/article/details/9264587

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.