• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1ndk=true
2ndk.version=1.6
3ndk.rel.id=1
4ndk.date=September 2009
5
6ndk.win_download=android-ndk-1.6_r1-windows.zip
7ndk.win_bytes=25896444
8ndk.win_checksum=a213c9592f24c61620755c6c43f85210
9
10ndk.mac_download=android-ndk-1.6_r1-darwin-x86.zip
11ndk.mac_bytes=20611080
12ndk.mac_checksum=93c5b8b92a1f7b1cdadf267cab8ec403
13
14ndk.linux_download=android-ndk-1.6_r1-linux-x86.zip
15ndk.linux_bytes=19421662
16ndk.linux_checksum=464d3dc02739b82917e6f0591e17edd1
17
18page.title=Android 1.6 NDK, Release 1
19@jd:body
20
21<h2 id="overview">What is the Android NDK?</h2>
22
23<p>The Android NDK provides tools that allow Android application developers
24to embed components that make use of native code in their Android applications.
25</p>
26
27<p>Android applications run in the Dalvik virtual machine. The NDK allows
28developers to implement parts of their applications using native-code languages
29such as C and C++. This can provide benefits to certain classes of applications,
30in the form of reuse of existing code and in some cases increased speed.</p>
31
32<p>The NDK provides:</p>
33
34<ul>
35<li>A set of tools and build files used to generate native code libraries from C
36and C++ sources</li>
37<li>A way to embed the corresponding native libraries into application package
38files (.apks) that can be deployed on Android devices</li>
39<li>A set of native system headers and libraries that will be supported in all
40future versions of the Android platform, starting from Android 1.5 </li>
41<li>Documentation, samples, and tutorials</li>
42</ul>
43
44<p>This release of the NDK supports the ARMv5TE machine instruction set
45and provides stable headers for libc (the C library), libm (the Math library),
46OpenGL ES 1.1 (3D graphics library), the JNI interface, and other libraries.</p>
47
48<p>The NDK will not benefit most applications. As a developer, you will need
49to balance its benefits against its drawbacks; notably, using native code does
50not result in an automatic performance increase, but does always increase
51application complexity. Typical good candidates for the NDK are self-contained,
52CPU-intensive operations that don't allocate much memory, such as signal processing,
53physics simulation, and so on. Simply re-coding a method to run in C usually does
54not result in a large performance increase. The NDK can, however, can be
55an effective way to reuse a large corpus of existing C/C++ code.</p>
56
57<p>Please note that the NDK <em>does not</em> enable you to develop native-only
58applications. Android's primary runtime remains the Dalvik virtual machine.</p>
59
60<h2 id="contents">Contents of the NDK</h2>
61
62<h4>Development tools</h4>
63
64<p>The NDK includes a set of cross-toolchains (compilers, linkers, etc..) that
65can generate native ARM binaries on Linux, OS X, and Windows (with Cygwin)
66platforms.</p>
67
68<p>It provides a set of system headers for stable native APIs that are
69guaranteed to be supported in all later releases of the platform:</p>
70
71<ul>
72<li>libc (C library) headers</li>
73<li>libm (math library) headers</li>
74<li>JNI interface headers</li>
75<li>libz (Zlib compression) headers</li>
76<li>liblog (Android logging) header</li>
77<li>OpenGL ES 1.1 (3D graphics library) headers</li>
78<li>A Minimal set of headers for C++ support</li>
79</ul>
80
81<p>The NDK also provides a build system that lets you work efficiently with your
82sources, without having to handle the toolchain/platform/CPU/ABI details. You
83create very short build files to describe which sources to compile and which
84Android application will use them &mdash; the build system compiles the sources
85and places the shared libraries directly in your application project. </p>
86
87<p class="caution"><strong>Important:</strong> With the exception of the
88libraries listed above, native system libraries in the Android platform are
89<em>not</em> stable and may change in future platform versions.
90Your applications should <em>only</em> make use of the stable native system
91libraries provided in this NDK. </p>
92
93<h4>Documentation</h4>
94
95<p>The NDK package includes a set of documentation that describes the
96capabilities of the NDK and how to use it to create shared libraries for your
97Android applications. In this release, the documentation is provided only in the
98downloadable NDK package. You can find the documentation in the
99<code>&lt;ndk&gt;/docs/</code> directory. Included are these files:</p>
100
101<ul>
102<li>INSTALL.TXT &mdash; describes how to install the NDK and configure it for
103your host system</li>
104<li>OVERVIEW.TXT &mdash; provides an overview of the NDK capabilities and
105usage</li>
106<li>ANDROID-MK.TXT &mdash; describes the use of the Android.mk file, which
107defines the native sources you want to compile</li>
108<li>APPLICATION-MK.TXT &mdash; describes the use of the Application.mk file,
109which describes the native sources required by your Android application</li>
110<li>HOWTO.TXT &mdash; information about common tasks associated with NDK
111development.</li>
112<li>SYSTEM-ISSUES.TXT &mdash; known issues in the Android system images
113that you should be aware of, if you are developing using the NDK. </li>
114<li>STABLE-APIS.TXT &mdash; a complete list of the stable APIs exposed
115by headers in the NDK.</li>
116<li>CHANGES.TXT &mdash; a complete list of changes to the NDK across all
117releases.</li>
118</ul>
119
120<p>Additionally, the package includes detailed information about the "bionic"
121C library provided with the Android platform that you should be aware of, if you
122are developing using the NDK. You can find the documentation in the
123<code>&lt;ndk&gt;/docs/system/libc/</code> directory:</p>
124
125<ul>
126<li>OVERVIEW.TXT &mdash; provides an overview of the "bionic" C library and the
127features it offers.</li>
128</ul>
129
130<h4>Sample applications</h4>
131
132<p>The NDK includes three sample Android applications that illustrate how to use
133native code in your Android applications:</p>
134
135<ul>
136<li><code>hello-jni</code> &mdash; A simple application that loads a string from
137a native method implemented in a shared library and then displays it in the
138application UI. </li>
139<li><code>two-libs</code> &mdash; A simple application that loads a shared
140library dynamically and calls a native method provided by the library. In this
141case, the method is implemented in a static library that is imported by the
142shared library. </li>
143<li><code>san-angeles</code> &mdash; A simple application that renders 3D
144graphics through the native OpenGL ES APIs, while managing activity lifecycle
145with a {@link android.opengl.GLSurfaceView GLSurfaceView} object. </li>
146</ul>
147
148<p>For more information, see <a href="#samples">Using the Sample
149Applications</a>.</p>
150
151<h2 id="requirements">System and Software Requirements</h2>
152
153<p>The sections below describe the system and software requirements for using
154the Android NDK, as well as platform compatibility considerations that affect
155appplications using libraries produced with the NDK. </p>
156
157<h4>The Android SDK</h4>
158<ul>
159  <li>A complete Android SDK installation (including all dependencies) is
160required.</li>
161  <li>Android 1.5 SDK or later version is required.</li>
162</ul>
163
164<h4>Supported operating systems</h4>
165<ul>
166  <li>Windows XP (32-bit) or Vista (32- or 64-bit)</li>
167  <li>Mac OS X 10.4.8 or later (x86 only)</li>
168  <li>Linux (32- or 64-bit, tested on Linux Ubuntu Dapper Drake)</li>
169</ul>
170
171<h4>Required development tools</h4>
172<ul>
173  <li>For all development platforms, GNU Make 3.81 or later is required. Earlier
174versions of GNU Make might work but have not been tested.</li>
175  <li>For Windows, a recent release of <a
176href="http://www.cygwin.com">Cygwin</a> is required. </li>
177</ul>
178
179<h4>Android platform compatibility</h4>
180<ul>
181  <li>The native libraries created by the Android NDK can only be used on
182devices running the Android 1.5 platform version or later. This is due to
183toolchain and ABI related changes that make the native libraries incompatible
184with 1.0 and 1.1 system images.</li>
185  <li>For this reason, you should use native libraries produced with the NDK in
186applications that are deployable to devices running the Android 1.5 platform
187version or later.
188  <li>To ensure compatibility, an application using a native library
189produced with the NDK <em>must</em> declare a <code>&lt;uses-library&gt;</code>
190element in its manifest file, with an <code>android:minSdkVersion</code> attribute
191value of "3" or higher. For example:
192
193<pre>&lt;manifest&gt;
194  ...
195  &lt;uses-sdk android:minSdkVersion="3" /&gt;
196  ...
197&lt;/manifest&gt;</pre>
198
199<p>Additionally, if you use this NDK to create a native library that uses the
200OpenGL ES APIs, the application containing the library can be deployed only to
201devices running Android 1.6 or later. To ensure compatibility in this case,
202the application <em>must</em> declare an <code>android:minSdkVersion</code>
203attribute value of "4" or higher. </p>
204
205<p>For more information about API Level and its relationship to Android
206platform versions, see <a href="{@docRoot}guide/appendix/api-levels.html">
207Android API Levels</a>.</p></li>
208</ul>
209
210<h2 id="installing">Installing the NDK</h2>
211
212<p>Installing the NDK on your development computer is straightforward and
213involves extracting the NDK from its download package and running a host-setup
214script. </p>
215
216<p>Before you get started make sure that you have downloaded the latest <a
217href="{@docRoot}sdk/index.html">Android SDK</a> and upgraded your applications
218and environment as needed. The NDK will not work with older versions of the
219Android SDK. Also, take a moment to review the <a href="#requirements">System
220and Software Requirements</a> for the NDK, if you haven't already. </p>
221
222<p>To install the NDK, follow these steps:</p>
223
224<ol>
225<li>From the table at the top of this page, select the NDK package that is
226appropriate for your development computer and download the package.</li>
227<li>Uncompress the NDK download package using tools available on your computer.
228When uncompressed, the NDK files are contained in a directory called
229<code>android-ndk-&lt;version&gt;</code>. You can rename the NDK directory if
230necessary and you can move it to any location on your computer. This
231documentation refers to the NDK directory as <code>&lt;ndk&gt;</code>.  </li>
232<li>Open a terminal, change to the NDK directory, and run the host-setup script.
233The script sets up your environment and generates a host configuration file used
234later, when building your shared libraries. The path to the host-setup script
235is:
236
237<p><code>&lt;ndk&gt;/build/host-setup.sh</code></p>
238
239<p>If the script completes successfully, it prints a "Host setup complete."
240message. If it fails, it prints instructions that you can follow to correct any
241problems. </p>
242</li>
243</ol>
244
245<p>Once you have run the host-setup script, you are ready start working with the
246NDK. </p>
247
248<h2 id="gettingstarted">Getting Started with the NDK</h2>
249
250<p>Once you've installed the NDK successfully, take a few minutes to read the
251documentation included in the NDK. You can find the documentation in the
252<code>&lt;ndk&gt;/docs/</code> directory. In particular, please read the
253OVERVIEW.TXT document completely, so that you understand the intent of the NDK
254and how to use it.</p>
255
256<p>If you used a previous version of the NDK, take a moment to review the
257list of NDK changes in the CHANGES.TXT document. </p>
258
259<p>Here's the general outline of how you work with the NDK tools:</p>
260
261<ol>
262<li>Place your native sources under
263<code>&lt;project&gt;/jni/...</code></li>
264<li>Create <code>&lt;project&gt;/jni/Android.mk</code> to
265describe your native sources to the NDK build system</li>
266<li>Create <code>&lt;ndk&gt;/apps/&lt;my_app&gt;/Application.mk</code> to
267describe your Android application and native sources it needs to the NDK build
268system. This file sets up the link between an Android SDK application project
269and any number of shared libraries defined in the
270<code>&lt;project&gt;/jni/</code> folder and it specifies the path to the
271application project that will receive the shared library built from the
272sources.</li>
273<li>Build your native code by running this make command from the top-level NDK
274directory:
275
276<p><code>$ make APP=&lt;my_app&gt;</code></p>
277
278<p>The build tools copy the stripped, shared libraries needed by your
279application to the proper location in the application's project directory.</p>
280</li>
281
282<li>Finally, compile your application using the SDK tools in the usual way. The
283SDK build tools will package the shared libraries in the application's
284deployable .apk file. </p></li>
285
286</ol>
287
288<p>For complete information on all of the steps listed above, please see the
289documentation included with the NDK package. </p>
290
291
292<h2 id="samples">Using the Sample Applications</h2>
293
294<p>The NDK includes three sample applications that illustrate how to use native
295code in your Android applications:</p>
296
297<ul>
298<li><code>hello-jni</code> &mdash; A simple application that loads a string from
299a native method implemented in a shared library and then displays it in the
300application UI. </li>
301<li><code>two-libs</code> &mdash; A simple application that loads a shared
302library dynamically and calls a native method provided by the library. In this
303case, the method is implemented in a static library imported by the shared
304library. </li>
305<li><code>san-angeles</code> &mdash; A simple application that renders 3D
306graphics through the native OpenGL ES APIs, while managing activity lifecycle
307with a {@link android.opengl.GLSurfaceView GLSurfaceView} object. </li>
308</ul>
309
310<p>For each sample, the NDK includes an Android application project, as well as
311the corresponding C source code and the necessary Android.mk and Application.mk
312files. The application projects are provided in
313<code>&lt;ndk&gt;/apps/&lt;app_name&gt;/project/</code> and the C source for
314each application is provided in
315<code>&lt;ndk&gt;/apps/&lt;app_name&gt;/project/jni/</code>.</p>
316
317<p>Once you have installed the NDK, you can build the shared libraries from the
318NDK by using these commands from the root of the NDK directory:</p>
319<ul>
320<li><code>$ make APP=hello-jni</code> &mdash; compiles
321<code>&lt;ndk&gt;/apps/hello-jni/project/jni/hello-jni.c</code> and outputs a
322shared library to
323<code>&lt;ndk&gt;/apps/hello-jni/project/libs/armeabi/libhello-jni.so</code>.
324</li>
325<li><code>$ make APP=two-libs</code> &mdash; compiles
326<code>&lt;ndk&gt;/apps/two-libs/project/jni/second.c</code> and
327<code>first.c</code> and outputs a shared library to
328<code>&lt;ndk&gt;/apps/two-libs/project/libs/armeabi/libtwolib-second.so</code>.
329</li>
330</ul>
331
332<p>Next, build the sample Android applications that use the shared
333libraries:</p>
334
335<ul>
336<li>If you are developing in Eclipse with ADT, use the New Project Wizard to
337create a new Android project for each sample, using the "Import from Existing
338Source" option and importing the source from
339<code>&lt;ndk&gt;/apps/&lt;app_name&gt;/project/</code>. Then, set up an AVD, if
340necessary, and build/run the application in the emulator. For more information
341about creating a new Android project in Eclipse, see <a
342href="{@docRoot}guide/developing/eclipse-adt.html">Developing in
343Eclipse</a>.</li>
344<li>If you are developing with Ant, use the <code>android</code> tool to create
345the build file for each of the sample projects at
346<code>&lt;ndk&gt;/apps/&lt;app_name&gt;/project/</code>. Then set up an AVD, if
347necessary, build your project in the usual way, and run it in the emulator.
348For more information, see <a
349href="{@docRoot}guide/developing/other-ide.html">Developing in Other
350IDEs</a>.</li>
351</ul>
352
353<h2>Discussion Forum and Mailing List</h2>
354
355<p>If you have questions about the NDK or would like to read or contribute to
356discussions about it, please visit the <a
357href="http://groups.google.com/group/android-ndk">android-ndk</a> group and
358mailing list.</p>
359
360
361