blob: bda3768b32b533f913f9fc4cfa07ed33dad79c77 [file] [log] [blame] [view]
Skyler Kaufman991ae4d2011-04-07 12:30:41 -07001# Build Cookbook #
2
3<p>The Android Build Cookbook offers code snippets to help you quickly implement some common build tasks. For additional instruction, please see the other build documents in this section.</p>
4<h3><a name="simpleAPK"></a>Building a simple APK</h3>
5<pre>
6 LOCAL_PATH := $(call my-dir)
7 include $(CLEAR_VARS)
8 &nbsp;
9 # Build all java files in the java subdirectory
10 LOCAL_SRC_FILES := $(call all-subdir-java-files)
11 &nbsp;
12 # Name of the APK to build
13 LOCAL_PACKAGE_NAME := LocalPackage
14 &nbsp;
15 # Tell it to build an APK
16 include $(BUILD_PACKAGE)
17</pre>
18<h3><a name="APKJar"></a>Building a APK that depends on a static .jar file</h3>
19<pre>
20 LOCAL_PATH := $(call my-dir)
21 include $(CLEAR_VARS)
22 &nbsp;
23 # List of static libraries to include in the package
24 LOCAL_STATIC_JAVA_LIBRARIES := static-library
25 &nbsp;
26 # Build all java files in the java subdirectory
27 LOCAL_SRC_FILES := $(call all-subdir-java-files)
28 &nbsp;
29 # Name of the APK to build
30 LOCAL_PACKAGE_NAME := LocalPackage
31 &nbsp;
32 # Tell it to build an APK
33 include $(BUILD_PACKAGE)
34</pre>
35<h3><a name="APKPlatform"></a>Building a APK that should be signed with the platform key</h3>
36<pre>
37 LOCAL_PATH := $(call my-dir)
38 include $(CLEAR_VARS)
39 &nbsp;
40 # Build all java files in the java subdirectory
41 LOCAL_SRC_FILES := $(call all-subdir-java-files)
42 &nbsp;
43 # Name of the APK to build
44 LOCAL_PACKAGE_NAME := LocalPackage
45 &nbsp;
46 LOCAL_CERTIFICATE := platform
47 &nbsp;
48 # Tell it to build an APK
49 include $(BUILD_PACKAGE)
50</pre>
51<h3><a name="APKVendor"></a>Building a APK that should be signed with a specific vendor key</h3>
52<pre>
53 LOCAL_PATH := $(call my-dir)
54 include $(CLEAR_VARS)
55 &nbsp;
56 # Build all java files in the java subdirectory
57 LOCAL_SRC_FILES := $(call all-subdir-java-files)
58 &nbsp;
59 # Name of the APK to build
60 LOCAL_PACKAGE_NAME := LocalPackage
61 &nbsp;
62 LOCAL_CERTIFICATE := vendor/example/certs/app
63 &nbsp;
64 # Tell it to build an APK
65 include $(BUILD_PACKAGE)
66</pre>
67<h3><a name="prebuiltAPK"></a>Adding a prebuilt APK</h3>
68<pre>
69 LOCAL_PATH := $(call my-dir)
70 include $(CLEAR_VARS)
71 &nbsp;
72 # Module name should match apk name to be installed.
73 LOCAL_MODULE := LocalModuleName
74 LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
75 LOCAL_MODULE_CLASS := APPS
76 LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
77 &nbsp;
78 include $(BUILD_PREBUILT)
79</pre>
80<h3><a name="staticJava"></a>Adding a Static Java Library</h3>
81<pre>
82 LOCAL_PATH := $(call my-dir)
83 include $(CLEAR_VARS)
84 &nbsp;
85 # Build all java files in the java subdirectory
86 LOCAL_SRC_FILES := $(call all-subdir-java-files)
87 &nbsp;
88 # Any libraries that this library depends on
89 LOCAL_JAVA_LIBRARIES := android.test.runner
90 &nbsp;
91 # The name of the jar file to create
92 LOCAL_MODULE := sample
93 &nbsp;
94 # Build a static jar file.
95 include $(BUILD_STATIC_JAVA_LIBRARY)
96</pre>
97<h3><a name="mkVars"></a>Android.mk Variables</h3>
98
99<p>These are the variables that you'll commonly see in Android.mk files, listed
100alphabetically. First, a note on the variable naming: </p>
101
102<ul>
103 <li><b>LOCAL_</b> - These variables are set per-module. They are cleared
104 by the <code>include $(CLEAR_VARS)</code> line, so you can rely on them
105 being empty after including that file. Most of the variables you'll use
106 in most modules are LOCAL_ variables.</li>
107 <li><b>PRIVATE_</b> - These variables are make-target-specific variables. That
108 means they're only usable within the commands for that module. It also
109 means that they're unlikely to change behind your back from modules that
110 are included after yours. This
111 <a href="http://www.gnu.org/software/make/manual/make.html#Target_002dspecific">link to the make documentation</a>
112 describes more about target-specific variables.
113 </li>
114 <li><b>HOST_</b> and <b>TARGET_</b> - These contain the directories
115 and definitions that are specific to either the host or the target builds.
116 Do not set variables that start with HOST_ or TARGET_ in your makefiles.
117 </li>
118 <li><b>BUILD_</b> and <b>CLEAR_VARS</b> - These contain the names of
119 well-defined template makefiles to include. Some examples are CLEAR_VARS
120 and BUILD_HOST_PACKAGE.</li>
121 <li>Any other name is fair-game for you to use in your Android.mk. However,
122 remember that this is a non-recursive build system, so it is possible that
123 your variable will be changed by another Android.mk included later, and be
124 different when the commands for your rule / module are executed.</li>
125</ul>
126
127<table border=1 cellpadding=2 cellspacing=0>
128 <tbody><tr>
129 <th scope="col">Parameter</th>
130 <th scope="col">Description</th>
131 </tr>
132<tr>
133<td valign="top">LOCAL_AAPT_FLAGS</td>
134<td valign="top"></td>
135</tr>
136<tr>
137<td valign="top">LOCAL_ACP_UNAVAILABLE</td>
138<td valign="top"></td>
139</tr>
140<tr>
141<td valign="top">LOCAL_ADDITIONAL_JAVA_DIR</td>
142<td valign="top"></td>
143</tr>
144<tr>
145<td valign="top">LOCAL_AIDL_INCLUDES</td>
146<td valign="top"></td>
147</tr>
148<tr>
149<td valign="top">LOCAL_ALLOW_UNDEFINED_SYMBOLS</td>
150<td valign="top"></td>
151</tr>
152<tr>
153<td valign="top">LOCAL_ARM_MODE</td>
154<td valign="top"></td>
155</tr>
156<tr>
157<td valign="top">LOCAL_ASFLAGS</td>
158<td valign="top"></td>
159</tr>
160<tr>
161<td valign="top">LOCAL_ASSET_DIR</td>
162<td valign="top"></td>
163</tr>
164<tr>
165<td valign="top">LOCAL_ASSET_FILES</td>
166<td valign="top">In Android.mk files that <code>include $(BUILD_PACKAGE)</code> set this
167to the set of files you want built into your app. Usually:</p>
168<p><code>LOCAL_ASSET_FILES += $(call find-subdir-assets)</code></td>
169</tr>
170<tr>
171<td valign="top">LOCAL_BUILT_MODULE_STEM</td>
172<td valign="top"></td>
173</tr>
174<tr>
175<td valign="top">LOCAL_C_INCLUDES</td>
176<td valign="top"><p>Additional directories to instruct the C/C++ compilers to look for header
177files in. These paths are rooted at the top of the tree. Use
178<code>LOCAL_PATH</code> if you have subdirectories of your own that you
179want in the include paths. For example:</p>
180<p><code>
181LOCAL_C_INCLUDES += extlibs/zlib-1.2.3<br/>
182LOCAL_C_INCLUDES += $(LOCAL_PATH)/src
183</code></p>
184<p>You should not add subdirectories of include to
185<code>LOCAL_C_INCLUDES</code>, instead you should reference those files
186in the <code>#include</code> statement with their subdirectories. For
187example:</p>
188<p><code>#include &lt;utils/KeyedVector.h&gt;</code><br/>
189not <code><s>#include &lt;KeyedVector.h&gt;</s></code></p> </td>
190</tr>
191<tr>
192<td valign="top">LOCAL_CC</td>
193<td valign="top">If you want to use a different C compiler for this module, set LOCAL_CC
194to the path to the compiler. If LOCAL_CC is blank, the appropriate default
195compiler is used.</td>
196</tr>
197<tr>
198<td valign="top">LOCAL_CERTIFICATE</td>
199<td valign="top"></td>
200</tr>
201<tr>
202<td valign="top">LOCAL_CFLAGS</td>
203<td valign="top">If you have additional flags to pass into the C or C++ compiler, add
204them here. For example:</p>
205<p><code>LOCAL_CFLAGS += -DLIBUTILS_NATIVE=1</code></td>
206</tr>
207<tr>
208<td valign="top">LOCAL_CLASSPATH</td>
209<td valign="top"></td>
210</tr>
211<tr>
212<td valign="top">LOCAL_COMPRESS_MODULE_SYMBOLS</td>
213<td valign="top"></td>
214</tr>
215<tr>
216<td valign="top">LOCAL_COPY_HEADERS</td>
217<td valign="top"><p>The set of files to copy to the install include tree. You must also
218supply <code>LOCAL_COPY_HEADERS_TO</code>.</p>
219<p>This is going away because copying headers messes up the error messages, and
220may lead to people editing those headers instead of the correct ones. It also
221makes it easier to do bad layering in the system, which we want to avoid. We
222also aren't doing a C/C++ SDK, so there is no ultimate requirement to copy any
223headers.</p></td>
224</tr>
225<tr>
226<td valign="top">LOCAL_COPY_HEADERS_TO</td>
227<td valign="top"><p>The directory within "include" to copy the headers listed in
228<code>LOCAL_COPY_HEADERS</code> to.</p>
229<p>This is going away because copying headers messes up the error messages, and
230may lead to people editing those headers instead of the correct ones. It also
231makes it easier to do bad layering in the system, which we want to avoid. We
232also aren't doing a C/C++ SDK, so there is no ultimate requirement to copy any
233headers.</p></td>
234</tr>
235<tr>
236<td valign="top">LOCAL_CPP_EXTENSION</td>
237<td valign="top">If your C++ files end in something other than "<code>.cpp</code>",
238you can specify the custom extension here. For example:
239<p><code>LOCAL_CPP_EXTENSION := .cc</code></p>
240Note that all C++ files for a given module must have the same
241extension; it is not currently possible to mix different extensions.</td>
242</tr>
243<tr>
244<td valign="top">LOCAL_CPPFLAGS</td>
245<td valign="top">If you have additional flags to pass into <i>only</i> the C++ compiler, add
246them here. For example:</p>
247<p><code>LOCAL_CPPFLAGS += -ffriend-injection</code></p>
248<code>LOCAL_CPPFLAGS</code> is guaranteed to be after <code>LOCAL_CFLAGS</code>
249on the compile line, so you can use it to override flags listed in
250<code>LOCAL_CFLAGS</code></td>
251</tr>
252<tr>
253<td valign="top">LOCAL_CXX</td>
254<td valign="top">If you want to use a different C++ compiler for this module, set LOCAL_CXX
255to the path to the compiler. If LOCAL_CXX is blank, the appropriate default
256compiler is used.</td>
257</tr>
258<tr>
259<td valign="top">LOCAL_DX_FLAGS</td>
260<td valign="top"></td>
261</tr>
262<tr>
263<td valign="top">LOCAL_EXPORT_PACKAGE_RESOURCES</td>
264<td valign="top"></td>
265</tr>
266<tr>
267<td valign="top">LOCAL_FORCE_STATIC_EXECUTABLE</td>
268<td valign="top"><p>If your executable should be linked statically, set
269<code>LOCAL_FORCE_STATIC_EXECUTABLE:=true</code>. There is a very short
270list of libraries that we have in static form (currently only libc). This is
271really only used for executables in /sbin on the root filesystem.</p> </td>
272</tr>
273<tr>
274<td valign="top">LOCAL_GENERATED_SOURCES</td>
275<td valign="top"><p>Files that you add to <code>LOCAL_GENERATED_SOURCES</code> will be
276automatically generated and then linked in when your module is built.
277See the <a href="#custom-tools">Custom Tools</a> template makefile for an
278example.</p> </td>
279</tr>
280<tr>
281<td valign="top">LOCAL_INSTRUMENTATION_FOR</td>
282<td valign="top"></td>
283</tr>
284<tr>
285<td valign="top">LOCAL_INSTRUMENTATION_FOR_PACKAGE_NAME</td>
286<td valign="top"></td>
287</tr>
288<tr>
289<td valign="top">LOCAL_INTERMEDIATE_SOURCES</td>
290<td valign="top"></td>
291</tr>
292<tr>
293<td valign="top">LOCAL_INTERMEDIATE_TARGETS</td>
294<td valign="top"></td>
295</tr>
296<tr>
297<td valign="top">LOCAL_IS_HOST_MODULE</td>
298<td valign="top"></td>
299</tr>
300<tr>
301<td valign="top">LOCAL_JAR_MANIFEST</td>
302<td valign="top"></td>
303</tr>
304<tr>
305<td valign="top">LOCAL_JARJAR_RULES</td>
306<td valign="top"></td>
307</tr>
308<tr>
309<td valign="top">LOCAL_JAVA_LIBRARIES</td>
310<td valign="top"><p>When linking Java apps and libraries, <code>LOCAL_JAVA_LIBRARIES</code>
311specifies which sets of java classes to include. Currently there are
312two of these: <code>core</code> and <code>framework</code>.
313In most cases, it will look like this:</p>
314<p><code>LOCAL_JAVA_LIBRARIES := core framework</code></p>
315<p>Note that setting <code>LOCAL_JAVA_LIBRARIES</code> is not necessary
316(and is not allowed) when building an APK with
317"<code>include $(BUILD_PACKAGE)</code>". The appropriate libraries
318will be included automatically.</p> </td>
319</tr>
320<tr>
321<td valign="top">LOCAL_JAVA_RESOURCE_DIRS</td>
322<td valign="top"></td>
323</tr>
324<tr>
325<td valign="top">LOCAL_JAVA_RESOURCE_FILES</td>
326<td valign="top"></td>
327</tr>
328<tr>
329<td valign="top">LOCAL_JNI_SHARED_LIBRARIES</td>
330<td valign="top"></td>
331</tr>
332<tr>
333<td valign="top">LOCAL_LDFLAGS</td>
334<td valign="top"><p>You can pass additional flags to the linker by setting
335<code>LOCAL_LDFLAGS</code>. Keep in mind that the order of parameters is
336very important to ld, so test whatever you do on all platforms.</p> </td>
337</tr>
338<tr>
339<td valign="top">LOCAL_LDLIBS</td>
340<td valign="top"><p><code>LOCAL_LDLIBS</code> allows you to specify additional libraries
341that are not part of the build for your executable or library. Specify
342the libraries you want in -lxxx format; they're passed directly to the
343link line. However, keep in mind that there will be no dependency generated
344for these libraries. It's most useful in simulator builds where you want
345to use a library preinstalled on the host. The linker (ld) is a particularly
346fussy beast, so it's sometimes necessary to pass other flags here if you're
347doing something sneaky. Some examples:</p>
348<p><code>LOCAL_LDLIBS += -lcurses -lpthread<br/>
349LOCAL_LDLIBS += -Wl,-z,origin
350</code></p> </td>
351</tr>
352<tr>
353<td valign="top">LOCAL_MODULE</td>
354<td valign="top"><code>LOCAL_MODULE</code> is the name of what's supposed to be generated
355from your Android.mk. For exmample, for libkjs, the <code>LOCAL_MODULE</code>
356is "libkjs" (the build system adds the appropriate suffix -- .so .dylib .dll).
357For app modules, use <code>LOCAL_PACKAGE_NAME</code> instead of
358<code>LOCAL_MODULE</code>. </td>
359</tr>
360<tr>
361<td valign="top">LOCAL_MODULE_PATH</td>
362<td valign="top">Instructs the build system to put the module somewhere other than what's
363normal for its type. If you override this, make sure you also set
364<code>LOCAL_UNSTRIPPED_PATH</code> if it's an executable or a shared library
365so the unstripped binary has somewhere to go. An error will occur if you forget
366to.</p>
367<p>See <a href="#moving-modules">Putting modules elsewhere</a> for more.</td>
368</tr>
369<tr>
370<td valign="top">LOCAL_MODULE_STEM</td>
371<td valign="top"></td>
372</tr>
373<tr>
374<td valign="top">LOCAL_MODULE_TAGS</td>
375<td valign="top"><p>Set <code>LOCAL_MODULE_TAGS</code> to any number of whitespace-separated
376tags. <p>This variable controls what build flavors the package gets included in. For example:</p>
377<ul type="disc">
378 <li><code>user</code>: include this in user/userdebug builds</li>
379 <li><code>eng</code>: include this in eng builds</li>
380 <li><code>tests</code>: the target is a testing target and makes it available for tests</li>
381 <li><code>optional</code>: don't include this</li>
382</ul></td>
383</tr>
384<tr>
385<td valign="top">LOCAL_NO_DEFAULT_COMPILER_FLAGS</td>
386<td valign="top"></td>
387</tr>
388<tr>
389<td valign="top">LOCAL_NO_EMMA_COMPILE</td>
390<td valign="top"></td>
391</tr>
392<tr>
393<td valign="top">LOCAL_NO_EMMA_INSTRUMENT</td>
394<td valign="top"></td>
395</tr>
396<tr>
397<td valign="top">LOCAL_NO_STANDARD_LIBRARIES</td>
398<td valign="top"></td>
399</tr>
400<tr>
401<td valign="top">LOCAL_OVERRIDES_PACKAGES</td>
402<td valign="top"></td>
403</tr>
404<tr>
405<td valign="top">LOCAL_PACKAGE_NAME</td>
406<td valign="top"><code>LOCAL_PACKAGE_NAME</code> is the name of an app. For example,
407Dialer, Contacts, etc. </td>
408</tr>
409<tr>
410<td valign="top">LOCAL_POST_PROCESS_COMMAND</td>
411<td valign="top"><p>For host executables, you can specify a command to run on the module
412after it's been linked. You might have to go through some contortions
413to get variables right because of early or late variable evaluation:</p>
414<p><code>module := $(HOST_OUT_EXECUTABLES)/$(LOCAL_MODULE)<br/>
415LOCAL_POST_PROCESS_COMMAND := /Developer/Tools/Rez -d __DARWIN__ -t APPL\<br/>
416&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-d __WXMAC__ -o $(module) Carbon.r
417</code></p>
418 </td>
419</tr>
420<tr>
421<td valign="top">LOCAL_PREBUILT_EXECUTABLES</td>
422<td valign="top">When including $(BUILD_PREBUILT) or $(BUILD_HOST_PREBUILT), set these to
423executables that you want copied. They're located automatically into the
424right bin directory.</td>
425</tr>
426<tr>
427<td valign="top">LOCAL_PREBUILT_JAVA_LIBRARIES</td>
428<td valign="top"></td>
429</tr>
430<tr>
431<td valign="top">LOCAL_PREBUILT_LIBS</td>
432<td valign="top">When including $(BUILD_PREBUILT) or $(BUILD_HOST_PREBUILT), set these to
433libraries that you want copied. They're located automatically into the
434right lib directory.</td>
435</tr>
436<tr>
437<td valign="top">LOCAL_PREBUILT_OBJ_FILES</td>
438<td valign="top"></td>
439</tr>
440<tr>
441<td valign="top">LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES</td>
442<td valign="top"></td>
443</tr>
444<tr>
445<td valign="top">LOCAL_PRELINK_MODULE</td>
446<td valign="top"></td>
447</tr>
448<tr>
449<td valign="top">LOCAL_REQUIRED_MODULES</td>
450<td valign="top"><p>Set <code>LOCAL_REQUIRED_MODULES</code> to any number of whitespace-separated
451module names, like "libblah" or "Email". If this module is installed, all
452of the modules that it requires will be installed as well. This can be
453used to, e.g., ensure that necessary shared libraries or providers are
454installed when a given app is installed.</td>
455</tr>
456<tr>
457<td valign="top">LOCAL_RESOURCE_DIR</td>
458<td valign="top"></td>
459</tr>
460<tr>
461<td valign="top">LOCAL_SDK_VERSION</td>
462<td valign="top"></td>
463</tr>
464<tr>
465<td valign="top">LOCAL_SHARED_LIBRARIES</td>
466<td valign="top">These are the libraries you directly link against. You don't need to
467pass transitively included libraries. Specify the name without the suffix:</p>
468<p><code>LOCAL_SHARED_LIBRARIES := \<br/>
469 &nbsp;&nbsp;&nbsp;&nbsp;libutils \<br/>
470 &nbsp;&nbsp;&nbsp;&nbsp;libui \<br/>
471 &nbsp;&nbsp;&nbsp;&nbsp;libaudio \<br/>
472 &nbsp;&nbsp;&nbsp;&nbsp;libexpat \<br/>
473 &nbsp;&nbsp;&nbsp;&nbsp;libsgl
474</code></td>
475</tr>
476<tr>
477<td valign="top">LOCAL_SRC_FILES</td>
478<td valign="top">The build system looks at <code>LOCAL_SRC_FILES</code> to know what source
479files to compile -- .cpp .c .y .l .java. For lex and yacc files, it knows
480how to correctly do the intermediate .h and .c/.cpp files automatically. If
481the files are in a subdirectory of the one containing the Android.mk, prefix
482them with the directory name:</p>
483<p><code>LOCAL_SRC_FILES := \<br/>
484 &nbsp;&nbsp;&nbsp;&nbsp;file1.cpp \<br/>
485 &nbsp;&nbsp;&nbsp;&nbsp;dir/file2.cpp
486</code></td>
487</tr>
488<tr>
489<td valign="top">LOCAL_STATIC_JAVA_LIBRARIES</td>
490<td valign="top"></td>
491</tr>
492<tr>
493<td valign="top">LOCAL_STATIC_LIBRARIES</td>
494<td valign="top">These are the static libraries that you want to include in your module.
495Mostly, we use shared libraries, but there are a couple of places, like
496executables in sbin and host executables where we use static libraries instead.
497<p><code>LOCAL_STATIC_LIBRARIES := \<br/>
498 &nbsp;&nbsp;&nbsp;&nbsp;libutils \<br/>
499 &nbsp;&nbsp;&nbsp;&nbsp;libtinyxml
500</code></td>
501</tr>
502<tr>
503<td valign="top">LOCAL_UNINSTALLABLE_MODULE</td>
504<td valign="top"></td>
505</tr>
506<tr>
507<td valign="top">LOCAL_UNSTRIPPED_PATH</td>
508<td valign="top">Instructs the build system to put the unstripped version of the module
509somewhere other than what's normal for its type. Usually, you override this
510because you overrode <code>LOCAL_MODULE_PATH</code> for an executable or a
511shared library. If you overrode <code>LOCAL_MODULE_PATH</code>, but not
512<code>LOCAL_UNSTRIPPED_PATH</code>, an error will occur.</p>
513<p>See <a href="#moving-modules">Putting modules elsewhere</a> for more.</td>
514</tr>
515<tr>
516<td valign="top">LOCAL_WHOLE_STATIC_LIBRARIES</td>
517<td valign="top">These are the static libraries that you want to include in your module without allowing
518the linker to remove dead code from them. This is mostly useful if you want to add a static library
519to a shared library and have the static library's content exposed from the shared library.
520<p><code>LOCAL_WHOLE_STATIC_LIBRARIES := \<br/>
521 &nbsp;&nbsp;&nbsp;&nbsp;libsqlite3_android<br/>
522</code></td>
523</tr>
524<tr>
525<td valign="top">LOCAL_YACCFLAGS</td>
526<td valign="top">Any flags to pass to invocations of yacc for your module. A known limitation
527here is that the flags will be the same for all invocations of YACC for your
528module. This can be fixed. If you ever need it to be, just ask.</p>
529<p><code>LOCAL_YACCFLAGS := -p kjsyy</code></td>
530</tr>
531<tr>
532<td valign="top">OVERRIDE_BUILT_MODULE_PATH</td>
533<td valign="top"></td>
534</tr>
535
536</table>