Android Platform NDK Programming

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/wangbin_jxust/article/details/37389383

Prior to the development of COCOS2DX, already detailed how to transplant the Win32 C + + code to the Android platform, when re-review, found that some basic understanding of things is not very thorough, today using the Android NDK provides an example of a simple porting. Before doing this demo, make sure you have configured the Android development environment and installed the latest Android NDK.

1. Create an Android project

Create an Android project, the package name is COM.EXAMPLE.HELLOJNI, create an activity as the program entered the acitivity, named Hellojni.

2. Create a C file

Create a C file and put a function that takes the current CPU schema and returns it as a string. Note the format of the function: Java_ the name of the package name _java the name of the _java function.

#include <string.h>#include<jni.h>Jstringjava_com_example_hellojni_hellojni_stringfromjni (jnienv*env, Jobject thiz) {#ifDefined (__arm__)#ifDefined (__arm_arch_7a__)#ifDefined (__arm_neon__)#defineABI "Armeabi-v7a/neon"#else      #defineABI "armeabi-v7a"#endif  #else   #defineABI "Armeabi"#endif#elifDefined (__i386__)#defineABI "x86"#elifDefined (__mips__)#defineABI "MIPS"#else   #defineABI "Unknown"#endif    return(*env)->newstringutf (env,"Hello from JNI! Compiled with ABI"Abi".");}

3. Configuring JNI

Create a folder under the root directory of the Android project (that is, the directory where the Androidmanifest.xml file is located), named JNI (Note that the file name cannot be written incorrectly)

Under the JNI directory, create the android.mk and application.mk two files, and also put the C files under the Jni folder. As follows:

Here's the Nick folder, can be ignored first, this is for the later packaging multiple so prepared.

A. Configuring the Android.mk file

Android.mk file is actually a very small ndk build script, its syntax in: NDK installation directory/docs/android-mk.html, the following code also adds comments to some of the basic properties.

#返回当前文件在系统中的路径, the Mk file must be defined at the beginning of the variable Local_path:= $ (call my-dir) #CLEAR_VARS variable is provided by the build system, because there are a lot of global variables, before this build, Clear the previous include $ (clear_vars) #LOCAL_MODULE is actually the project name, used to differentiate individual items, the name must be unique and does not contain spaces, and the final so library, named will also be the   Lib project name. Solocal_module    := hello-jni# The C or CPP file to be compiled, note that you do not need to enumerate the header files or the Include files here, the build system will automatically help you rely on these files local_src_files:= hello- jni.c# build system-supplied variables include $ (build_shared_library)

B. Configuring the Application.mk file

The application.mk file is actually a file that is described to the application itself, which defines the list of functional modules that the application requires, packages different so for different CPU architectures, builds release or debug packages, and so on.

App_abi: = xxx, here is the XXX refers to different platforms, you can choose to fill the x86,armeabi,armeabi-v7a,mips,all, it is worth mentioning that, select all, will build all the platform so, if not fill out the item, The default build is Armeabi. At the same time, the author has also done an experiment, build Armeabi platform so is can run on the Intel x86 architecture CPU platform, but build x86 platform of so is not running on the Armeabi platform , so it should be Intel is compatible with Armeabi , but if you want so to run on the Intel x86 platform with minimal power consumption, you still want to specify the so-x86 platform for the build.

4. Packaging so and how to pack multiple so

Under the root directory of the current Android project, run the NDK installation path/ndk-build, and then start packing so.

Also, if you run the NDK installation path/ndk-build clean, all current so will be clean;

Run the NDK installation path/ndk-build-b v=1, then Force RePack,

If you want to pack multiple so, you can define multiple modules in android.mk, or write multiple android.mk, each android.mk define a modules, and here I create a Nick folder under the JNI directory to place the new C file.

At this point, just change the android.mk in the JNI directory, and then package the C code of the Nick folder again. android.mk files under JNI:

#返回当前文件在系统中的路径, the Mk file must be defined at the beginning of the variable Local_path:= $ (call my-dir) #CLEAR_VARS variable is provided by the build system, because there are a lot of global variables, before this build, Clear the previous include $ (clear_vars) #LOCAL_MODULE is actually the project name, used to differentiate individual items, the name must be unique and does not contain spaces, and the final so library, named will also be the   Lib project name. Solocal_module    := hello-jni# The C or CPP file to be compiled, note that you do not need to enumerate the header files or the Include files here, the build system will automatically help you rely on these files local_src_files:= hello- jni.c# build system-supplied variables include $ (build_shared_library) #对nick文件夹下的代码打包soinclude $ (clear_vars) local_module    := hello-jni-minelocal_src_files:= nick/hello-jni.cinclude $ (build_shared_library)

Yes, you read it right, re-add the Local_module and Local_src_files variables to reconfigure.

5.jni Call

In activity, we use the static keyword to place the loaded so in the function body to ensure that so is loaded directly first.

Static {        system.loadlibrary ("hello-jni");    }

Note thatsystem.loadlibrary () does not fill in the full so name, but instead removes the prefix lib and suffix. So, which is the local_module variable in android.mk.

Java layer functions to use the NATIVE keyword to declare this call native layer function, if the Java function is public native String XXXX (), then here is the call in C code JAVA_COM_EXAMPLE_HELLOJNI The _hellojni_stringfromjni () function.

The above is the Android platform packaging so and call one of the most basic demo, in fact, the whole process is relatively simple, there are some provisions of the name can not be arbitrarily modified, if the JNI folder name, Android.mk, APPLICATION.MK file name, called by the Java layer C function name, and so on, these are all rules.

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.