One Introduction to JNI Basics
JNI is the Java Native Interface. It defines a-on-managed code (written in the Java programming language) to interact with native code (written in c/c+ +).
JNI (Java Local interface): Defines a way to allow Java code to interact with C code or dynamic shared Libraries (a dynamically sharing library).
Call Mode:
C code generates a. So dynamic shared library by compiling for Java code calls.
Two Introduction to NDK Development
The NDK is a toolkit that Google has developed for Android, including a compiler for GCC and related header files for each platform (Arm,x86,mips), and some platform-related files for cross-compiling. Can be downloaded from Google official website. 1. The NDK Resource Pack location downloaded by Eclipse configuration
unzip the downloaded NDK package, Android-ndk-r9b-windows-x86.zip
PREFERENCES-->ANDROID-->NDK, configuring the NDK location
2, development and use of demonew A common Android engineering testjni;
Right-click Project, Android tools-->add Native support, input want to generate dynamic library name: Testjni;
at this point, a more Jni folder, including a testjni.c and Android.mk file, is created.
android.mk Configuration compilation related actions, the contents are as follows:
Local_path: = $ (call My-dir)
Include $ (clear_vars)
Local_module: = Testjni
Local_src_files: = Testjni.c
Include $ (build_shared_library)
Note:
Build_shared_library: Indicates that the compilation target is a dynamic shared library, that is, the. so file is generated
local_module: Compile target Name
Local_src_files: Compiling source files
TESTJNI.C is the local C code, in this case only add a Hello string return, the definition of this method name, in order to make Java code can find the corresponding native code, its specification is: java+ package name +class+method, otherwise cannot be found.
Of course, there can be another way of mapping, you can implement the Java to C method mapping, not detailed in this article.
Jstring Java_com_pingtech_testjni_testjni_sayhello (jnienv* env, jobject thiz) {return (*env)->newstringutf ( Env, "Hello from JNI!");}
3. JNI Java code
public class Testjni{public static native String SayHello () static{system.loadlibrary ("Testjni");}}
using static code blocks, first load the testjni.so dynamic shared library, SayHello the local method in C, preceded by a native modifier. This we can directly through Testjni.sayhello (); Call c code in Java code. 4. Compile and run
Project build all, which detects local code errors and generates. So libraries (Lib/armeabi, this for arm platforms, of course, can be configured to compile other platforms).
Introduction to Android NDK development