The first step of the Android JNI-Starting with HelloWorld

Source: Internet
Author: User

First step: Configure the NDK runtime environmentTwo kits:

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

Android-ndk-r10 (can of course 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's plugins folder. Then restart Eclipse.

Former tool pack Download connection

The latter is the toolkit required for Eclipse to run the JNI program, placing it under a path that you are accustomed to. Once placed, the environment variables are configured, such as:



To Add the NDK path:

Select the Preferences option in the window toolbar to enter the Preferences dialog box, as follows:


The first toolkit mentioned above is for the generation of the 3 "NDK", if not unexpected, after adding the first toolkit will have this option. Then the 4th step is to add our second kit.

Through the above steps, our development environment has been built.


Step Two: Create an Android projectnew Android Project:

The new project is no different from the normal Android project.


Add local support:

after the new project is built, we need to Add local support


After this step is completed, the ANDROID.MK configuration file and the appropriate CPP file are automatically generated, and the contents of the Mk file are:

Local_path: = $ (call My-dir) include $ (clear_vars) local_module    : = william_hello_jnilocal_src_files: = WILLIAM_ Hello_jni.cppinclude $ (build_shared_library)
and for that Local_module: The William_hello_jni in the William_hello_jni is the library that needs to be called in the Java code, which is explained later.

Of course, the premise of these two steps is that you have added the first toolkit.


Write C + + code:

Next, you can try to write your C + + code (just write it in the william_hello_jni.cpp that you just generated). My example is this:

#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.");

There is one way to see the above C + + code: Jniexport jstring jnicall Java_com_demo_jni_mainactivity_sayhello (jnienv * env, jobject obj);

The components are described below:

Jniexport fixed part, indicating JNI external introduction

Jstring represents the return type

The Jnicall fixed part, as the name implies, indicates that this method is provided to the JNI layer to invoke

The Java fixed section, which indicates that the following part is the contents of the Java code

Com_demo_jni is a package name in Java code

Mainactivity indicates the class name where the corresponding method resides

SayHello is the method name that corresponds to one by one in the Java code.


write Java code:

The first step in Java code is to introduce a library in C + +, where the library is the value of the Local_module in the Android.mk file that we generate when we add local support.

For example, my project is:

static {system.loadlibrary ("William_hello_jni");}
the complete main function, 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:



Source Download:

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


Reference

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

The first step of the Android JNI-Starting with HelloWorld

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.