• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1ndk=true
2ndk.version=1.5
3ndk.rel.id=1
4ndk.date=June 2009
5
6ndk.win_download=android-ndk-1.5_r1-windows.zip
7ndk.win_bytes=22500667
8ndk.win_checksum=e5c53915903d8b81f3e2ea422e2e2717
9
10ndk.mac_download=android-ndk-1.5_r1-darwin-x86.zip
11ndk.mac_bytes=17215303
12ndk.mac_checksum=1931f0e182798a4c98924fd87380b5b8
13
14ndk.linux_download=android-ndk-1.5_r1-linux-x86.zip
15ndk.linux_bytes=16025885
16ndk.linux_checksum=80a4e14704ca84c21bf1824cb25fbd8b
17
18page.title=Android 1.5 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),
46the 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>A Minimal set of headers for C++ support</li>
78</ul>
79
80<p>The NDK also provides a build system that lets you work efficiently with your
81sources, without having to handle the toolchain/platform/CPU/ABI details. You
82create very short build files to describe which sources to compile and which
83Android application will use them &mdash; the build system compiles the sources
84and places the shared libraries directly in your application project. </p>
85
86<p class="caution"><strong>Important:</strong> With the exception of the
87libraries listed above, native system libraries in the Android 1.5 platform are
88<em>not</em> stable and may change in future platform versions.
89Your applications should <em>only</em> make use of the stable native system
90libraries provided in this NDK. </p>
91
92<h4>Documentation</h4>
93
94<p>The NDK package includes a set of documentation that describes the
95capabilities of the NDK and how to use it to create shared libraries for your
96Android applications. In this release, the documentation is provided only in the
97downloadable NDK package. You can find the documentation in the
98<code>&lt;ndk&gt;/docs/</code> directory. Included are these files:</p>
99
100<ul>
101<li>INSTALL.TXT &mdash; describes how to install the NDK and configure it for
102your host system</li>
103<li>OVERVIEW.TXT &mdash; provides an overview of the NDK capabilities and
104usage</li>
105<li>ANDROID-MK.TXT &mdash; describes the use of the Android.mk file, which
106defines the native sources you want to compile</li>
107<li>APPLICATION-MK.TXT &mdash; describes the use of the Application.mk file,
108which describes the native sources required by your Android application</li>
109<li>HOWTO.TXT &mdash; information about common tasks associated with NDK
110development.</li>
111<li>SYSTEM-ISSUES.TXT &mdash; known issues in the Android system images
112that you should be aware of, if you are developing using the NDK. </li>
113<li>STABLE-APIS.TXT &mdash; a complete list of the stable APIs exposed
114by headers in the NDK.</li>
115</ul>
116
117<p>Additionally, the package includes detailed information about the "bionic"
118C library provided with the Android platform that you should be aware of, if you
119are developing using the NDK. You can find the documentation in the
120<code>&lt;ndk&gt;/docs/system/libc/</code> directory:</p>
121
122<ul>
123<li>OVERVIEW.TXT &mdash; provides an overview of the "bionic" C library and the
124features it offers.</li>
125</ul>
126
127<h4>Sample applications</h4>
128
129<p>The NDK includes two sample Android applications that illustrate how to use
130native code in your Android applications:</p>
131
132<ul>
133<li><code>hello-jni</code> &mdash; A simple application that loads a string from
134a native method implemented in a shared library and then displays it in the
135application UI. </li>
136<li><code>two-libs</code> &mdash; A simple application that loads a shared
137library dynamically and calls a native method provided by the library. In this
138case, the method is implemented in a static library that is imported by the
139shared library. </li>
140</ul>
141
142<p>For more information, see <a href="#samples">Using the Sample
143Applications</a>.</p>
144
145<h2 id="requirements">System and Software Requirements</h2>
146
147<p>The sections below describe the system and software requirements for using
148the Android NDK, as well as platform compatibility considerations that affect
149appplications using libraries produced with the NDK. </p>
150
151<h4>The Android SDK</h4>
152<ul>
153  <li>A complete Android SDK installation (including all dependencies) is
154required.</li>
155  <li>Android 1.5 SDK or later version is required.</li>
156</ul>
157
158<h4>Supported operating systems</h4>
159<ul>
160  <li>Windows XP (32-bit) or Vista (32- or 64-bit)</li>
161  <li>Mac OS X 10.4.8 or later (x86 only)</li>
162  <li>Linux (32- or 64-bit, tested on Linux Ubuntu Dapper Drake)</li>
163</ul>
164
165<h4>Required development tools</h4>
166<ul>
167  <li>For all development platforms, GNU Make 3.81 or later is required. Earlier
168versions of GNU Make might work but have not been tested.</li>
169  <li>For Windows, a recent release of <a
170href="http://www.cygwin.com">Cygwin</a>, including both the gmake and gcc
171packages, is required. </li>
172</ul>
173
174<h4>Android platform compatibility</h4>
175<ul>
176  <li>The native libraries created by the Android NDK can only be used on
177devices running the Android 1.5 platform version or later. This is due to
178toolchain and ABI related changes that make the native libraries incompatible
179with 1.0 and 1.1 system images.</li>
180  <li>For this reason, you should use native libraries produced with the NDK in
181applications that are deployable to devices running the Android 1.5 platform
182version or later. To ensure compatibility, an application using a native library
183produced with the NDK must declare a <code>&lt;uses-library&gt;</code> element
184in its manifest file, with the attribute
185<code>android:minSdkVersion="3"</code>.</li>
186</ul>
187
188<h2 id="installing">Installing the NDK</h2>
189
190<p>Installing the NDK on your development computer is straightforward and
191involves extracting the NDK from its download package and running a host-setup
192script. </p>
193
194<p>Before you get started make sure that you have downloaded the latest <a
195href="{@docRoot}sdk/index.html">Android SDK</a> and upgraded your applications
196and environment as needed. The NDK will not work with older versions of the
197Android SDK. Also, take a moment to review the <a href="#requirements">System
198and Software Requirements</a> for the NDK, if you haven't already. </p>
199
200<p>To install the NDK, follow these steps:</p>
201
202<ol>
203<li>From the table at the top of this page, select the NDK package that is
204appropriate for your development computer and download the package.</li>
205<li>Uncompress the NDK download package using tools available on your computer.
206When uncompressed, the NDK files are contained in a directory called
207<code>android-ndk-&lt;version&gt;</code>. You can rename the NDK directory if
208necessary and you can move it to any location on your computer. This
209documentation refers to the NDK directory as <code>&lt;ndk&gt;</code>.  </li>
210<li>Open a terminal, change to the NDK directory, and run the host-setup script.
211The script sets up your environment and generates a host configuration file used
212later, when building your shared libraries. The path to the host-setup script
213is:
214
215<p><code>&lt;ndk&gt;/build/host-setup.sh</code></p>
216
217<p>If the script completes successfully, it prints a "Host setup complete."
218message. If it fails, it prints instructions that you can follow to correct any
219problems. </p>
220</li>
221</ol>
222
223<p>Once you have run the host-setup script, you are ready start working with the
224NDK. </p>
225
226<h2 id="gettingstarted">Getting Started with the NDK</h2>
227
228<p>Once you've installed the NDK successfully, take a few minutes to read the
229documentation included in the NDK. You can find the documentation in the
230<code>&lt;ndk&gt;/docs/</code> directory. In particular, please read the
231OVERVIEW.TXT document completely, so that you understand the intent of the NDK
232and how to use it.</p>
233
234<p>Here's the general outline of how you work with the NDK tools:</p>
235
236<ol>
237<li>Place your native sources under
238<code>&lt;ndk&gt;/sources/&lt;my_src&gt;/...</code>. If you want, you can place
239a symlink to your sources, rather than the sources themselves. The sources you
240reference here are not strictly associated with a specific shared library or
241Android application. Instead, they are accessible to any build configuration and
242can be used to produce any number of shared libraries that can be used by any
243Android application.</li>
244<li>Create <code>&lt;ndk&gt;/sources/&lt;my_src&gt;/Android.mk</code> to
245describe your native sources to the NDK build system</li>
246<li>Create <code>&lt;ndk&gt;/apps/&lt;my_app&gt;/Application.mk</code> to
247describe your Android application and native sources it needs to the NDK build
248system. This file sets up the link between an Android SDK application project
249and any number of shared libraries defined in the
250<code>&lt;ndk&gt;/sources/</code> folder and it specifies the path to the
251application project that will receive the shared library built from the
252sources.</li>
253<li>Build your native code by running this make command from the top-level NDK
254directory:
255
256<p><code>$ make APP=&lt;my_app&gt;</code></p>
257
258<p>The build tools copy the stripped, shared libraries needed by your
259application to the proper location in the application's project directory.</p>
260</li>
261
262<li>Finally, compile your application using the SDK tools in the usual way. The
263SDK build tools will package the shared libraries in the application's
264deployable .apk file. </p></li>
265
266</ol>
267
268<p>For complete information on all of the steps listed above, please see the
269documentation included with the NDK package. </p>
270
271
272<h2 id="samples">Using the Sample Applications</h2>
273
274<p>The NDK includes two sample applications that illustrate how to use native
275code in your Android applications:</p>
276
277<ul>
278<li><code>hello-jni</code> &mdash; A simple application that loads a string from
279a native method implemented in a shared library and then displays it in the
280application UI. </li>
281<li><code>two-libs</code> &mdash; A simple application that loads a shared
282library dynamically and calls a native method provided by the library. In this
283case, the method is implemented in a static library imported by the shared
284library. </li>
285</ul>
286
287<p>For each sample, the NDK includes an Android application project, as well as
288the corresponding C source code and the necessary Android.mk and Application.mk
289files. The application projects are provided in
290<code>&lt;ndk&gt;/apps/&lt;app_name&gt;/project/</code> and the C source for
291each application is provided in
292<code>&lt;ndk&gt;/sources/samples/&lt;library&gt;/</code>.</p>
293
294<p>Once you have installed the NDK, you can build the shared libraries from the
295NDK by using these commands from the root of the NDK directory:</p>
296<ul>
297<li><code>$ make APP=hello-jni</code> &mdash; compiles
298<code>&lt;ndk&gt;/sources/samples/hello-jni/hello-jni.c</code> and outputs a
299shared library to
300<code>&lt;ndk&gt;/apps/hello-jni/project/libs/armeabi/libhello-jni.so</code>.
301</li>
302<li><code>$ make APP=two-libs</code> &mdash; compiles
303<code>&lt;ndk&gt;/sources/samples/two-libs/second.c</code> and
304<code>first.c</code> and outputs a shared library to
305<code>&lt;ndk&gt;/apps/two-libs/project/libs/armeabi/libtwolib-second.so</code>.
306</li>
307</ul>
308
309<p>Next, build the sample Android applications that use the shared
310libraries:</p>
311
312<ul>
313<li>If you are developing in Eclipse with ADT, use the New Project Wizard to
314create a new Android project for each sample, using the "Import from Existing
315Source" option and importing the source from
316<code>&lt;ndk&gt;/apps/&lt;app_name&gt;/project/</code>. Then, set up an AVD, if
317necessary, and build/run the application in the emulator. For more information
318about creating a new Android project in Eclipse, see <a
319href="{@docRoot}guide/developing/eclipse-adt.html">Developing in
320Eclipse</a>.</li>
321<li>If you are developing with Ant, use the <code>android</code> tool to create
322the build file for each of the sample projects at
323<code>&lt;ndk&gt;/apps/&lt;app_name&gt;/project/</code>. Then set up an AVD, if
324necessary, build your project in the usual way, and run it in the emulator.
325For more information, see <a
326href="{@docRoot}guide/developing/other-ide.html">Developing in Other
327IDEs</a>.</li>
328</ul>
329
330<h2>Discussion Forum and Mailing List</h2>
331
332<p>If you have questions about the NDK or would like to read or contribute to
333discussions about it, please visit the <a
334href="http://groups.google.com/group/android-ndk">android-ndk</a> group and
335mailing list.</p>
336
337
338