1. 新建一个project目录ledapp 在ledapp下建立一个 jni 目录。如果要使用 NDK 自带的 Android.mk,必须叫做这个名字。
把源码放到这个目录。这里程序的名字是 led_misc_test.c,那么目录结构为 ledapp/jni/led_misc_test.c
//led_misc_test.c 1 #include<unistd.h> 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<sys/ioctl.h> 5 6 int main(int argc, char **argv) 7 { 8 int fd; 9 unsigned int state; 10 11 if(argc != 2 || sscanf(argv[1],"%d",&state) != 1 || state > 1 ){ 12 printf("Useage: xxx #./filename 1|0 \n"); 13 exit(0); 14 } 15 16 fd = open("/dev/leds",0); 17 if(fd < 0){ 18 printf("error to open /dev/leds\n"); 19 exit(0); 20 } 21 22 ioctl(fd,state,NULL); 23 close(fd); 24 return 0; 25 }
2. 从 android-ndk-r6b 的 sample 里复制一份 Android.mk 到 jni 目录。因为我们编译的是 Native App 可执行程序而不是 JNI lib,所以需要修改 Android.mk,把 include $(BUILD_SHARED_LIBRARY ) 改成 include $(BUILD_EXECUTABLE ).
这里给出示例的 Android.mk 文件内容:
1 # Copyright (C) 2009 The Android Open Source Project 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # 15 LOCAL_PATH := $(call my-dir) 16 17 include $(CLEAR_VARS) 18 19 LOCAL_MODULE := led_misc_test 20 LOCAL_SRC_FILES := led_misc_test.c 21 22 include $(BUILD_EXECUTABLE)
3. 在 ledapp目录中运行 ndk-build (如果没有添加环境变量则要运行)/home/android/android-ndk-r6b/ndk-build. 如果编译没有错误,那么输出为:
那么,我们需要的可执行文件就在./libs/armeabi 目录下了。可以用 adb push上传 或 u盘拷贝 到手机(开发板)或者模拟器中运行测试。