1<html> 2<body> 3 <pre> 4Native Activities and Applications: 5----------------------------------- 6 7I. Overview 8=========== 9The Android SDK provides a helper class, NativeActivity, that allows you to write a completely 10native activity. With a native activity, it is possible to write a completely native application. 11NativeActivity handles the communication between the Android framework and your 12native code, so you do not have to subclass it or call its methods. All you need to do is declare 13your application to be native in your AndroidManifest.xml file and begin creating your native 14application. 15 16Native activities do not change the fact that Android applications still run in their own virtual 17machine, sandboxed from other applications. Because of this, you can still access Android framework 18APIs through the JNI. There are, however, native interfaces to access things such as sensors, input 19events, and assets that you can use. For more information about what is supported, see the 20<ndk_root>/docs/STABLE-APIS.HTML. 21 22If 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 24Android build tools, so the build system can only build Android projects that have the correct 25structure. Using the android tool or Eclipse helps ensure that. 26 27The 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 <ndk_root>/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 <ndk_root>/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 <ndk_root>/sources/android/native_app_glue/android_native_app_glue.h file 45 for more information. 46 47II. Using the native-activity.h interface: 48========================================== 49You can use the native-activity.h interface to implement a completely native activity. If you use 50this interface you must ensure that your callback implementations do not block the main UI thread. 51For more information on how to use this interface, see 52<ndk_root>/platforms/android-9/arch-arm/usr/include/android/native_activity.h. 53 54You might find it easier to use the native_app_glue static helper library that handles the 55callbacks in an event loop in another thread. See the native-activity sample application for more 56information on how to use this static library. 57 58To 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 <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"> 69 70 <uses-sdk android:minSdkVersion="9" /> 71 72 <application android:label="@string/app_name" android:hasCode="false"> 73 74 <activity android:name="android.app.NativeActivity" 75 android:label="@string/app_name" 76 android:configChanges="orientation|keyboardHidden"> 77 78 <meta-data android:name="android.app.lib_name" 79 android:value="native-activity" /> 80 <intent-filter> 81 <action android:name="android.intent.action.MAIN" /> 82 <category android:name="android.intent.category.LAUNCHER" /> 83 </intent-filter> 84 </activity> 85 </application> 86 </manifest> 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->callbacks to the implementations of your callbacks. 101 102 4/ Set the ANativeActivity->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->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 <ndk_root>/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 <ndk_root>/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