Two minutes to learn the Android platform NDK programming (without Eclipse and Cygwin, you can use the command line to package multiple so)

Source: Internet
Author: User

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) {#if defined (__arm__)  #if defined (__arm_arch_7a__)    #if defined (__arm_neon__)      # Define ABI "Armeabi-v7a/neon"    #else      #define ABI "armeabi-v7a"    #endif  #else   #define ABI "Armeabi"  #endif #elif defined (__i386__)   #define ABI "x86" #elif defined (__mips__)   #define ABI "MIPS" #else   # Define ABI "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 number of global variables, before this build, clear the previous include $ ( Clear_vars) #LOCAL_MODULE is actually the project name, used to differentiate the 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 #要编译的c or CPP file, 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 provides variables include $ (build_ Shared_library)

b. Configure 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 the item, the default build to 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 multipleso, 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 number of global variables, before this build, clear the previous include $ ( Clear_vars) #LOCAL_MODULE is actually the project name, used to differentiate the 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 #要编译的c or CPP file, 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 provides 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");    }
It's important to 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.



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.