Android NDK Development

Source: Internet
Author: User

Android NDK Development
1. Introduction to JNI is the Java Native Interface. it defines a way for managed code (written in the Java programming language) to interact with native code (written in C/C ++ ).
JNI (Java Local interface): defines a method for Java code to interact with C code or Dynamic Shared Libraries (Dynamic Shared library.
Call method:

C code generates a. so dynamic shared library through compilation for java code calls.

2. Introduction to NDK development NDK is a C/C ++ code Development Toolkit provided by Google for Android, including GCC compilers and header files related to various platforms (arm, X86, MIPS, cross-compiled platform-related files. You can download it from the google website. 1. Configure the downloaded ndk resource package to unmount the downloaded ndk package. android-ndk-r9b-windows-x86.zip
Preferences --> Android --> NDK, configure the location of the ndk
2. Demonew is developed and used as a general Android project TestJni;
Right-click the project, choose Android Tools --> Add Native Support, and enter the name of the dynamic library to be generated: TestJni;
In this case, a jni folder is added, including a file TestJni. c and Android. mk.
Android. mk:
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: name of the compilation target.

LOCAL_SRC_FILES: Compile the source file

 

TestJni. c is the local C code. In this example, only the return of a Hello string is added. The method name is defined so that the java code can find the corresponding native code. The specification is as follows: java + package name + class + Method, otherwise it cannot be found.
Of course, there can be another method ing method that can realize the ing between java and C methods, which is not described in detail here.

 

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");}}
To use a static code block, first load the TestJni. so dynamic shared library. sayHello is the local method in C, and add a native modifier. We can use TestJni. sayHello (); To call C code in Java code. 4. Compile and run the Project build all to detect local code errors and generate the. so Library (in lib/armeabi, for the arm platform, you can compile it on other platforms by configuring it ).

 


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.