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