JNI (2) Android NDK environment establishment and use

Source: Internet
Author: User

NDK interacts with C/C ++ through Java's Jni. If it doesn't work with Jni, you can check out what I 've previously compiled. <JNI (1) interaction between Java and C ++> 1. Build an NDK development environment 2. How to Use NDK for development

I. Build an NDK Development Environment1. Download the right NDK version of the platform. Download URL:Http://developer.android.com/tools/sdk/ndk/index.htmlFor Windows, you no longer need to download Cygwin. You can download NDK directly and decompress it. 2. NDK-contained libc (C library) headers libm (math library) headers JNI interface headers libz (Zlib compression) headers liblog (Android logging) header OpenGL ES 1.1 and OpenGL ES 2.0 (3D graphics libraries) headers libjnigraphics (Pixel buffer access) header (for Android 2.2 and above ). A Minimal set of headers for C ++ support OpenSL ES native audio libraries Android native application APIS3. NDK is worth a little time. the Android NDK Overview (overview.html in the ndkdirectory) provides a clear description of how to use NDK for development. b. sample Code under the samples directory.
II. How to Use NDK for developmentGetting Started with the NDK on the official website

Place your native sources under <project>/jni /... create <project>/jni/Android. mk to describe your native sources to the NDK build systemOptional: Create <project>/jni/Application. mk. build your native code by running the 'ndk-build' script from your project's directory. it is located in the top-level NDK directory: cd <project> <ndk>/ndk-buildThe build tools copy the stripped, shared libraries needed by your application to the proper location in the application's project directory. finally, compile your application using the SDK tools in the usual way. the SDK build tools will package the shared libraries in the application's deployable. apk file.

Example based on Hello-jni in NDK 1. Use Jni Android files
Package com. xcl. jnidemo5;/*** demonstrate NDK development ** author: xcl * date: 2014-3-20 */import android. OS. bundle; import android. app. activity; import android. view. menu; import android. widget. textView; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // setContentView (R. layout. activity_main); TextView TV = new TextView (this); TV. setText (stringFromJNI (); setContentView (TV) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true;} public native String stringFromJNI (); static {System. out. println (System. getProperty ("java. library. path "); System. loadLibrary ("jnicplus5 ");}}
2. Jni-Related Processing2.1 generate related information according to the Java class above. the purpose of the H file is to create a function prototype for the c file to be generated, although not used in the NDK. h file, but it is better to use javah to generate the prototype. Otherwise, java will appear if you accidentally write the standard. lang. unsatisfiedLinkError: Native method not found class error.
D: \ AppWork \ XExample \ workspace \ jnidemo5 \ src> javah com. xcl. jnidemo5.MainActivityD: \ AppWork \ XExample \ workspace \ jnidemo5 \ src> the volume in the dir drive D is the Data volume serial number is 0EC2-012C D: \ AppWork \ XExample \ workspace \ jnidemo5 \ src directory <DIR>. <DIR> .. <DIR> com2014/03/29 529 com_xcl_jnidemo5_MainActivity.h 1 file 529 bytes 3 directories 18,683,146,240 available bytes
2.2. Right-click the directory for jni storage in the Eclipse Android project, select Source Folder, and create jni directory. 2.3 create the c file jnicplus5.c corresponding to the. h file.
/*** Demonstrate the C Implementation of NDK Development * JNI * author: xcl * date: 2014-3-20 */# include <string. h> # include <jni. h>/** Class: com_xcl_jnidemo5_MainActivity * Method: stringFromJNI * Signature: () Ljava/lang/String; */JNIEXPORT jstring JNICALL trim (JNIEnv * env, jobject jobj) {return (* env)-> NewStringUTF (env, "Hello from JNI! ");}

2.4 generate the compilation file Android. mk:
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := jnicplus5LOCAL_SRC_FILES := jnicplus5.cinclude $(BUILD_SHARED_LIBRARY)
The LOCAL_MODULE in Android. mk specifies the name of the dynamic library to be generated, which must be consistent with the one specified in Java code. LOCAL_SRC_FILES: you only need to specify the c file of the native function implemented by the Java file. Other files are automatically derived during NDK compilation. LOCAL_PATH indicates finding the source file from the project directory. Other parameters are easy to understand, and they are like handwritten Makefile files. Only. mk has some custom macro definitions.
Application. mk
APP_ABI := all
Application. mk is used to specify the platforms on which to generate the. so file.
2.5 It is easy to compile the. c file into the dynamic library NDK to compile the dynamic library. Go to the jni directory of the Android project and call the NDK-build under the ndk.
D:\AppWork\XExample\workspace\jnidemo5\jni>C:\Ndk\ndk\ndk-build[armeabi-v7a] Compile thumb : jnicplus5 <= jnicplus5.c[armeabi-v7a] SharedLibrary : libjnicplus5.so[armeabi-v7a] Install : libjnicplus5.so => libs/armeabi-v7a/libjnicplus5.so[armeabi] Compile thumb : jnicplus5 <= jnicplus5.c[armeabi] SharedLibrary : libjnicplus5.so[armeabi] Install : libjnicplus5.so => libs/armeabi/libjnicplus5.so[x86] Compile : jnicplus5 <= jnicplus5.c[x86] SharedLibrary : libjnicplus5.so[x86] Install : libjnicplus5.so => libs/x86/libjnicplus5.so[mips] Compile : jnicplus5 <= jnicplus5.c[mips] SharedLibrary : libjnicplus5.so[mips] Install : libjnicplus5.so => libs/mips/libjnicplus5.so

If the content of Application. mk is defined as APP_ABI: = armeabi, only the arm dynamic library is generated.
D:\AppWork\XExample\workspace\jnidemo5\jni>C:\Ndk\ndk\ndk-build[armeabi] Compile thumb  : jnicplus5 <= jnicplus5.c[armeabi] SharedLibrary  : libjnicplus5.so[armeabi] Install        : libjnicplus5.so => libs/armeabi/libjnicplus5.so

3. Run the entire Android project to get the desired result.
MAIL: xcl_168@aliyun.comBLOG: http://blog.csdn.net/xcl168








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.