Android JNI configuration and Getting Started

Source: Internet
Author: User

JNI is a technique that can call C + + code in Java, which means that it can be developed on Android using C + +. However, it is not possible to develop Android apps in pure C + +, because these C + + code is called through Java-defined interfaces.


I experimented with Jni on Android using Ndk-r10, no cygin, and another tutorial when using the lower version of the NDK.


NDK installation and how to configure the NDK in ADT

You can go to the Android Developer website to download the latest NDK toolkit, download it and unzip it to a list of easy-to-find directories, such as we unzipped to C: \ So the NDK is installed in the C:\android-ndk-r10 directory.


To use the NDK in your Android project also requires configuration, the configuration method is very simple:

In Eclipse ADT, open the WINDOW->PREFERENCE->ANDORID->NDK and fill in the NDK location with the directory where you installed the NDK, which is the C:\android-ndk-r10 above. Now ADT can automatically invoke the tools associated with building the NDK project based on the NDK path you provide.



Try the first NDK program

1. First create an Android project named Ndktest in Eclipse ADT.

2. In ADT you want to use the NDK's engineering catalog (here is the Ndktest directory), right-click on Android tools->add Native support ...


In the popup dialog box, fill in the file name of the binary link library you are about to generate, here we take hello.so as an example


Then click Finish, and wait a moment, and you'll find a folder called JNI in your project directory.



3. Now it's time to start writing the C + + code that finally worked that part, which is written in Hello.cpp (the file name of the CPP file is the same as the filename of the. So link library you just created), and we write the following code in the CPP:

#include <jni.h> #include <string.h>//-C + + definition method must be java+ package name +activity name + your function name, each part with an underscore _ to connect,// And the parameters are fixed. Therefore, the fixed format for defining A/C + + function is://java_packagename_activityname_function (jnienv* env, Jobject obj) {...} here extern "C" is to prevent C + + compile binary link library when the function is renamed, in fact, add does not affect the implementation of the code to run the effect,//But if your program because java.lang.UnsatisfiedLinkError:Native Method not found this//error and you still add it. extern "C" jstring Java_com_example_ndktest_mainactivity_hello (jnienv* env, Jobject obj) {return Env->newstringutf ( "Hello JNI.");}
The comments in the code have been explained very clearly, and you have defined C + + functions to be named in a fixed format.

ADT automatically calls Ndk-build when you click the Run button to run your app to compile our C/C + + code.

4. In order to invoke our write-C + + code, we need to add the following code snippet to the Java source:

public class Mainactivity extends Activity {//Because the static block is loaded more than other code, so here we import our dynamic link library// Remember to let yourself fill in the. so file name, where you can write the file name to import the static {system.loadlibrary ("Hello");} To use your own defined C/s + + functions, define an empty method in Java code like this-the return value is the same as the type returned by the C + + function that you defined earlier, the function name and the C/s functions name//Last part is okay, because the last part is the function name public Native String hello (); ...}

5. So we have the C + + code and the Java calling interface ready, we can test it.

Add the following code to the XML layout code in Mainactivity:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:paddingbottom= "@dimen/activity_vertical_margin"    android:paddingleft= "@dimen/activity_ Horizontal_margin "    android:paddingright=" @dimen/activity_horizontal_margin "    android:paddingtop=" @dimen /activity_vertical_margin "    tools:context=". Mainactivity ">    <!--Use this TextView to show us results--    <textview         android:id=" @+id/display "         Android:layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:text= "@string/hello _world "/></relativelayout>
Then add the following sections to the Java code:

@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);((TextView) Findviewbyid (R.id.display)). SetText (hello ());}
Post-run Effects:




If reproduced please specify the source: Http://blog.csdn.net/gophers




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.