Android NDK 开发 Native 应用程序


编译

1. 下载安装 Android NDK,不必赘述。假定我们安装到了 /Developer/android-ndk-r4b/.

2. 在工作目录下建立一个 jni 目录。如果要使用 NDK 自带的 Android.mk,必须叫做这个名字,否则 make 的时候会找不到文件,错误提示可能类似下面:

  1. Android NDK: Could not find application project directory !       
  2. Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.       
  3. /Developer/android-ndk-r4b/build/core/build-local.mk:85: *** Android NDK: Aborting    .  Stop.  

把程序源码放到这个目录。假定程序的名字是 hello.c,那么目录结构为

<Project>/jni/hello.c

3. 从 NDK 的 sample 里复制一份 Android.mk 到 jni 目录。因为我们编译的是 Native App 可执行程序而不是 JNI lib,所以需要修改 Android.mk,把 include $(BUILD_SHARED_LIBRARY ) 改成 include $(BUILD_EXECUTABLE ).

这里给出示例的 Android.mk 文件内容:

  1.     
  2. # Copyright (C) 2009 The Android Open Source Project   
  3. #   
  4. # Licensed under the Apache License, Version 2.0 (the "License");   
  5. # you may not use this file except in compliance with the License.   
  6. # You may obtain a copy of the License at   
  7. #   
  8. #      http://www.apache.org/licenses/LICENSE-2.0   
  9. #   
  10. # Unless required by applicable law or agreed to in writing, software   
  11. # distributed under the License is distributed on an "AS IS" BASIS,   
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   
  13. # See the License for the specific language governing permissions and   
  14. # limitations under the License.   
  15. #   
  16. LOCAL_PATH := $(call my-dir)   
  17. include $(CLEAR_VARS)   
  18. LOCAL_MODULE    := hello   
  19. LOCAL_SRC_FILES := hello.c   
  20. #include $(BUILD_SHARED_LIBRARY)   
  21. include $(BUILD_EXECUTABLE)  

4. 在 <Project> 目录中运行 /Developer/android-ndk-r4b/ndk-build. 如果编译没有错误,那么输出为:

  1. Compile thumb  : hello <= <Project>/jni/hello.c   
  2. Executable     : hello   
  3. Install        : hello => <Project>/libs/armeabi  

那么,我们需要的可执行文件就在 libs/armeabi 目录下了。可以用 adb push 上传到手机或者模拟器中运行测试。

  • 1
  • 2
  • 下一页
【内容导航】
第1页:编译 第2页:调试

相关内容