NDK Programmer's Guide
|
Native Activities and Applications
This guide assumes that you are:
This section provides a high-level explanation of how the NDK works. The Android NDK is a set of tools allowing you to embed C or C++ (“native code”) into your Android apps. The ability to use native code in Android apps can be particularly useful to developers who wish to do one or more of the following:
This section introduces the main components used in building a native application for Android, and goes on to describe the flow of building and packaging.
You should have an understanding of the following components, as you build your app:
For more information, see the ndk-build section of this guide.
.dex
("Dalvik EXecutable") files, which are what the Android OS
runs in the Dalvik Virtual Machine (“DVM”). Even if your app contains no
Java source code at all, the build process still generates a .dex
executable file within which the native component runs.When developing Java components, use the native
keyword to
indicate methods implemented as native code. For example, the following
function declaration tells the compiler that the implementation is in a native
library:
public native int add(int x, int y);
.so
files, from your native source code..a
files, which you can link against other libraries..so
files against these definitions. Different ABIs
correspond to different architectures: The NDK includes ABI support for ARMEABI
(default), MIPS, and x86. For more information, see Supported ABIs.Android.mk
: You must create an Android.mk
configuration file inside your jni
folder. The ndk-build script
looks at this file, which defines the module and its name, the source files to
be compiled, build flags and libraries to link. For more information, see the
Android.mk section of this document.Application.mk
: You may optionally create an Application.mk
file. This file enumerates and describes the modules that your app requires.
This information includes:For more information, see the Applicatio n.mk section.
The general flow for developing a native app for Android is as follows:
Application.mk
file configuring the target
ABIs, toolchain, release/debug mode, and STL. For any of these that you do not
specify, the following default values are used, respectively: jni
directory..so
, .a
)
libraries..dex
file..so
,
.dex
, and other files needed for your app to run.Note that Eclipse can perform steps 7. through 9. in a single operation.
The Android SDK provides a helper class, NativeActivity
, that
allows you to write a completely native activity. NativeActivity
handles the communication between the Android framework and your native code,
so you do not have to subclass it or call its methods. All you need to do is
declare your application to be native in your AndroidManifest.xml
file, and begin creating your native application.
An Android application using NativeActivity
still runs in its
own virtual machine, sandboxed from other applications. You can therefore still
access Android framework APIs through the JNI. In certain cases,
however–such as for sensors, input events, and assets–the NDK
provides native interfaces that you can use instead of having to call across
the JNI. For more information about such support, see
STABLE-APIS.HTML
.
Regardless of whether or not you are developing a native activity, we recommend that you create your projects with the usual Android build tools. Doing so helps ensure building and packaging of Android applications with the correct structure.
The Android NDK provides you with two choices to implement your native activity:
native_activity.h
header defines the native version of the
NativeActivity class. It contains the callback interface and data structures
that you need to create your native activity. Because the main thread of your
application handles the callbacks, your callback implementations must not be
blocking. If they block, you might receive ANR (Application Not Responding)
errors because your main thread is unresponsive until the callback returns.native_activity.h
interface. It spawns another thread,
which handles things such as callbacks or input events in an event loop. Moving
these events to a separate thread prevents any callbacks from blocking your
main thread.The
<ndk_root>/sources/android/native_app_glue/android_native_app_glue.c
source is also available, allowing you to modify the implementation.
For more information on how to use this static library, examine the
native-activity sample application, and its documentation. Further reading is
also available in the comments in the
<ndk_root>/sources/android/native_app_glue/android_native_app_glue.h
file.
native-activity.h
interfaceTo implement a native activity with the native-activity.h
interface:
jni/
directory in your project's root directory. This
directory stores all of your native code.AndroidManifest.xml
file.Because your application has no Java code, set android:hasCode
to false
.
<application android:label="@string/app_name" android:hasCode="false">
The android:name
attribute of the activity tag must be
set to android.app.NativeActivity
.
<activity android:name="android.app.NativeActivity" android:label="@string/app_name"Note: You can subclass
NativeActivity
. If you do, use the
name of the subclass instead of NativeActivity
.
The android:value
attribute of the meta-data
tag
specifies the name of the shared library containing the entry point to the
application (e.g., C/C++ main
), omitting the lib
prefix and .so
suffix from the library name.
<meta-data android:name="android.app.lib_name" android:value="native-activity" /> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
ANativeActivity_onCreate()
function, which the app calls when the
native activity starts. This function, analogous to main
in C/C++,
receives a pointer to an ANativeActivity
structure, which contains
function pointers to the various callback implementations that you need to
write. Set the applicable callback function pointers in
ANativeActivity->;callbacks
to the implementations of your
callbacks.ANativeActivity->;instance
field to the address of
any instance of specific data that you want to use.ANativeActivity->;callbacks
. For more information on when the
callbacks are called, see the SDK documentation for Activity Lifecycles.Android.mk file
in the jni/
directory
of your project to describe your native module to the build system. For more
information, see the Android.mk section.Android.mk
file, compile your native code
using the ndk-build
command.$ cd <path>/<to>/<project> $ <ndk>/ndk-build
jni/
directory, the build script
automatically packages the .so
file(s) built from it into the
APK.You can find further information on using
native-activity.h
in the comments in the
<ndk_root>/platforms/android-9/arch-arm/usr/include/android/native_a
ctivity.h
file.