1page.title=Concepts 2@jd:body 3 4<div id="qv-wrapper"> 5 <div id="qv"> 6 <h2>On this page</h2> 7 8 <ol> 9 <li><a href="#bb">Before Beginning</a></li> 10 <li><a href="#intro">Introduction</a></li> 11 <li><a href="#hiw">How It Works</a></li> 12 <li><a href="#naa">Native Activities and Applications</a></li> 13 </ol> 14 </li> 15 </ol> 16 </div> 17 </div> 18 19<h2 id="bb">Before Beginning</h2> 20 21<p>This guide assumes that you are:</p> 22<ul> 23<li>Already familiar with concepts inherent in native programming and in 24<a href="{@docRoot}">Android development</a>.</li> 25<li>Working in <a href="{@docRoot}sdk/index.html">Eclipse, and using the Android 26Development Tools (ADT)</a>, except where otherwise noted.</li> 27</ul> 28<h2 id="intro">Introduction</h2> 29 30<p>This section provides a high-level explanation of how the NDK works. The Android NDK is a set of 31tools allowing you to embed C or C++ (“native code”) into your Android apps. The ability to use 32native code in Android apps can be particularly useful to developers who wish to do one or more of 33the following:</p> 34<ul> 35<li>Port their apps between platforms.</li> 36<li>Reuse existing libraries, or provide their own libraries for reuse. 37</li> 38<li>Increase performance in certain cases, particularly computationally intensive ones like games. 39</li> 40</ul> 41<h2 id="hiw">How it Works</h2> 42 43<p>This section introduces the main components used in building a native application for Android, 44and goes on to describe the process of building and packaging.</p> 45<h3 id="mc">Main components</h3> 46 47<p>You should have an understanding of the following components as you build your app:</p> 48<ul> 49<li>ndk-build: The ndk-build script launches the build scripts at the heart of the NDK. These 50scripts: 51<ul> 52<li>Automatically probe your development system and app project file to determine what to build.</li> 53<li>Generate binaries.</li> 54<li>Copy the binaries to your app's project path.</li> 55</ul> 56<p>For more information, see 57<a href="{@docRoot}ndk/guides/ndk-build.html">ndk-build</a>.</p> 58</li> 59</ul> 60 61<ul> 62<li>Java: From your Java source, the Android build process generates {@code .dex} 63(Dalvik EXecutable) files, which are what the Android OS runs in the Dalvik Virtual Machine 64(“DVM”). Even if your app contains no Java source code at all, the build process still generates a 65{@code .dex} executable file within which the native component runs. 66 67<p>When developing Java components, use the {@code native} keyword to indicate methods implemented 68as native code. For example, the following function declaration tells the compiler that the 69implementation is in a native library:</p> 70 71 72 73<pre> 74public native int add(int x, int y); 75</pre> 76</li> 77</ul> 78 79<ul> 80<li>Native shared libraries: The NDK builds these libraries, or {@code .so} files, from your native 81source code. 82 83<p class="note"><strong>Note:</strong> If two libraries implement respective methods with the same 84signature, a link error occurs. In C, "signature" means method name only. In C++, "signature" means 85not only method name, but also its argument names and types.</p> 86</li> 87</ul> 88 89<ul> 90<li>Native static libraries: The NDK can also build static libraries, or {@code .a} files, which you 91can link against other libraries.</li> 92</ul> 93 94<ul> 95<li>Java Native Interface (JNI): The JNI is the interface via which the Java and C++ components 96talk to one another. This guide assumes knowledge of the JNI; for information about it, consult the 97<a href="http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html"> 98Java Native Interface Specification</a>.</li> 99</ul> 100 101<ul> 102<li>Application Binary Interface (ABI): The ABI defines exactly how your app's machine code is 103expected to interact with the system at runtime. The NDK builds {@code .so} files against these 104definitions. Different ABIs correspond to different architectures: The NDK includes ABI support for 105ARMEABI (default), MIPS, and x86. For more information, see 106<a href="{@docRoot}ndk/guides/abis.html">ABI Management</a>.</li> 107</ul> 108 109<ul> 110<li>Manifest: If you are writing an app with no Java component to it, you must declare the 111{@link android.app.NativeActivity} class in the 112<a href="{@docRoot}guide/topics/manifest/manifest-intro.html">manifest</a>. 113<a href="#naa">Native Activities and Applications</a> provides more detail on how to do this, under 114“Using the {@code native_activity.h} interface.” 115</li> 116</ul> 117 118<p>The following two items are only required for building using the 119<a href="{@docRoot}ndk/guides/ndk-build.html">{@code ndk-build}</a> script, 120and for debugging using the <a href="{@docRoot}ndk/guides/ndk-gdb.html"> 121{@code ndk-gdb}</a> script. 122 123<ul> 124<li><a href="{@docRoot}ndk/guides/android_mk.html">{@code Android.mk}</a>: 125You must create an <a href="{@docRoot}ndk/guides/android_mk.html"> 126{@code Android.mk}</a> configuration file inside your {@code jni} folder. The {@code ndk-build} 127script looks at this file, which defines the module and its name, the source files to be compiled, 128build flags and libraries to link.</li> 129</ul> 130 131<ul> 132<li><a href="{@docRoot}ndk/guides/application_mk.html">{@code Application.mk}</a>: This file 133enumerates and describes the modules that your app requires. This information includes: 134 135<ul> 136<li>ABIs used to compile for specific platforms.</li> 137<li>Toolchains.</li> 138<li>Standard libraries to include (static and dynamic STLport or default system).</li> 139</ul> 140</li> 141</ul> 142 143 144<h3 id="fl">Flow</h3> 145 146<p>The general flow for developing a native app for Android is as follows:</p> 147<ol type="1"> 148<li>Design your app, deciding which parts to implement in Java, and which parts to implement as 149native code. 150 151<p class="note"><strong>Note:</strong> While it is possible to completely avoid Java, you are likely 152to find the Android Java framework useful for tasks including controlling the display and UI.</p> 153</li> 154 155<li>Create an Android app Project in Eclipse as you would for any other Android project.</li> 156<li>If you are writing a native-only app, declare the {@link android.app.NativeActivity} class in 157{@code AndroidManifest.xml}. You can do so from the Eclipse/ADT Android Manifest Editor, or by 158hand-editing the file. For more information, see the <a href="#naa">Native Activities and 159Applications</a>. 160</li> 161<li>Create an {@code Android.mk} file describing the native library, including name, flags, linked 162libraries, and source files to be compiled in the ‘JNI’ directory.</li> 163<li>Optionally, you can create an {@code Application.mk} file configuring the target ABIs, 164toolchain, release/debug mode, and STL. For any of these that you do not specify, the following 165default values are used, respectively: 166<ul> 167<li> 168ABI: armeabi 169 </li> 170<li> 171Toolchain: GCC 4.8 172 </li> 173<li> 174Mode: Release 175 </li> 176<li> 177STL: system 178</ul> 179</li> 180<li>Place your native source under the project's {@code jni} directory.</li> 181<li>Use ndk-build to compile the native ({@code .so}, {@code .a}) libraries.</li> 182<li>Build the Java component, producing the executable {@code .dex} file.</li> 183<li>Package everything into an APK file, containing {@code .so}, {@code .dex}, and other files 184needed for your app to run. 185</ol> 186 187<p>Note that Eclipse can perform steps 7. through 9. in a single operation.</p> 188 189<h2 id="naa">Native Activities and Applications</h2> 190 191<p>The Android SDK provides a helper class, {@link android.app.NativeActivity}, that allows you to 192write a completely native activity. {@link android.app.NativeActivity} handles the communication 193between the Android framework and your native code, so you do not have to subclass it or call its 194methods. All you need to do is declare your application to be native in your 195{@code AndroidManifest.xml} file, and begin creating your native application.</p> 196 197<p>An Android application using {@link android.app.NativeActivity} still runs in its own virtual 198machine, sandboxed from other applications. You can therefore still access Android framework APIs 199through the JNI. In certain cases, however–such as for sensors, input events, and 200assets–the NDK provides native interfaces that you can use instead of having to call 201across the JNI. For more information about such support, see 202<a href="{@docRoot}ndk/guides/stable_apis.html">Android NDK Native APIs</a>.</p> 203 204<p>Regardless of whether or not you are developing a native activity, we recommend that you create 205your projects with the traditional Android build tools. Doing so helps ensure building and packaging 206of Android applications with the correct structure.</p> 207 208<p>The Android NDK provides you with two choices to implement your native activity:</p> 209 210<ul> 211<li>The <a href="{@docRoot}ndk/reference/native__activity_8h.html">{@code native_activity.h}</a> 212header defines the native version of the 213{@link android.app.NativeActivity} class. It contains the callback interface and data structures 214that you need to create your native activity. Because the main thread of your application handles 215the callbacks, your callback implementations must not be blocking. If they block, you might receive 216ANR (Application Not Responding) errors because your main thread is unresponsive until the callback 217returns.</li> 218<li>The {@code android_native_app_glue.h} file defines a static helper library built on top of the 219<a href="{@docRoot}ndk/reference/native__activity_8h.html">{@code native_activity.h}</a> interface. 220It spawns another thread, which handles things such as 221callbacks or input events in an event loop. Moving these events to a separate thread prevents any 222callbacks from blocking your main thread.</li> 223</ul> 224 225<p>The {@code <ndk_root>/sources/android/native_app_glue/android_native_app_glue.c} source is 226also available, allowing you to modify the implementation.</p> 227<p>For more information on how to use this static library, examine the native-activity sample 228application and its documentation. Further reading is also available in the comments in the {@code <ndk_root>/sources/android/native_app_glue/android_native_app_glue.h} file.</p> 229 230<h3 id="na">Using the native_activity.h interface</h3> 231 232<p>To implement a native activity with the 233<a href="{@docRoot}ndk/reference/native__activity_8h.html">{@code native_activity.h}</a> 234interface:</p> 235 236<ol type="1"> 237<li>Create a {@code jni/} directory in your project's root directory. This directory stores all of 238your native code.</li> 239<li>Declare your native activity in the {@code AndroidManifest.xml} file.</li> 240 241<p>Because your application has no Java code, set {@code android:hasCode} to {@code false}.</p> 242 243<pre> 244<application android:label="@string/app_name" android:hasCode="false"> 245</pre> 246 247<p>You must set the {@code android:name} attribute of the activity tag to 248{@link android.app.NativeActivity}.</p> 249 250<pre> 251<activity android:name="android.app.NativeActivity" 252 android:label="@string/app_name"> 253</pre> 254<p class="note"><strong>Note:</strong> You can subclass {@link android.app.NativeActivity}. If you 255do, use the name of the subclass instead of {@link android.app.NativeActivity}.</p> 256<p>The {@code android:value} attribute of the {@code meta-data} tag specifies the name of the shared 257library containing the entry point to the application (such as C/C++ {@code main}), omitting the 258{@code lib} prefix and {@code .so} suffix from the library name.</p> 259 260<pre> 261 <meta-data android:name="android.app.lib_name" 262 android:value="native-activity" /> 263 <intent-filter> 264 <action android:name="android.intent.action.MAIN" /> 265 <category android:name="android.intent.category.LAUNCHER" /> 266 </intent-filter> 267 </activity> 268 </application> 269 </manifest> 270</pre> 271 272<li>Create a file for your native activity, and implement the function named in the 273<a href="{@docRoot}ndk/reference/group___native_activity.html#ga02791d0d490839055169f39fdc905c5e"> 274{@code ANativeActivity_onCreate}</a> variable. 275The app calls this function when the native activity starts. This function, analogous 276to {@code main} in C/C++, receives a pointer to an 277<a href="{@docRoot}ndk/reference/struct_a_native_activity.html">{@code ANativeActivity}</a> 278structure, which contains function pointers to the various callback implementations that you need 279to write. 280Set the applicable callback function pointers in {@code ANativeActivity->callbacks} to the 281implementations of your callbacks.</li> 282 283<li>Set the {@code ANativeActivity->instance} field to the address of any instance of specific 284data that you want to use.</li> 285<li>Implement anything else that you want your activity to do upon starting.</li> 286<li>Implement the rest of the callbacks that you set in {@code ANativeActivity->callbacks}. For 287more information on when the callbacks are called, see 288<a href="{@docRoot}training/basics/activity-lifecycle/index.html">Managing the Activity 289Lifecycle</a>. 290</li> 291<li>Develop the rest of your application.</li> 292<li>Create an {@code Android.mk file} in the {@code jni/} directory of your project to describe your 293native module to the build system. For more information, see 294<a href="{@docRoot}ndk/guides/android_mk.html">Android.mk</a>.</li> 295<li>Once you have an <a href="{@docRoot}ndk/guides/android_mk.html">{@code Android.mk}</a> 296file, compile your native code using the {@code ndk-build} command.</li> 297 298<pre class="no-pretty-print"> 299$ cd <path>/<to>/<project> 300$ <ndk>/ndk-build 301</pre> 302 303<li>Build and install your Android project as usual, using Ant or Eclipse. If your native code is in 304the {@code jni/} directory, the build script automatically packages the {@code .so} file(s) built 305from it into the APK.</li> 306</ol> 307 308</li> 309</ul> 310