您可以下载NDK r4 for Windows或NDK r5 for Windows安装包,下载地址:
地址:http://developer.android.com/sdk/ndk/index.html
由于NDK开发大都涉及到C/C++在GCC环境下编译、运行,所以在Windows环境下,需要模拟Linux模拟编译环境,下载地址:
http://www.cygwin.com/
package com.example; import android.util.Log; public class HardwareControlClass { static public native String stringFromJNI(); static public native int Open(); static public native int IOCTLLED(int ledID, int ledState ,int fd); static { try { System.loadLibrary("hello-jni"); } catch (UnsatisfiedLinkError e) { Log.d("HardwareControler", "HardwareControler ibrary not found!"); } } }
#include <string.h> #include <jni.h> #include <fcntl.h> #define DEV_NAME "/dev/leds" jstring Java_com_example_HardwareControlClass_stringFromJNI( JNIEnv* env, jobject thiz ) { return (*env)->NewStringUTF(env, "Hello from JNI !"); } jint Java_com_example_HardwareControlClass_Open(JNIEnv *env, jobject thiz) { int fd=open(DEV_NAME,O_RDWR); return fd; } jint Java_com_example_HardwareControlClass_IOCTLLED(JNIEnv *env, jobject thiz, jint ledid, jint controlcode, jint fd) { int CTLCODE = controlcode; ioctl(fd,controlcode,ledid); return 0; }
songa@songa-PC ~ $ cd $NDK songa@songa-PC /cygdrive/d/android/android-ndk-r8b-windows/android-ndk-r8b $ cd samples/hello-jni songa@songa-PC /cygdrive/d/android/android-ndk-r8b-windows/android-ndk-r8b/samples/hello-jni $ $NDK/ndk-build //在工程目录下编译,ndk-build会寻找jni目录,将其目录下的文件编译 Gdbserver : [arm-linux-androideabi-4.6] libs/armeabi/gdbserver Gdbsetup : libs/armeabi/gdb.setup Install : helloworld => libs/armeabi/helloworld //会在工程目录中生成libs目录直接测试c代码,jni目录下的文件如下
//Android.mk LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_SRC_FILES:=hello.c LOCAL_MODULE := helloworld LOCAL_MODULE_TAGS := optional include $(BUILD_EXECUTABLE)
//hello.c #include <stdio.h> int main() { printf("hello song\n"); }
//Android.mk LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := hello-jni LOCAL_SRC_FILES := hello-jni.c include $(BUILD_SHARED_LIBRARY)
//hello-jni.c #include <string.h> #include <jni.h> /* This is a trivial JNI example where we use a native method * to return a new VM String. See the corresponding Java source * file located at: * * apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java */ jstring Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz ) { return (*env)->NewStringUTF(env, "Hello from JNI !"); }
$ ls toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/arm-linux-androideabi/bin/ ar.exe as.exe c++.exe g++.exe gcc.exe ld.exe nm.exe objcopy.exe objdump.exe ranlib.exe strip.exe