1.安裝NDK 到這裡:http://developer.android.com/sdk/ndk/index.html 下載NDK. 解壓之。
To install the NDK, follow these steps:
- From the table at the top of this page, select the NDK package that is appropriate for your development computer and download the package.
- Uncompress the NDK download package using tools available on your computer. When uncompressed, the NDK files are contained in a directory called
android-ndk-<version>
. You can rename the NDK directory if necessary and you can move it to any
location on your computer. This documentation refers to the NDK directory as <ndk>
.
You are now ready to start working with the NDK.
2. Windows下需要安裝cygwin 的make組件,ndk-build是在cygwin下啟動並執行。
Getting Started with the NDK
Once you've installed the NDK successfully, take a few minutes to read the documentation included in the NDK. You can find the documentation in the <ndk>/docs/
directory. In particular, please read the OVERVIEW.HTML document completely, so that
you understand the intent of the NDK and how to use it.
If you used a previous version of the NDK, take a moment to review the list of NDK changes in the CHANGES.HTML document.
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.
3.產生build.xml
On the command line:
- Change to the
<ndk-root>/samples/hello-jni
directory.
- Run the following command to generate a build.xml file:
android update project -p . -s
Dos命令運行結果:
F:/Android_Tools/NDK/android-ndk-r5c/samples/hello-jni>android update project -p . -sUpdated local.propertiesAdded file F:/Android_Tools/NDK/android-ndk-r5c/samples/hello-jni/build.xmlAdded file F:/Android_Tools/NDK/android-ndk-r5c/samples/hello-jni/proguard.cfg Updated local.propertiesAdded file F:/Android_Tools/NDK/android-ndk-r5c/samples/hello-jni/tests/build.xmlAdded file F:/Android_Tools/NDK/android-ndk-r5c/samples/hello-jni/tests/proguard.cfg
4.Compile the native code using the ndk-build
command.
cd <ndk-root>/samples/hello-jni<ndk_root>/ndk-build
Cygwin運行結果:
5. 這一步可以在eclipse中clean一下,重建apk.
Build and install the application as you would a normal Android application. If you are using Eclipse, run the application to build and install it on a device. If you are using Ant, run the following commands from the project directory:
ant debugadb install bin/HelloJni-debug.apk
log:
06-21 02:45:49.380: INFO/ActivityManager(58): Start proc com.example.hellojni for activity com.example.hellojni/.HelloJni: pid=334 uid=10038 gids={1015}06-21 02:45:49.662: INFO/jdwp(334): Ignoring second debugger -- accepting and dropping06-21 02:45:50.482: DEBUG/dalvikvm(334): Trying to load lib /data/data/com.example.hellojni/lib/libhello-jni.so 0x44edea6006-21 02:45:50.500: DEBUG/dalvikvm(334): Added shared lib /data/data/com.example.hellojni/lib/libhello-jni.so 0x44edea6006-21 02:45:50.500: DEBUG/dalvikvm(334): No JNI_OnLoad found in /data/data/com.example.hellojni/lib/libhello-jni.so 0x44edea60, skipping init06-21 02:45:51.282: INFO/ActivityManager(58): Displayed activity com.example.hellojni/.HelloJni: 2011 ms (total 1089367 ms)