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