• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml">
4<head>
5<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
6<meta http-equiv="X-UA-Compatible" content="IE=9"/>
7<meta name="generator" content="Doxygen 1.8.5"/>
8<title>NDK Programmer&#39;s Guide: Concepts</title>
9<link href="tabs.css" rel="stylesheet" type="text/css"/>
10<script type="text/javascript" src="jquery.js"></script>
11<script type="text/javascript" src="dynsections.js"></script>
12<link href="navtree.css" rel="stylesheet" type="text/css"/>
13<script type="text/javascript" src="resize.js"></script>
14<script type="text/javascript" src="navtree.js"></script>
15<script type="text/javascript">
16  $(document).ready(initResizable);
17  $(window).load(resizeHeight);
18</script>
19<link href="doxygen.css" rel="stylesheet" type="text/css" />
20</head>
21<body>
22<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
23<div id="titlearea">
24<table cellspacing="0" cellpadding="0">
25 <tbody>
26 <tr style="height: 56px;">
27  <td style="padding-left: 0.5em;">
28   <div id="projectname">NDK Programmer&#39;s Guide
29   </div>
30  </td>
31 </tr>
32 </tbody>
33</table>
34</div>
35<!-- end header part -->
36<!-- Generated by Doxygen 1.8.5 -->
37</div><!-- top -->
38<div id="side-nav" class="ui-resizable side-nav-resizable">
39  <div id="nav-tree">
40    <div id="nav-tree-contents">
41      <div id="nav-sync" class="sync"></div>
42    </div>
43  </div>
44  <div id="splitbar" style="-moz-user-select:none;"
45       class="ui-resizable-handle">
46  </div>
47</div>
48<script type="text/javascript">
49$(document).ready(function(){initNavTree('md_1__concepts__concepts.html','');});
50</script>
51<div id="doc-content">
52<div class="header">
53  <div class="headertitle">
54<div class="title">Concepts </div>  </div>
55</div><!--header-->
56<div class="contents">
57<div class="textblock"><ul style="list-style: none; ">
58<li>
59<a href="#bb">Before Beginning</a> </li>
60<li>
61<a href="#intro">Introduction</a> </li>
62<li>
63<a href="#hiw">Process</a> <ul style="list-style:none;">
64<li>
65<a href="#mc">Main Components</a> </li>
66<ul>
67<li>
68<a href="#ndk">ndk-build</a> </li>
69<li>
70<a href="#jav">Java</a> </li>
71<li>
72<a href="#nshl">Native shared libraries</a> </li>
73<li>
74<a href="#nstl">Native static libraries</a> </li>
75<li>
76<a href="#jni">Java Native Interface (JNI)</a> </li>
77<li>
78<a href="#abi">Application Binary Interface (ABI)</a> </li>
79<li>
80<a href="#man">Manifest</a> </li>
81<li>
82<a href="#and">Android.mk</a> </li>
83<li>
84<a href="#app">Application.mk</a> </li>
85</ul>
86<a href="#fl">Flow</a> </li>
87</ul>
88</li>
89<li>
90<p class="startli"><a href="#naa">Native Activities and Applications</a></p>
91<p><a class="anchor" id="bb"></a> </p>
92<h2>Before Beginning</h2>
93<p></p>
94<p>This guide assumes that you are:</p>
95<ul>
96<li>Already familiar with concepts inherent in native programming, and in <a
97href="http://developer.android.com">development of Android apps</a>.</li>
98<li>Working in <a href="http://developer.android.com/sdk/index.html">Eclipse,
99and using the Android Development Tools ("ADT")</a>, except where otherwise
100noted.</li>
101</ul>
102<p><a class="anchor" id="intro"></a> </p>
103<h2>Introduction</h2>
104<p></p>
105<p>This section provides a high-level explanation of how the NDK works. The
106Android NDK is a set of tools allowing you to embed C or C++ (“native
107code”) into your Android apps. The ability to use native code in Android apps
108can be particularly useful to developers who wish to do one or more of the
109following:</p>
110<ul>
111<li>Port their apps between platforms.</li>
112<li>Use existing libraries created by other developers (or provide their own
113libraries to others).</li>
114<li>Increase performance in certain cases, particularly computationally
115intensive ones like games.</li>
116</ul>
117<p><br/>
118 </p>
119<b>Important Notes:</b> The Android NDK can only be used to target Android
120system images running Android 1.5 (API Level 3) or later.
121
122The NDK only provides system headers for a very limited set of native
123APIs and libraries supported by the Android platform. While a typical
124Android system image includes many native shared libraries, you should
125consider them an implementation detail that might change drastically
126between platform releases. If the NDK headers do not explicitly support an
127Android system library, then apps should not depend on its being
128available; otherwise, the next over-the-air (“OTA”) system update may
129break them.
130
131<p><a class="anchor" id="hiw"></a> </p>
132<h2>Process</h2>
133<p>This section introduces the main components used in building a native
134application for Android, and goes on to describe the flow of building and
135packaging.</p>
136<p><a class="anchor" id="mc"></a> </p>
137<h3>Main components</h3>
138<p></p>
139<p>You should have an understanding of the following components, as you build
140your app:</p>
141<p><a class="anchor" id="ndk"></a></p>
142<ul>
143<li>ndk-build: The ndk-build script launches the build scripts at the heart of
144the NDK. These scripts:<ul>
145<li>Automatically probe your development system and app project file to
146determine what to build.</li>
147<li>Generate binaries.</li>
148<li>Copy the binaries to your app's project path.</li>
149</ul>
150</li>
151</ul>
152<p>For more information, see the <a
153href="./md_3__key__topics__building__chapter_1-section_8_ndk-build.html">ndk-build</a> section of this guide.</p>
154<p><a class="anchor" id="jav"></a></p>
155<ul>
156<li>Java: From your Java source, the Android build process generates
157<code>.dex</code> ("Dalvik EXecutable") files, which are what the Android OS
158runs in the Dalvik Virtual Machine (“DVM”). Even if your app contains no
159Java source code at all, the build process still generates a <code>.dex</code>
160executable file within which the native component runs.</li>
161</ul>
162<p>When developing Java components, use the <code>native</code> keyword to
163indicate methods implemented as native code. For example, the following
164function declaration tells the compiler that the implementation is in a native
165library:</p>
166<pre class="fragment">public native int add(int  x, int  y);
167</pre><p><a class="anchor" id="nshl"></a></p>
168<ul>
169<li>Native shared libraries: The NDK builds these libraries, or
170<code>.so</code> files, from your native source code.</li>
171</ul>
172<br>
173<b>Note:</b> If two libraries implement the same method, with the same name, a
174link
175error occurs.
176</pre><p><a class="anchor" id="nstl"></a></p>
177<ul>
178<li>Native static libraries: The NDK can also build static libraries, or
179<code>.a</code> files, which you can link against other libraries.</li>
180</ul>
181<p><a class="anchor" id="jni"></a></p>
182<ul>
183<li>Java Native Interface ("JNI"): The JNI is the interface via which the Java
184and C++ components talk to one another. This guide assumes knowledge of the
185JNI; for information about it, consult the <a
186href="http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html
187">Java Native Interface Specification</a>.</li>
188</ul>
189<p><a class="anchor" id="abi"></a></p>
190<ul>
191<li>Application Binary Interface ("ABI"): The ABI defines exactly how your
192app's machine code is expected to interact with the system at runtime. The NDK
193builds <code>.so</code> files against these definitions. Different ABIs
194correspond to different architectures: The NDK includes ABI support for ARMEABI
195(default), MIPS, and x86. For more information, see <a
196href="./md_3__key__topics__c_p_u__support__chapter_1-section_8__a_b_is.html">Supported ABIs</a>.</li>
197</ul>
198<p><a class="anchor" id="man"></a></p>
199<ul>
200<li>Manifest: If you are writing an app with no Java component to it, you must
201declare the NativeActivity class in the <a
202href="http://developer.android.com/guide/topics/manifest/manifest-intro.html">manifest</a>. The <a href=#naa>Native Activity</a> section provides more detail
203on how to do this, under “Using the native-activity.h interface.”</li>
204</ul>
205<br>
206<b>Note:</b> The following two items only apply in cases in which you are using
207the
208toolchains provided with the Android NDK as standalone compilers.
209<p><a class="anchor" id="and"></a></p>
210<ul>
211<li><code>Android.mk</code>: You must create an <code>Android.mk</code>
212configuration file inside your <code>jni</code> folder. The ndk-build script
213looks at this file, which defines the module and its name, the source files to
214be compiled, build flags and libraries to link. For more information, see the
215<a
216href="./md_3__key__topics__building__chapter_1-section_8__android_8mk.html">Android.mk</a> section of this document.</li>
217</ul>
218<p><a class="anchor" id="app"></a></p>
219<ul>
220<li><code>Application.mk</code>: You may optionally create an Application.mk
221file. This file enumerates and describes the modules that your app requires.
222This information includes:<ul>
223<li>ABIs used to compile for specific platforms.</li>
224<li>Toolchains.</li>
225<li>Standard libraries to include (static and dynamic STLport or default
226system).</li>
227</ul>
228</li>
229</ul>
230<p>For more information, see the <a
231href="./md_3__key__topics__building__a_p_p_l_i_c_a_t_i_o_n-_m_k.html">Applicatio
232n.mk</a> section.</p>
233<p><a class="anchor" id="fl"></a> </p>
234<h3>Flow</h3>
235<p></p>
236<p>The general flow for developing a native app for Android is as follows:</p>
237<ol type="1">
238<li>Design your app, deciding which parts to implement in Java, and which parts
239to implement as native code.
240
241<br>
242<b>Note:</b> While it is possible to completely avoid Java, you are likely to
243find
244the Android Java framework useful for tasks including controlling the
245display and UI.</li>
246<li>Create an Android app Project in Eclipse as you would for any other Android
247project.</li>
248<li>If you are writing a native-only app, declare the NativeActivity class in
249AndroidManifest.xml. You can do so from the Eclipse/ADT Android Manifest
250Editor, or by hand-editing the file. For more information, see the <a
251href=#naa>Native Activity</a> section.</li>
252<li>Create an Android.mk file describing the native library, including name,
253flags, linked libraries and source files to be compiled in the ‘JNI’
254directory.</li>
255<li>OPTIONAL: Create an <code>Application.mk</code> file configuring the target
256ABIs, toolchain, release/debug mode, and STL. For any of these that you do not
257specify, the following default values are used, respectively: <ul>
258<li>
259ABI: armeabi<br/>
260 </li>
261<li>
262Toolchain: GCC 4.8<br/>
263 </li>
264<li>
265Mode: Release<br/>
266 </li>
267<li>
268STL: system </li>
269</ul>
270</li>
271<li>Place your native source under the project's <code>jni</code>
272directory.</li>
273<li>Use ndk-build to compile the native (<code>.so</code>, <code>.a</code>)
274libraries.</li>
275<li>Build the Java component, producing the executable <code>.dex</code>
276file.</li>
277<li>Package everything into an APK file, containing <code>.so</code>,
278<code>.dex</code>, and other files needed for your app to run.</li>
279</ol>
280<p>Note that Eclipse can perform steps 7. through 9. in a single operation.</p>
281<p><a class="anchor" id="naa"></a> </p>
282<h2>Native Activities and Applications</h2>
283<p></p>
284<p>The Android SDK provides a helper class, <code>NativeActivity</code>, that
285allows you to write a completely native activity. <code>NativeActivity</code>
286handles the communication between the Android framework and your native code,
287so you do not have to subclass it or call its methods. All you need to do is
288declare your application to be native in your <code>AndroidManifest.xml</code>
289file, and begin creating your native application.</p>
290<p>An Android application using <code>NativeActivity</code> still runs in its
291own virtual machine, sandboxed from other applications. You can therefore still
292access Android framework APIs through the JNI. In certain cases,
293however&ndash;such as for sensors, input events, and assets&ndash;the NDK
294provides native interfaces that you can use instead of having to call across
295the JNI. For more information about such support, see
296<code>STABLE-APIS.HTML</code>.</p>
297<p>Regardless of whether or not you are developing a native activity, we
298recommend that you create your projects with the usual Android build tools.
299Doing so helps ensure building and packaging of Android applications with the
300correct structure.</p>
301<p>The Android NDK provides you with two choices to implement your native
302activity:</p>
303<ul>
304<li>The <code>native_activity.h</code> header defines the native version of the
305NativeActivity class. It contains the callback interface and data structures
306that you need to create your native activity. Because the main thread of your
307application handles the callbacks, your callback implementations must not be
308blocking. If they block, you might receive ANR (Application Not Responding)
309errors because your main thread is unresponsive until the callback returns.</li>
310<li>The android_native_app_glue.h file defines a static helper library built on
311top of the <code>native_activity.h</code> interface. It spawns another thread,
312which handles things such as callbacks or input events in an event loop. Moving
313these events to a separate thread prevents any callbacks from blocking your
314main thread.</li>
315</ul>
316<p>The
317<code>&lt;ndk_root&gt;/sources/android/native_app_glue/android_native_app_glue.c
318</code> source is also available, allowing you to modify the implementation.</p>
319<p>For more information on how to use this static library, examine the
320native-activity sample application, and its documentation. Further reading is
321also available in the comments in the
322<code>&lt;ndk_root&gt;/sources/android/native_app_glue/android_native_app_glue.h
323</code> file.</p>
324<p><a class="anchor" id="na"></a> </p>
325<h3>Using the <code>native-activity.h</code> interface</h3>
326<p></p>
327<p>To implement a native activity with the <code>native-activity.h</code>
328interface:</p>
329<ol type="1">
330<li>Create a <code>jni/</code> directory in your project's root directory. This
331directory stores all of your native code.</li>
332<li>Declare your native activity in the <code>AndroidManifest.xml</code>
333file.</li>
334
335<p>Because your application has no Java code, set <code>android:hasCode</code>
336to <code>false</code>.</p>
337<pre class="fragment">        &lt;application android:label="@string/app_name"
338android:hasCode="false"&gt;
339</pre><p>The <code>android:name</code> attribute of the activity tag must be
340set to <code>android.app.NativeActivity</code>.</p>
341<pre class="fragment">          &lt;activity
342android:name="android.app.NativeActivity"
343            android:label="@string/app_name"
344
345</pre>
346<b>Note:</b> You can subclass <code>NativeActivity</code>. If you do, use the
347name of the subclass instead of <code>NativeActivity</code>.
348<p>The <code>android:value</code> attribute of the <code>meta-data</code> tag
349specifies the name of the shared library containing the entry point to the
350application (e.g., C/C++ <code>main</code>), omitting the <code>lib</code>
351prefix and <code>.so</code> suffix from the library name.</p>
352<pre class="fragment">          &lt;meta-data
353android:name="android.app.lib_name"
354            android:value="native-activity" /&gt;
355            &lt;intent-filter&gt;
356              &lt;action android:name="android.intent.action.MAIN" /&gt;
357              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
358            &lt;/intent-filter&gt;
359          &lt;/activity&gt;
360        &lt;/application&gt;
361      &lt;/manifest&gt;
362</pre>
363<li>Create a file for your native activity, and implement the
364<code>ANativeActivity_onCreate()</code> function, which the app calls when the
365native activity starts. This function, analogous to <code>main</code> in C/C++,
366receives a pointer to an <code>ANativeActivity</code> structure, which contains
367function pointers to the various callback implementations that you need to
368write. Set the applicable callback function pointers in
369<code>ANativeActivity-&gt;;callbacks</code> to the implementations of your
370callbacks.</li>
371<li>Set the <code>ANativeActivity-&gt;;instance</code> field to the address of
372any instance of specific data that you want to use.</li>
373<li>Implement anything else that you want your activity to do upon
374starting.</li>
375<li>Implement the rest of the callbacks that you set in
376<code>ANativeActivity-&gt;;callbacks</code>. For more information on when the
377callbacks are called, see the <a
378href="http://developer.android.com/training/basics/activity-lifecycle/index.html
379">SDK documentation for Activity Lifecycles</a>.</li>
380<li>Develop the rest of your application.</li>
381<li>Create an <code>Android.mk file</code> in the <code>jni/</code> directory
382of your project to describe your native module to the build system. For more
383information, see the <a
384href="./md_3__key__topics__building__chapter_1-section_8__android_8mk.html">Android.mk section.</a></li>
385<li>Once you have an <code>Android.mk</code> file, compile your native code
386using the <code>ndk-build</code> command.</li>
387
388<pre class="fragment">$ cd &lt;path&gt;/&lt;to&gt;/&lt;project&gt;
389$ &lt;ndk&gt;/ndk-build
390</pre>
391<li>Build and install your Android project as usual, using Ant or Eclipse. If
392your native code is in the <code>jni/</code> directory, the build script
393automatically packages the <code>.so</code> file(s) built from it into the
394APK.</li>
395</ol>
396<p class="endli">You can find further information on using
397<code>native-activity.h</code> in the comments in the
398<code>&lt;ndk_root&gt;/platforms/android-9/arch-arm/usr/include/android/native_a
399ctivity.h</code> file. </p>
400</li>
401</ul>
402</div></div><!-- contents -->
403</div><!-- doc-content -->
404<!-- start footer part -->
405<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
406  <ul>
407    <li class="footer">Generated on Sun Jun 22 2014 15:25:44 for NDK
408Programmer&#39;s Guide by
409    <a href="http://www.doxygen.org/index.html">
410    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.5 </li>
411  </ul>
412</div>
413</body>
414</html>
415