Android NDK How-To ---- Android 4.4
2013-12-20 10:59
399 查看
Android NDK How-To:
===================
A collection of tips and tricks for NDK users
How to force the display of build commands:
-------------------------------------------
Do "ndk-build V=1" and actual build commands will be
displayed. This can be used to verify that things are compiled
as you expect them to, and check for bugs in the NDK build system.
(The V=1 trick comes from the Linux kernel build system)
How to force a rebuild of all your sources:
-------------------------------------------
Use GNU Make's "-B" option, as in:
ndk-build -B
How to store your native sources in a location other than $PROJECT/jni:
-----------------------------------------------------------------------
First, you can simply tell your $PROJECT/jni/Android.mk to include
another Android.mk that are located in different places.
Alternatively, you can define APP_BUILD_SCRIPT in your Application.mk
to point to an alternative Android.mk file.
How to build a project's native files without cd-ing to it:
-----------------------------------------------------------
Sometimes, you may need to rebuild a project's native file without
being able to cd to its top-level path from the command-line. This
is do-able by using the GNU-Make '-C <path>' option, as in:
ndk-build -C <project-path>
How to store your Application.mk in a location other than $PROJECT/jni:
-----------------------------------------------------------------------
Starting with NDK r4, you can simply place the file under $PROJECT/jni/
and launch the 'ndk-build' script from your project tree.
If you want to use 'ndk-build' but place the file to a different location,
use a GNU Make variable override as:
ndk-build NDK_APPLICATION_MK=/path/to/your/Application.mk
If you're using the legacy $NDK/apps/<name> build method, you can create
a symbolic link to your final Application.mk there. For example, imagine
that you wrote:
$PROJECT/foo/Application.mk
You can create a symlink like with a command like:
ln -s $PROJECT/foo $NDK/apps/<name>
This will make $NDK/apps/<name>/Application.mk point directly to
$PROJECT/jni/Application.mk
Note that generated files will still go under $NDK/out/apps/<name> though.
Windows users: The NDK is only supported on Cygwin, which implements
symbolic links through the "ln -s" command, as in:
ln -s <target> <link>
How to properly add include directories to your module declaration:
-------------------------------------------------------------------
If you define several modules, it is common to need to include one
module's header while compiling another one. For example, consider
the following example:
$PROJECT/jni/foo/
Android.mk
foo.h
foo.c
$PROJECT/jni/bar/
Android.mk
bar.c
Where the 'bar.c' uses '#include <foo.h>'. You will need to add the
path to the 'foo' module in jni/bar/Android.mk to build it properly.
One is tempted to use the following:
LOCAL_C_INCLUDES := ../foo
However this will not work because all compilation happens from the
directory where 'ndk-build' is invoked, and include files must be
relative to it.
The correct line is instead:
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../foo
Which uses a path relative to $(LOCAL_PATH), in the case where you would
need to move 'foo' and 'bar' to a deeper level in the 'sources' hierarchy.
In case you absolutely need it, you can also use NDK_APP_PROJECT_PATH to
point to your project directory:
LOCAL_C_INCLUDES := $(NDK_APP_PROJECT_PATH)/jni/foo
However, we don't recommend using this, paths relative to $(LOCAL_PATH)
being better.
===================
A collection of tips and tricks for NDK users
How to force the display of build commands:
-------------------------------------------
Do "ndk-build V=1" and actual build commands will be
displayed. This can be used to verify that things are compiled
as you expect them to, and check for bugs in the NDK build system.
(The V=1 trick comes from the Linux kernel build system)
How to force a rebuild of all your sources:
-------------------------------------------
Use GNU Make's "-B" option, as in:
ndk-build -B
How to store your native sources in a location other than $PROJECT/jni:
-----------------------------------------------------------------------
First, you can simply tell your $PROJECT/jni/Android.mk to include
another Android.mk that are located in different places.
Alternatively, you can define APP_BUILD_SCRIPT in your Application.mk
to point to an alternative Android.mk file.
How to build a project's native files without cd-ing to it:
-----------------------------------------------------------
Sometimes, you may need to rebuild a project's native file without
being able to cd to its top-level path from the command-line. This
is do-able by using the GNU-Make '-C <path>' option, as in:
ndk-build -C <project-path>
How to store your Application.mk in a location other than $PROJECT/jni:
-----------------------------------------------------------------------
Starting with NDK r4, you can simply place the file under $PROJECT/jni/
and launch the 'ndk-build' script from your project tree.
If you want to use 'ndk-build' but place the file to a different location,
use a GNU Make variable override as:
ndk-build NDK_APPLICATION_MK=/path/to/your/Application.mk
If you're using the legacy $NDK/apps/<name> build method, you can create
a symbolic link to your final Application.mk there. For example, imagine
that you wrote:
$PROJECT/foo/Application.mk
You can create a symlink like with a command like:
ln -s $PROJECT/foo $NDK/apps/<name>
This will make $NDK/apps/<name>/Application.mk point directly to
$PROJECT/jni/Application.mk
Note that generated files will still go under $NDK/out/apps/<name> though.
Windows users: The NDK is only supported on Cygwin, which implements
symbolic links through the "ln -s" command, as in:
ln -s <target> <link>
How to properly add include directories to your module declaration:
-------------------------------------------------------------------
If you define several modules, it is common to need to include one
module's header while compiling another one. For example, consider
the following example:
$PROJECT/jni/foo/
Android.mk
foo.h
foo.c
$PROJECT/jni/bar/
Android.mk
bar.c
Where the 'bar.c' uses '#include <foo.h>'. You will need to add the
path to the 'foo' module in jni/bar/Android.mk to build it properly.
One is tempted to use the following:
LOCAL_C_INCLUDES := ../foo
However this will not work because all compilation happens from the
directory where 'ndk-build' is invoked, and include files must be
relative to it.
The correct line is instead:
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../foo
Which uses a path relative to $(LOCAL_PATH), in the case where you would
need to move 'foo' and 'bar' to a deeper level in the 'sources' hierarchy.
In case you absolutely need it, you can also use NDK_APP_PROJECT_PATH to
point to your project directory:
LOCAL_C_INCLUDES := $(NDK_APP_PROJECT_PATH)/jni/foo
However, we don't recommend using this, paths relative to $(LOCAL_PATH)
being better.
相关文章推荐
- How to run test cases on android-ndk
- How to change statusbar text color to dark on android 4.4
- How to Build and Run Android NDK Examples?
- 如何在Android设备中用NDK编译SQLite并且对SQLite进行操作(增删)-How to Compile SQLite for Android using NDK
- How to include <android/bitmap.h> and other *.h NDK libraries to Titanium Android module?
- Step by Step - How to create a c++ library with NDK on Android Studio 1.5 (not experimental way)
- Why your Android NDK breakpoints might fail and how to fix them
- How to Render Image Buffer in Android NDK Native Code
- How to include <android/bitmap.h> and other *.h NDK libraries to Titanium Android module?
- How to Render Image Buffer in Android NDK Native Code
- How to Compile SQLite for Android using NDK
- HOWTO install and setup Android NDK for Windows(Android NDK 开发环境安装和配置)
- How to get file path in onActivityResult in Android 4.4
- [HOW TO] 获得android手机的CPU核心数
- Android 如何在自定义界面上启用输入法 (How to enable inputmethod for the custom UI)
- How to use jquery ajax and android request security RESTful WCF
- How to install Android SDK and play with Android 2.0 in the emulator
- [GUIDE][SCRIPT] How to root Android x86 4.3
- Using Android Studio to build applications with NDK and Boost C++ libraries
- How to use Google Test on Android-4.0.3_r1