Home | History | Annotate | Download | only in text
      1 Native Activities and Applications:
      2 ===
      3 
      4 I. Overview
      5 ===========
      6 The Android SDK provides a helper class, `NativeActivity`, that allows you to write a completely
      7 native activity. With a native activity, it is possible to write a completely native application.
      8 NativeActivity handles the communication between the Android framework and your
      9 native code, so you do not have to subclass it or call its methods. All you need to do is declare
     10 your application to be native in your `AndroidManifest.xml` file and begin creating your native
     11 application.
     12 
     13 Native activities do not change the fact that Android applications still run in their own virtual
     14 machine, sandboxed from other applications. Because of this, you can still access Android framework
     15 APIs through the JNI. There are, however, native interfaces to access things such as sensors, input
     16 events, and assets that you can use. For more information about what is supported, see the
     17 docs/STABLE-APIS.HTML.
     18 
     19 If you are developing a native activity, you should still create your projects with Eclipse or the
     20 "android create project" command. You still build and package native applications with the usual
     21 Android build tools, so the build system can only build Android projects that have the correct
     22 structure. Using the android tool or Eclipse helps ensure that.
     23 
     24 The Android NDK provides you with two choices to implement your native activity:
     25 
     26   - The native_activity.h header defines the native version of the NativeActivity class. It
     27     contains the callback interface and data structures that you need to create your native
     28     activity. Because the main thread of your application handles the callbacks, your callback
     29     implementations must not be blocking. If they block, you might receive ANR (Application Not
     30     Responding) errors because your main thread will be unresponsive until the callback returns.
     31     Read the comments in the 
     32     `<ndk_root>/platforms/android-9/arch-arm/usr/include/android/native_activity.h` file for
     33     more information.
     34 
     35   - The android_native_app_glue.h file defines a static helper library built on top of the
     36     native_activity.h interface. It spawns another thread to handle things such as callbacks or
     37     input events. This prevents any callbacks from blocking your main thread and adds some
     38     flexibility in how you implement the callbacks, so you might find this programming model a bit
     39     easier to implement. The `<ndk_root>/sources/android/native_app_glue/android_native_app_glue.c`
     40     source is also available to you, so you can modify the implementation if you need. Read the
     41     comments in the `<ndk_root>/sources/android/native_app_glue/android_native_app_glue.h` file
     42     for more information.
     43 
     44 II. Using the native-activity.h interface:
     45 ==========================================
     46 
     47 You can use the native-activity.h interface to implement a completely native activity. If you use
     48 this interface you must ensure that your callback implementations do not block the main UI thread.
     49 For more information on how to use this interface, see 
     50 `<ndk_root>/platforms/android-9/arch-arm/usr/include/android/native_activity.h`.
     51 
     52 You might find it easier to use the native_app_glue static helper library that handles the
     53 callbacks in an event loop in another thread. See the native-activity sample application for more
     54 information on how to use this static library.
     55 
     56 To implement a native activity with the native-activity.h interface:
     57 
     58   1. Create a project with the "android create project" command or from Eclipse. Create a jni/
     59      directory in the project's root directory. This directory stores all of your native code.
     60 
     61   2. Declare your native activity in the AndroidManifest.xml file. An example is shown below:
     62 
     63           <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     64             package="com.example.native_activity"
     65             android:versionCode="1"
     66             android:versionName="1.0">
     67 
     68             <uses-sdk android:minSdkVersion="9" />
     69 
     70             <application android:label="@string/app_name" android:hasCode="false">
     71 
     72               <activity android:name="android.app.NativeActivity"
     73                 android:label="@string/app_name"
     74                 android:configChanges="orientation|keyboardHidden">
     75 
     76               <meta-data android:name="android.app.lib_name"
     77                 android:value="native-activity" />
     78                 <intent-filter>
     79                   <action android:name="android.intent.action.MAIN" />
     80                   <category android:name="android.intent.category.LAUNCHER" />
     81                 </intent-filter>
     82               </activity>
     83             </application>
     84           </manifest>
     85 
     86     The main things to note are:
     87 
     88       - The android:name attribute of the activity tag must be set to android.app.NativeActivity.
     89         It is possible to subclass the NativeActivity, however, so if you do, specify the name of
     90         that class instead.
     91       - The android:name attribute of the meta-data tag must be in the form of android.app.lib_name
     92         where lib_name is the name of the module without the lib prefix and .so suffix.
     93 
     94   3. Create a file for your native activity and implement the ANativeActivity_onCreate() function,
     95      which is called when your native activity starts. This function receives a pointer to an
     96      ANativeActivity structure, which contains function pointers to the various callback
     97      implementations that you need to write. Set the applicable callback function pointers in
     98      ANativeActivity->callbacks to the implementations of your callbacks.
     99 
    100   4. Set the ANativeActivity->instance field to the address of any instance specific data that
    101      you want to use.
    102 
    103   5. Implement any other things that you want your activity to do upon starting.
    104 
    105   6. Implement the rest of the callbacks that you set in ANativeActivity->callbacks. For more
    106      information on when the callbacks are called, see the SDK documentation for Activity
    107      Lifecycles. Remember that your callback implementations must not be blocking, or you might get
    108      ANR (Application Not Responding) errors because the main UI thread is waiting for the callbacks
    109      to return.
    110 
    111   7. Develop the rest of your application.
    112 
    113   8. Create an Android.mk file in the jni/ directory of your project to describe your native module
    114      to the build system. An Android.mk file is essentially a snippet of a GNU Make file. For
    115      example:
    116 
    117           LOCAL_PATH := $(call my-dir)
    118           include $(CLEAR_VARS)
    119           LOCAL_MODULE    := my_native_module
    120           LOCAL_SRC_FILES := my_native_code.c
    121           include $(BUILD_SHARED_LIBRARY)
    122 
    123      For more information on how to create an Android.mk file and what the variables mean,
    124      see the <ndk_root>/docs/ANDROID-MK.TXT file.
    125 
    126   9. Once you have an Android.mk file, compile your native code using the "ndk-build" command.
    127 
    128           cd path/to/project
    129           <ndk_root>/ndk-build
    130 
    131  10. Build and install your Android project as usual, using Ant or Eclipse. The build automatically
    132      packages your native code into the .apk file if it is present in the jni/ directory.
    133