用 Android NDK 开发 Native 应用程序(一)

转载自:http://blog.csdn.net/quaful/article/details/6050618




编译

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 上传到手机或者模拟器中运行测试。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值