page.title=Concepts @jd:body

On this page

  1. Before Beginning
  2. Introduction
  3. How It Works
  4. Native Activities and Applications

Before Beginning

This guide assumes that you are already familiar with concepts inherent in native programming and in Android development.

Introduction

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:

How it Works

This section introduces the main components used in building a native application for Android, and goes on to describe the process of building and packaging.

Main components

You should have an understanding of the following components as you build your app:

The following two items are only required for building using the {@code ndk-build} script, and for debugging using the {@code ndk-gdb} script.

Flow

The general flow for developing a native app for Android is as follows:

  1. Design your app, deciding which parts to implement in Java, and which parts to implement as native code.

    Note: While it is possible to completely avoid Java, you are likely to find the Android Java framework useful for tasks including controlling the display and UI.

  2. Create an Android app Project as you would for any other Android project.
  3. If you are writing a native-only app, declare the {@link android.app.NativeActivity} class in {@code AndroidManifest.xml}. For more information, see the Native Activities and Applications.
  4. Create an {@code Android.mk} file describing the native library, including name, flags, linked libraries, and source files to be compiled in the "JNI" directory.
  5. Optionally, you can create an {@code 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:
  6. Place your native source under the project's {@code jni} directory.
  7. Use ndk-build to compile the native ({@code .so}, {@code .a}) libraries.
  8. Build the Java component, producing the executable {@code .dex} file.
  9. Package everything into an APK file, containing {@code .so}, {@code .dex}, and other files needed for your app to run.

Native Activities and Applications

The Android SDK provides a helper class, {@link android.app.NativeActivity}, that allows you to write a completely native activity. {@link android.app.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 {@code AndroidManifest.xml} file, and begin creating your native application.

An Android application using {@link android.app.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 Android NDK Native APIs.

Regardless of whether or not you are developing a native activity, we recommend that you create your projects with the traditional 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:

The {@code /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 {@code /sources/android/native_app_glue/android_native_app_glue.h} file.

Using the native_activity.h interface

To implement a native activity with the {@code native_activity.h} interface:

  1. Create a {@code jni/} directory in your project's root directory. This directory stores all of your native code.
  2. Declare your native activity in the {@code AndroidManifest.xml} file.
  3. Because your application has no Java code, set {@code android:hasCode} to {@code false}.

    <application android:label="@string/app_name" android:hasCode="false">
    

    You must set the {@code android:name} attribute of the activity tag to {@link android.app.NativeActivity}.

    <activity android:name="android.app.NativeActivity"
                android:label="@string/app_name">
    

    Note: You can subclass {@link android.app.NativeActivity}. If you do, use the name of the subclass instead of {@link android.app.NativeActivity}.

    The {@code android:value} attribute of the {@code meta-data} tag specifies the name of the shared library containing the entry point to the application (such as C/C++ {@code main}), omitting the {@code lib} prefix and {@code .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>
    
  4. Create a file for your native activity, and implement the function named in the {@code ANativeActivity_onCreate} variable. The app calls this function when the native activity starts. This function, analogous to {@code main} in C/C++, receives a pointer to an {@code ANativeActivity} structure, which contains function pointers to the various callback implementations that you need to write. Set the applicable callback function pointers in {@code ANativeActivity->callbacks} to the implementations of your callbacks.
  5. Set the {@code ANativeActivity->instance} field to the address of any instance of specific data that you want to use.
  6. Implement anything else that you want your activity to do upon starting.
  7. Implement the rest of the callbacks that you set in {@code ANativeActivity->callbacks}. For more information on when the callbacks are called, see Managing the Activity Lifecycle.
  8. Develop the rest of your application.
  9. Create an {@code Android.mk file} in the {@code jni/} directory of your project to describe your native module to the build system. For more information, see Android.mk.
  10. Once you have an {@code Android.mk} file, compile your native code using the {@code ndk-build} command.
  11. $ cd <path>/<to>/<project>
    $ <ndk>/ndk-build
    
  12. Build and install your Android project as usual. If your native code is in the {@code jni/} directory, the build script automatically packages the {@code .so} file(s) built from it into the APK.