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