在网上看了很多资料实现JNI,可谓花样百出,其实android自身已经提供了最好的实现方法,NDK
NDK 下载:
http://developer.android.com/sdk/ndk/index.html
NDK的使用也很简单。我把它分为两步。
第一步,产生.h头文件,并根据此头文件写.C/.CPP文件
我们需要在.java中将相应method声明为native,具体可参考 http://marakana.com/forums/android/examples/49.html
第二步,将.C/.CPP编译成最终我们工程能接受的库文件,这就要用到NDK了
官方指导如下
Here's the general outline of how you work with the NDK tools:
- Place your native sources under
<project>/jni/...
- Create
<project>/jni/Android.mk
to describe your native sources to the NDK build system - Optional: 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-build
The 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.
For complete information on all of the steps listed above, please see the documentation included with the NDK package.