Study on the JNI programming of Android Learning

Source: Internet
Author: User

The development of "Java+c" has become the official support of the development, the essence of the NDK is to allow Android applications to easily use JNI technology to provide a set of tools, using the NDK is mainly 4 advantages, the first, code protection, Because the Java layer Code of the APK is easy to decompile, and the anti-C + + disassembly is more difficult, the second, it is easy to use the open Source Library, because many of the existing open source libraries are written in C/S code, third, improve the execution efficiency of the program, fourth, easy to transplant, use C/ The written library can be easily used on other embedded platforms. Here's another problem: if you run on the emulator, we can only run on arm and not run on Intel, or we won't be able to load that so library file.

NDK Environment Setup

First step: Download the NDK

Step two: Create an Android project

Step three: Create a jni directory in the root directory of the project directory to be used as the NDK's compilation path (the NDK scripts are compiled to generate so files based on C + + files in the JNI directory and MK files)

Fourth step: Create Builder (Builder uses the tools in the NDK to compile files from the JNI directory into so files) Project->properties->builders->new->program






By the way, the function of this code is to add and subtract two int,

Here we first need to create a Java file

Package Com.jk.ndkdemo;public class Ndkbridge {public  native int add (int a,int b);}

Then we build with the Javah tool in the JDK, according to the Ndkbridge class. h, when using the command line, we first enter into the bin\clsses Javah Com.jk.ndkdemo.NDKBridge, the generated. h named rule is the package name _ class name. h

/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class Com_jk_ndkdemo_ndkbridge */ #ifndef _included_com_jk_ndkdemo_ndkbridge#define _included_com_jk_ndkdemo_ndkbridge#ifdef __cplusplusextern "C" {# endif/* * Class:     Com_jk_ndkdemo_ndkbridge * Method:    Add * Signature: (II) I */jniexport jint Jnicall java_com_jk_n Dkdemo_ndkbridge_add  (jnienv *, Jobject, Jint, jint); #ifdef __cplusplus} #endif #endif

Next we create a C file in the JNI directory based on the contents of the H file

#include "com_jk_ndkdemo_ndkbridge.h" #include <stdlib.h> #include <stdio.h> #ifdef __cplusplusextern "C" { #endifJNIEXPORT jint jnicall java_com_jk_ndkdemo_ndkbridge_add  (jnienv *env, Jobject obj, jint A, Jint b) {// Convert the JNI type to c type int a1 = A;int B1 = b;int C1 = a1 + b1;//the C type is transferred to the JNI type jint c = C1;return C;} #ifdef __cplusplus} #endif
Then we create the Mk file

Local_path: = $ (call My-dir) include $ (clear_vars) Local_module: = myndkdemolocal_src_files: = Com_jk_ndkdemo_ Ndkbridge.cinclude $ (build_shared_library)

The local_module here is the name of the so file we generated, and local_src_files is what we created from this file, and once we save it, the so file will be generated automatically.



Then we need to load so files and then we can use them, this step is implemented in the activity

Package Com.jk.ndkdemo;import Android.os.bundle;import Android.app.activity;import android.view.menu;import Android.widget.toast;public class Mainactivity extends Activity {//load so file static {system.loadlibrary ("Myndkdemo");} @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);//Get the object containing the Jni method Ndkbridge bridge = new Ndkbridge ();//Call the Jni method int t = Bridge.add (3, 5); Toast.maketext (This, "" + t, Toast.length_short). Show ();}}

Well, a simple JNI programming is done.


Study on the JNI programming of Android Learning

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.