Updated for NDK 1.6 While Android SDK is great for application development, every once in a while you may need access to native code. This code is usually done in C. While you were able to access native code via Java Native Interface (JNI) all along, the process was rather hard. You would've typically had to compile everything on your host computer for the target architecture, requiring you to have the entire toolchain on your development machine. Android NDK (Native Development Kit) simplifies working with native code. It includes the entire toolchain needed to build for your target platform (ARM). It is designed to help you create that shared library. Note that native code accessible via JNI still runs inside the Dalvik VM, and as such is subject to the same life-cycle rules that any Android application lives by. The advantage of writing parts of your app code in native language is presumably speed in certain cases. Note: I'm using <NDKHOME> to refer to the root directory in which you installed your NDK. For me that's /Users/marko/WorkArea/android-ndk-1.6_r1 . I'm assuming all other directories and files are relative to your Eclipse project root, in my case /Users/marko/Workspace/Android/NDKDemo .Overview We are roughly going to do this: 1. Create the Java class representing the native code 2. Create the native code header file 3. Implement the native code by writing your C code 4. Compile everything and build you Shared Library 5. Use your native code inside Android activity Create Native Library This is just a Java file that lives in standard src directory in your Eclipse project. It serves as the glue to the native code that we'll write later./src/com.marakana/NativeLib.java
Code:
Create C Header File In your project bin directory (in my case, <EclipseWorkspace>/NDKDemo/bin ), run javah tool to create the JNI header file.Next, create a jni directory in your project directory (in my case, <EclipseWorkspace>/NDKDemo/jni ).Next, copy the JNI header from <EclipseWorkspace>/NDKDemo/bin to <EclipseWorkspace>/NDKDemo/jni Here's my command line:
Code:
Write the C Code In your <EclipseWorkspace>/NDKDemo/jni/ folder, create ndk_demo.c
file. This is where we'll implement the native code. To start, copy the
function signatures from the header file, and provide the
implementation for those functions. In this example, the header file
looks like this:<EclipseWorkspace>/NDKDemo/jni/com_marakana_NativeLib.h
Code:
And the corresponding implementation looks like this: <EclipseWorkspace>/NDKDemo/jni/ndk_demo.c
Code:
Build The Library To build the library, first we need to create a makefile for how to compile the C code: <EclipseWorkspace>/NDKDemo/jni/Android.mk
Code:
Next, we need to tell NDK how to build the shared library and put it in the correct place inside the Eclipse project. To do this, create a folder <NDKHOME>/apps/ndk_demo/ and inside this folder create the Application file:<NDKHOME>/apps/ndk_demo/Application
Code:
Next, create a symbolic link <NDKHOME>/apps/ndk_demo/project to your Eclipse project:ln -s ~/Workspace/Android/NDKDemo <NDKHOME>/apps/ndk_demo/project If you are on Windows, or another OS that doesn't support symbolic links, you may have to copy entire Eclipse project into <NDKHOME>/apps/ndk_demo/project directory, then copy back to Eclipse. I'm running all this on Mac OS X 10.6 and I assume Linux-type shell.You can now to to your <NDKHOME> and run make APP=ndk_demo The output should look lie this:
Code:
You can now refresh your Eclipse project and you should /lib/ directory containing your libndk_demo.so file.Calling Native Code from Java So now that we have the native C library implemented, compiled, and placed in the right place, let's see how we can call it from our Activity. It's actually rather simple - you just have to instantiate the instance of your NativeLib class and from there on, it's just a regular Java object./src/com.marakana/NDKDemo.java
Code:
The UI for this example is not that significant, but I'm going to include it here for the sake of completeness. /res/layout/main.xml
Code:
Output Source Code http:///static/tutorials/NDKDemo.zip http:///static/tutorials/NDKHOME.zip |
|
来自: shaobin0604@1... > 《Android》