转载自:http://blog.csdn.net/quaful/article/details/6050618
编译
1. 下载安装 Android NDK,不必赘述。假定我们安装到了 /Developer/android-ndk-r4b/.
2. 在工作目录下建立一个 jni 目录。如果要使用 NDK 自带的 Android.mk,必须叫做这个名字,否则 make 的时候会找不到文件,错误提示可能类似下面:
- Android NDK: Could not find application project directory !
- Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.
- /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 文件内容:
- # Copyright (C) 2009 The Android Open Source Project
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- #
- LOCAL_PATH := $(call my-dir)
- include $(CLEAR_VARS)
- LOCAL_MODULE := hello
- LOCAL_SRC_FILES := hello.c
- #include $(BUILD_SHARED_LIBRARY)
- include $(BUILD_EXECUTABLE)
4. 在 <Project> 目录中运行 /Developer/android-ndk-r4b/ndk-build. 如果编译没有错误,那么输出为:
- Compile thumb : hello <= <Project>/jni/hello.c
- Executable : hello
- Install : hello => <Project>/libs/armeabi
那么,我们需要的可执行文件就在 libs/armeabi 目录下了。可以用 adb push 上传到手机或者模拟器中运行测试。