Home | History | Annotate | Download | only in guides
      1 page.title=ndk-build
      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="#int">Internals</a></li>
     10          <li><a href="#ifc">Invoking from the Command Line</a></li>
     11          <li><a href="#ife">Invoking from Eclipse</a></li>
     12          <li><a href="#6432">64-Bit and 32-Bit Toolchains</a></li>
     13          <li><a href="#req">Requirements</a></li>
     14           </ol>
     15         </li>
     16       </ol>
     17     </div>
     18   </div>
     19 
     20 <p>The {@code ndk-build} file is a shell script introduced in Android NDK r4. Its purpose
     21 is to invoke the right NDK build script.
     22 
     23 <h2 id="int">Internals</h2>
     24 
     25 <p>Running the {@code ndk-build} script is equivalent to running the following command:</p>
     26 
     27 <pre class="no-pretty-print">
     28 $GNUMAKE -f &lt;ndk&gt;/build/core/build-local.mk
     29 &lt;parameters&gt;
     30 </pre>
     31 
     32 <p><code>$GNUMAKE</code> points to GNU Make 3.81 or later, and
     33 <code>&lt;ndk&gt;</code> points to your NDK installation directory. You can use
     34 this information to invoke ndk-build from other shell scripts, or even your own
     35 make files.</p>
     36 
     37 <h2 id="ifc">Invoking from the Command Line</h2>
     38 <p>The {@code ndk-build} file lives in the top level the NDK installation directory. To run it
     39 from the command line, invoke it while in or under your application project directory.
     40 For example: </p>
     41 
     42 <pre class="no-pretty-print">
     43 cd &lt;project&gt;
     44 $ &lt;ndk&gt;/ndk-build
     45 </pre>
     46 
     47 <p>In this example, <code>&lt;project&gt;</code> points to your
     48 projects root directory, and <code>&lt;ndk&gt;</code> is the directory where
     49 you installed the NDK.</p>
     50 
     51 <p><a class="anchor" id="options"></a> </p>
     52 <h3>Options</h3>
     53 <p>All parameters to ndk-build are passed directly to the underlying GNU {@code make}
     54 command that runs the NDK build scripts. Combine <code>ndk-build</code> and
     55 options in the form <code>ndk-build &lt;option&gt;</code>. For example: </p>
     56 
     57 <pre class="no-pretty-print">
     58 $ ndk-build clean
     59 </pre>
     60 
     61 <p>The following options are available:</p>
     62 <dl>
     63   <dt>{@code clean}</dt>
     64   <dd>Remove any previously generated binaries.</dd>
     65   <dt>{@code V=1}</dt>
     66   <dd>Launch build, and display build commands.<dd>
     67   <dt>{@code -B}</dt>
     68   <dd>Force a complete rebuild.</dd>
     69   <dt>{@code -B V=1}</dt>
     70   <dd>Force a complete rebuild, and display build commands.</dd>
     71   <dt>{@code NDK_LOG=1}</dd>
     72   <dd>Display internal NDK log messages (used for debugging the NDK itself).</dd>
     73   <dt>{@code NDK_DEBUG=1}</dt>
     74   <dd>Force a debuggable build (see <a href="#dvr">Table 1</a>).</dd>
     75   <dt>{@code NDK_DEBUG=0}</dt>
     76   <dd>Force a release build (see <a href="#dvr">Table 1</a>).</dd>
     77   <dt>{@code NDK_HOST_32BIT=1}</dt>
     78   <dd>Always use the toolchain in 32-bit mode (see <a href="#6432">64-bit and 32-bit
     79   Toolchains</a>).</dd>
     80   <dt>{@code NDK_APPLICATION_MK=&lt;file&gt;}</dt>
     81   <dd>Build, using a specific <code>Application.mk</code> file pointed to by the
     82   {@code NDK_APPLICATION_MK} variable.</dd>
     83   <dt>{@code -C &lt;project&gt;}</dt>
     84   <dd>Build the native code for the project path located at {@code &lt;project&gt;}. Useful if you
     85   don't want to {@code cd} to it in your terminal.</dd>
     86 </dl>
     87 
     88 <h2 id="ife">Invoking from Eclipse</h2>
     89 <p>To build from Eclipse, make sure that you have configured it as described in
     90 <a href="{@docRoot}ndk/guides/setup.html#configure">Setup</a>. If you
     91 wish to build using the default <code>ndk-build</code> command, with no
     92 options, you can just build your project just as you would any Android project.
     93 To get Eclipse to add any of the options described above, follow these steps:</p>
     94 <ol type="1">
     95 <li>In the <em>Project Explorer</em> pane, right-click your project name.</li>
     96 <li>Select <strong>Properties</strong>.</li>
     97 <li>Click <strong>C/C++ Build</strong>.</li>
     98 <li>Under the <em>Builder Settings</em> tab, uncheck <strong>Use default build command</strong>.</li>
     99 <li>In the <em>Build command</em> field, enter the entire build string as if you were typing it on
    100 the command line.</li>
    101 <li>Click <strong>OK</strong>.</li>
    102 </ol>
    103 Figure 1 shows an example of an entered string.<br>
    104 <br>
    105 <img src="./images/NDK_build_string.png"
    106     srcset="./images/NDK_build_string@2x.png 2x"
    107     alt="enter the build string next to 'Build command'"
    108     height="152" width="501">
    109 <p style="clear:both"><b>Figure 1.</b> Specifying a debug build from within
    110 Eclipse</p>
    111 <p><a class="anchor" id="dvr"></a> </p>
    112 <h3>Debuggable versus Release builds</h3>
    113 <p>Use the <code>NDK_DEBUG</code> option and, in certain cases,
    114 {@code AndroidManifest.xml} to specify debug or release build,
    115 optimization-related behavior, and inclusion of symbols. Table 1 shows the
    116 results of each possible combination of settings.</p>
    117 <p><em>Table 1.</em> Results of <code>NDK_DEBUG</code> (command line) and
    118 <code>android:debuggable</code> (manifest) combinations.</p>
    119 <table>
    120 <tr>
    121 <th></th><th>NDK_DEBUG=0 </th><th>NDK_DEBUG=1</th><th>NDK_DEBUG not specified
    122 </th></tr>
    123 <tr>
    124 <td>android:debuggble="true" </td><td>Debug; Symbols; Optimized*1
    125 </td><td>Debug; Symbols; Not optimized*2 </td><td>(same as NDK_DEBUG=1)
    126 </td></tr>
    127 <tr>
    128 <td>android:debuggable="false"</td><td>Release; Symbols; Optimized
    129 </td><td>Release; Symbols; Not optimized</td><td>Release; No symbols;
    130 Optimized*3 </td></tr>
    131 </table>
    132 *1: Useful for profiling.<br>
    133 *2: Default for running <a href="{@docRoot}ndk/guides/ndk-gdb.html">{@code ndk-gdb}</a>.<br>
    134 *3: Default mode.<br>
    135 <br>
    136 <p class="note"><strong>Note:</strong> {@code NDK_DEBUG=0} is the equivalent of
    137 {@code APP_OPTIM=release}, and complies with the GCC {@code -O2} option. {@code NDK_DEBUG=1} is the
    138 equivalent of {@code APP_OPTIM=debug} in {@code Application.mk}, and complies with the GCC
    139 {@code -O0} option. For more information about {@code APP_OPTIM}, see
    140 <a href="{@docRoot}ndk/guides/application_mk.html">Application.mk</a>.</p>
    141 <p>The syntax on the command line is, for example: </p>
    142 
    143 <pre class="no-pretty-print">
    144 $ ndk-build NDK_DEBUG=1
    145 </pre>
    146 
    147 <p>If you are using build tools from prior to SDK r8, you must also modify your
    148 {@code AndroidManifest.xml} file to specify debug mode. The syntax for doing so resembles the
    149 following:</p>
    150 
    151 <pre class="no-pretty-print">&lt;application android:label="@string/app_name"
    152 android:debuggable="true"&gt;
    153 </pre>
    154 
    155 From SDK r8 onward, you do not need to touch {@code AndroidManifest.xml}. Building a debug package
    156 (e.g. with ant debug or the corresponding option of the ADT plugin) causes the tool automatically to
    157 pick the native debug files generated with {@code NDK_DEBUG=1}.
    158 
    159 
    160 <h2 id="6432">64-Bit and 32-Bit Toolchains</h2>
    161 <p>Some toolchains come with both 64-bit and 32-bit versions. For example,
    162 directories {@code &lt;ndk&gt;/toolchain/&lt;name&gt;/prebuilt/} and
    163 {@code &lt;ndk&gt;/prebuilt/} may contain both {@code linux-x86} and
    164 {@code linux-x86_64} folders for Linux tools in 32-bit and 64-bit modes,
    165 respectively. The ndk-build script automatically chooses a 64-bit version of
    166 the toolchain if the host OS supports it. You can force the use of a 32-bit
    167 toolchain by using {@code NDK_HOST_32BIT=1} either in your environment or
    168 on the ndk-build command line.</p>
    169 <p>Note that 64-bit tools utilize host resources better (for instance, they are faster, and handle
    170 larger programs), and they can still generate 32-bit binaries for Android.</p>
    171 
    172 <h2 id="req">Requirements</h2>
    173 <p>You need GNU Make 3.81 or later to use ndk-build or the NDK in general.
    174 The build scripts will detect a non-compliant Make tool, and generate an error
    175 message.</p>
    176 <p>If you have GNU Make 3.81 installed, but the default <code>make</code>
    177 command doesnt launch it, define {@code GNUMAKE} in your environment to point to it
    178 before launching ndk-build. For example: </p>
    179 
    180 <pre class="no-pretty-print">
    181 $ export GNUMAKE=/usr/local/bin/gmake
    182 $ ndk-build
    183 </pre>
    184 
    185 <p>You can override other host prebuilt tools in {@code $NDK/prebuilt/&lt;OS&gt;/bin/}
    186 with the following environment variables: </p>
    187 
    188 <pre class="no-pretty-print">
    189 $ export NDK_HOST_AWK=&lt;path-to-awk&gt;
    190 $ export NDK_HOST_ECHO=&lt;path-to-echo&gt;
    191 $ export NDK_HOST_CMP=&lt;path-to-cmp&gt;
    192 </pre>
    193