blob: 4b29202e5b7925d5f590bd61c4382cc64356d776 [file] [log] [blame]
David Warren5f6ca4f2009-04-30 17:11:58 -07001page.title=Android Build System
2pdk.version=1.0
3@jd:body
4
5
David Warren5c40a482009-06-05 15:11:21 -07006<div id="qv-wrapper">
7<div id="qv">
8<h2>In this document</h2>
David Warren5f6ca4f2009-04-30 17:11:58 -07009<a name="toc"/>
David Warren5c40a482009-06-05 15:11:21 -070010<ul>
11<li><a href="#androidBuildSystemOverview">Understanding the makefile</a></li>
12<li><a href="#androidBuildSystemLayers">Layers</a></li>
13<li><a href="#androidSourceSetupBuildingCodeBase">Building the Android Platform</a></li>
14<li><a href="#androidSourceSetupBuildingKernel">Building the Android Kernel</a></li>
Reena Leed39413e2009-06-11 13:26:29 -070015<li><a href="#androidBuildVariants">Build Variants</a></li>
David Warren5c40a482009-06-05 15:11:21 -070016</ul>
17</div>
18</div>
David Warren5f6ca4f2009-04-30 17:11:58 -070019
20<p>Android uses a custom build system to generate tools, binaries, and documentation. This document provides an overview of Android's build system and instructions for doing a simple build. </p>
21<p>Android's build system is make based and requires a recent version of GNU Make (note that Android uses advanced features of GNU Make that may not yet appear on the GNU Make web site). Before continuing, check your version of make by running <code>% make -v</code>. If you don't have version 3.80 or greater, you need to <a href="http://www.gnu.org/software/make/">upgrade your version of make</a>. </p>
22
23
David Warren5f6ca4f2009-04-30 17:11:58 -070024
David Warren5c40a482009-06-05 15:11:21 -070025
26<a name="androidBuildSystemOverview"></a><h4>Understanding the makefile</h4>
David Warren5f6ca4f2009-04-30 17:11:58 -070027
28<p>A makefile defines how to build a particular application. Makefiles typically include all of the following elements:</p>
29<ol>
30 <li>Name: Give your build a name (<code>LOCAL_MODULE := &lt;build_name&gt;</code>).</li>
31 <li>Local Variables: Clear local variables with CLEAR_VARS (<code>include $(CLEAR_VARS)</code>).</li>
32 <li>Files: Determine which files your application depends upon (<code>LOCAL_SRC_FILES := main.c</code>).</li>
33 <li>Tags: Define tags, as necessary (<code>LOCAL_MODULE_TAGS := eng development</code>).</li>
34 <li>Libraries: Define whether your application links with other libraries (<code>LOCAL_SHARED_LIBRARIES := cutils</code>).</li>
35 <li>Template file: Include a template file to define underlining make tools for a particular target (<code>include $(BUILD_EXECUTABLE)</code>).</li>
36</ol>
37
38<p>The following snippet illustrates a typical makefile.</p>
39<pre class="prettyprint">
40LOCAL_PATH := $(my-dir)
41include $(CLEAR_VARS)
42LOCAL_MODULE := &lt;buil_name&gt;
43LOCAL_SRC_FILES := main.c
44LOCAL_MODULE_TAGS := eng development
45LOCAL_SHARED_LIBRARIES := cutils
46include $(BUILD_EXECUTABLE)
47(HOST_)EXECUTABLE, (HOST_)JAVA_LIBRARY, (HOST_)PREBUILT, (HOST_)SHARED_LIBRARY,
48 (HOST_)STATIC_LIBRARY, PACKAGE, JAVADOC, RAW_EXECUTABLE, RAW_STATIC_LIBRARY,
49 COPY_HEADERS, KEY_CHAR_MAP
50</pre>
51<p>The snippet above includes artificial line breaks to maintain a print-friendly document.</p>
52
53
David Warren5c40a482009-06-05 15:11:21 -070054<a name="androidBuildSystemLayers"></a><h4>Layers</h4>
David Warren5f6ca4f2009-04-30 17:11:58 -070055
56<p>The build hierarchy includes the abstraction layers described in the table below.</p>
57
58<p>Each layer relates to the one above it in a one-to-many relationship. For example, an arch can have more than one board and each board can have more than one device. You may define an element in a given layer as a specialization of an element in the same layer, thus eliminating copying and simplifying maintenance.</p>
59
60<table border=1 cellpadding=2 cellspacing=0>
61 <tbody><tr>
62 <th scope="col">Layer</th>
63 <th scope="col">Example</th>
64 <th scope="col">Description</th>
65 </tr>
66 <tr>
67 <td valign="top">Product</td>
68 <td valign="top">myProduct, myProduct_eu, myProduct_eu_fr, j2, sdk</td>
69 <td valign="top">The product layer defines a complete specification of a shipping product, defining which modules to build and how to configure them. You might offer a device in several different versions based on locale, for example, or on features such as a camera. </td>
70 </tr>
71 <tr>
72 <td valign="top">Device</td>
73 <td valign="top">myDevice, myDevice_eu, myDevice_eu_lite</td>
74 <td valign="top">The device layer represents the physical layer of plastic on the device. For example, North American devices probably include QWERTY keyboards whereas devices sold in France probably include AZERTY keyboards. Peripherals typically connect to the device layer. </td>
75 </tr>
76 <tr>
77 <td valign="top">Board</td>
78 <td valign="top">sardine, trout, goldfish </td>
79 <td valign="top">The board layer represents the bare schematics of a product. You may still connect peripherals to the board layer. </td>
80 </tr>
81 <tr>
82 <td valign="top">Arch</td>
83 <td valign="top">arm (arm5te) (arm6), x86, 68k </td>
84 <td valign="top">The arch layer describes the processor running on your board. </td>
85 </tr>
86</table>
87
David Warren5c40a482009-06-05 15:11:21 -070088<a name="androidSourceSetupBuildingCodeBase"></a><h3>Building the Android Platform</h3>
David Warren5f6ca4f2009-04-30 17:11:58 -070089
90<p>This section describes how to build the default version of Android. Once you are comfortable with a generic build, then you can begin to modify Android for your own target device.</p>
91
92
David Warren5c40a482009-06-05 15:11:21 -070093<a name="androidSourceSetupBuildingDeviceCodeBase"></a><h4>Device Code</h4>
David Warren5f6ca4f2009-04-30 17:11:58 -070094
Reena Lee390be0e2009-04-30 17:22:16 -070095<p>To do a generic build of android, source <code>build/envsetup.sh</code>, which contains necessary variable and function definitions, as described below.</p>
David Warren5f6ca4f2009-04-30 17:11:58 -070096<pre class="prettyprint">
97% cd $TOP
98
Reena Lee390be0e2009-04-30 17:22:16 -070099% . build/envsetup.sh
David Warren5f6ca4f2009-04-30 17:11:58 -0700100
Reena Lee390be0e2009-04-30 17:22:16 -0700101# pick a configuration using choosecombo
102% choosecombo
David Warren5f6ca4f2009-04-30 17:11:58 -0700103
104% make -j4 PRODUCT-generic-user
105</pre>
106<p>You can also replace user with eng for a debug engineering build:</p>
107
108<pre class="prettyprint">
109% make -j4 PRODUCT-generic-eng
110</pre>
111
Reena Leed39413e2009-06-11 13:26:29 -0700112<p>These <a href="#androidBuildVariants">Build Variants</a> differ in terms of debug options and packages installed.
113
David Warren5f6ca4f2009-04-30 17:11:58 -0700114
David Warren5c40a482009-06-05 15:11:21 -0700115<a name="androidBuildingCleaning"></a><h4>Cleaning Up</h4>
David Warren5f6ca4f2009-04-30 17:11:58 -0700116
Reena Lee390be0e2009-04-30 17:22:16 -0700117<p>Execute <code>% m clean</code> to clean up the binaries you just created. You can also execute <code>% m clobber</code> to get rid of the binaries of all combos. <code>% m clobber</code> is equivalent to removing the <code>//out/</code> directory where all generated files are stored.</p>
David Warren5f6ca4f2009-04-30 17:11:58 -0700118
119
David Warren5c40a482009-06-05 15:11:21 -0700120<a name="androidBuildingSpeeding"></a><h4>Speeding Up Rebuilds</h4>
David Warren5f6ca4f2009-04-30 17:11:58 -0700121
Reena Lee390be0e2009-04-30 17:22:16 -0700122<p> The binaries of each combo are stored as distinct sub-directories of <code>//out/</code>, making it possible to quickly switch between combos without having to recompile all sources each time. </p>
David Warren5f6ca4f2009-04-30 17:11:58 -0700123<p> However, performing a clean rebuild is necessary if the build system doesn't catch changes to environment variables or makefiles. If this happens often, you should define the <code>USE_CCACHE</code> environment variable as shown below: </p>
124<pre class="prettyprint">
125% export USE_CCACHE=1
126</pre>
127<p>Doing so will force the build system to use the ccache compiler cache tool, which reduces recompiling all sources.</p>
128
Reena Lee390be0e2009-04-30 17:22:16 -0700129<p><code>ccache</code> binaries are provided in <code>//prebuilt/...</code> and don't need to get installed on your system.</p>
David Warren5f6ca4f2009-04-30 17:11:58 -0700130
131
David Warren5c40a482009-06-05 15:11:21 -0700132<a name="androidBuildingTroubleshooting"></a><h4>Troubleshooting</h4>
David Warren5f6ca4f2009-04-30 17:11:58 -0700133
134<p>The following error is likely caused by running an outdated version of Java.</p>
135<pre class="prettyprint">
136device Dex: core UNEXPECTED TOP-LEVEL ERROR:
137java.lang.NoSuchMethodError: method java.util.Arrays.hashCode with
138signature ([Ljava.lang.Object;)I was not found.
139 at com.google.util.FixedSizeList.hashCode(FixedSizeList.java:66)
140 at com.google.rop.code.Rop.hashCode(Rop.java:245)
141 at java.util.HashMap.hash(libgcj.so.7)
142[...]
143</pre>
144<p><code>dx</code> is a Java program that uses facilities first made available in Java version 1.5. Check your version of Java by executing <code>% java -version</code> in the shell you use to build. You should see something like:</p>
145<pre class="prettyprint">
146java version "1.5.0_07"
147Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-164)
148Java HotSpot(TM) Client VM (build 1.5.0_07-87, mixed mode, sharing)
149</pre>
150<p>If you do have Java 1.5 or later and your receive this error, verify that you have properly updated your <code>PATH</code> variable.</p>
151
152
David Warren5c40a482009-06-05 15:11:21 -0700153<a name="androidSourceSetupBuildingKernel"></a><h3>Building the Android Kernel</h3>
David Warren5f6ca4f2009-04-30 17:11:58 -0700154
155<p>This section describes how to build Android's default kernel. Once you are comfortable with a generic build, then you can begin to modify Android drivers for your own target device.</p>
156
157
158<p>To build the kernel base, switch to the device directory (<code>/home/joe/android/device</code>) in order to establish variables and run:
159<pre class="prettyprint">
Reena Lee390be0e2009-04-30 17:22:16 -0700160% . build/envsetup.sh
David Warren5f6ca4f2009-04-30 17:11:58 -0700161% partner_setup generic
162</pre>
163<p>Then switch to the kernel directory <code>/home/joe/android/kernel</code>.
164
165
David Warren5c40a482009-06-05 15:11:21 -0700166<a name="androidSourceSetupBuildingKernelCheckingBranch"></a><h4>Checking Out a Branch</h4>
David Warren5f6ca4f2009-04-30 17:11:58 -0700167
168<p>The default branch is always <code>android</code>. To check out a different branch, execute the following:</p>
169
170<pre class="prettyprint">
171% git checkout --track -b android-mydevice origin/android-mydevice
172 //Branch android-mydevice set up to track remote branch
173% refs/remotes/origin/android-mydevice.
174 //Switched to a new branch "android-mydevice"
175</pre>
176
177<p>To simplify code management, give your local branch the same name as the remote branch it is tracking (as illustrated in the snippet above). Switch between branches by executing <code>% git checkout &lt;branchname&gt;</code>.</p>
178
179
David Warren5c40a482009-06-05 15:11:21 -0700180<a name="androidSourceSetupBuildingKernelBranchLocation"></a><h4>Verifying Location</h4>
David Warren5f6ca4f2009-04-30 17:11:58 -0700181
182<p>Find out which branches exist (both locally and remotely) and which one is active (marked with an asterisk) by executing the following:</p>
183<pre class="prettyprint">
184% git branch -a
185 android
186* android-mydevice
187 origin/HEAD
188 origin/android
189 origin/android-mydevice
190 origin/android-mychipset
191</pre>
192<p>To only see local branches, omit the <code>-a</code> flag.</p>
193
194
David Warren5c40a482009-06-05 15:11:21 -0700195<a name="androidSourceSetupBuildingKernelBuild"></a><h4>Building the Kernel</h4>
David Warren5f6ca4f2009-04-30 17:11:58 -0700196
197<p>To build the kernel, execute:</p>
198<pre class="prettyprint">
199% make -j4
Reena Leed39413e2009-06-11 13:26:29 -0700200</pre>
201
202<a name="androidBuildVariants"></a><h3>Build Variants</h3>
203
204<p>
205When building for a particular product, it's often useful to have minor
206variations on what is ultimately the final release build. These are the
207currently-defined build variants:
208</p>
209
210<table border=1>
211<tr>
212 <td>
213 <code>eng<code>
214 </td>
215 <td>
216 This is the default flavor. A plain "<code>make</code>" is the
217 same as "<code>make eng</code>".
218 <ul>
219 <li>Installs modules tagged with: <code>eng</code>, <code>debug</code>,
220 <code>user</code>, and/or <code>development</code>.
221 <li>Installs non-APK modules that have no tags specified.
222 <li>Installs APKs according to the product definition files, in
223 addition to tagged APKs.
224 <li><code>ro.secure=0</code>
225 <li><code>ro.debuggable=1</code>
226 <li><code>ro.kernel.android.checkjni=1</code>
227 <li><code>adb</code> is enabled by default.
228 </td>
229</tr>
230<tr>
231 <td>
232 <code>user<code>
233 </td>
234 <td>
235 "<code>make user</code>"
236 <p>
237 This is the flavor intended to be the final release bits.
238 <ul>
239 <li>Installs modules tagged with <code>user</code>.</li>
240 <li>Installs non-APK modules that have no tags specified.</li>
241 <li>Installs APKs according to the product definition files; tags
242 are ignored for APK modules.</li>
243 <li><code>ro.secure=1</code> </li>
244 <li><code>ro.debuggable=0</code> </li>
245 <li><code>adb</code> is disabled by default.</li>
246 </td>
247</tr>
248<tr>
249 <td>
250 <code>userdebug<code>
251 </td>
252 <td>
253 "<code>make userdebug</code>"
254 <p>
255 The same as <code>user</code>, except:
256 <ul>
257 <li>Also installs modules tagged with <code>debug</code>.
258 <li><code>ro.debuggable=1</code>
259 <li><code>adb</code> is enabled by default.
260 </td>
261</tr>
262</table>
263
264<p>
265If you build one flavor and then want to build another, you should run
266"<code>make installclean</code>" between the two makes to guarantee that
267you don't pick up files installed by the previous flavor. "<code>make
268clean</code>" will also suffice, but it takes a lot longer.
269</p>