blob: 36936aa54888a65e57dd0fb7a0d94546db10bf7c [file] [log] [blame]
David Warren5f6ca4f2009-04-30 17:11:58 -07001page.title=Android Build System
2pdk.version=1.0
3@jd:body
4
5
6<a name="toc"/>
7<div style="padding:10px">
8<a href="#androidBuildSystemIntroduction">Introduction</a><br/>
9<a href="#androidBuildSystemUnderstanding">Understanding Android's Build System</a><br/><div style="padding-left:40px">
10
11<a href="#androidBuildSystemOverview">Understanding the makefile</a><br/>
12<a href="#androidBuildSystemLayers">Layers</a><br/>
David Warren5f6ca4f2009-04-30 17:11:58 -070013<a href="#androidSourceSetupBuildingCodeBase">Building the Android Platform</a><br/><div style="padding-left:40px">
14
15<a href="#androidSourceSetupBuildingDeviceCodeBase">Device Code</a><br/>
16<a href="#androidBuildingCleaning">Cleaning Up</a><br/>
17<a href="#androidBuildingSpeeding">Speeding Up Rebuilds</a><br/>
18<a href="#androidBuildingTroubleshooting">Troubleshooting</a><br/></div>
19<a href="#androidSourceSetupBuildingKernel">Building the Android Kernel</a><br/><div style="padding-left:40px">
20
21<a href="#androidSourceSetupBuildingKernelCheckingBranch">Checking Out a Branch</a><br/>
22<a href="#androidSourceSetupBuildingKernelBranchLocation">Verifying Location</a><br/>
Reena Lee390be0e2009-04-30 17:22:16 -070023<a href="#androidSourceSetupBuildingKernelBuild">Building the Kernel</a><br/></div></div></div>
David Warren5f6ca4f2009-04-30 17:11:58 -070024
25<a name="androidBuildSystemIntroduction"></a><h2>Introduction</h2>
26
27<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>
28<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>
29
30
31<a name="androidBuildSystemUnderstanding"></a><h2>Understanding Android's Build System</h2>
32
David Warren5f6ca4f2009-04-30 17:11:58 -070033<a name="androidBuildSystemOverview"></a><h3>Understanding the makefile</h3>
34
35<p>A makefile defines how to build a particular application. Makefiles typically include all of the following elements:</p>
36<ol>
37 <li>Name: Give your build a name (<code>LOCAL_MODULE := &lt;build_name&gt;</code>).</li>
38 <li>Local Variables: Clear local variables with CLEAR_VARS (<code>include $(CLEAR_VARS)</code>).</li>
39 <li>Files: Determine which files your application depends upon (<code>LOCAL_SRC_FILES := main.c</code>).</li>
40 <li>Tags: Define tags, as necessary (<code>LOCAL_MODULE_TAGS := eng development</code>).</li>
41 <li>Libraries: Define whether your application links with other libraries (<code>LOCAL_SHARED_LIBRARIES := cutils</code>).</li>
42 <li>Template file: Include a template file to define underlining make tools for a particular target (<code>include $(BUILD_EXECUTABLE)</code>).</li>
43</ol>
44
45<p>The following snippet illustrates a typical makefile.</p>
46<pre class="prettyprint">
47LOCAL_PATH := $(my-dir)
48include $(CLEAR_VARS)
49LOCAL_MODULE := &lt;buil_name&gt;
50LOCAL_SRC_FILES := main.c
51LOCAL_MODULE_TAGS := eng development
52LOCAL_SHARED_LIBRARIES := cutils
53include $(BUILD_EXECUTABLE)
54(HOST_)EXECUTABLE, (HOST_)JAVA_LIBRARY, (HOST_)PREBUILT, (HOST_)SHARED_LIBRARY,
55 (HOST_)STATIC_LIBRARY, PACKAGE, JAVADOC, RAW_EXECUTABLE, RAW_STATIC_LIBRARY,
56 COPY_HEADERS, KEY_CHAR_MAP
57</pre>
58<p>The snippet above includes artificial line breaks to maintain a print-friendly document.</p>
59
60
61<a name="androidBuildSystemLayers"></a><h3>Layers</h3>
62
63<p>The build hierarchy includes the abstraction layers described in the table below.</p>
64
65<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>
66
67<table border=1 cellpadding=2 cellspacing=0>
68 <tbody><tr>
69 <th scope="col">Layer</th>
70 <th scope="col">Example</th>
71 <th scope="col">Description</th>
72 </tr>
73 <tr>
74 <td valign="top">Product</td>
75 <td valign="top">myProduct, myProduct_eu, myProduct_eu_fr, j2, sdk</td>
76 <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>
77 </tr>
78 <tr>
79 <td valign="top">Device</td>
80 <td valign="top">myDevice, myDevice_eu, myDevice_eu_lite</td>
81 <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>
82 </tr>
83 <tr>
84 <td valign="top">Board</td>
85 <td valign="top">sardine, trout, goldfish </td>
86 <td valign="top">The board layer represents the bare schematics of a product. You may still connect peripherals to the board layer. </td>
87 </tr>
88 <tr>
89 <td valign="top">Arch</td>
90 <td valign="top">arm (arm5te) (arm6), x86, 68k </td>
91 <td valign="top">The arch layer describes the processor running on your board. </td>
92 </tr>
93</table>
94
David Warren5f6ca4f2009-04-30 17:11:58 -070095<a name="androidSourceSetupBuildingCodeBase"></a><h2>Building the Android Platform</h2>
96
97<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>
98
99
100<a name="androidSourceSetupBuildingDeviceCodeBase"></a><h3>Device Code</h3>
101
Reena Lee390be0e2009-04-30 17:22:16 -0700102<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 -0700103<pre class="prettyprint">
104% cd $TOP
105
Reena Lee390be0e2009-04-30 17:22:16 -0700106% . build/envsetup.sh
David Warren5f6ca4f2009-04-30 17:11:58 -0700107
Reena Lee390be0e2009-04-30 17:22:16 -0700108# pick a configuration using choosecombo
109% choosecombo
David Warren5f6ca4f2009-04-30 17:11:58 -0700110
111% make -j4 PRODUCT-generic-user
112</pre>
113<p>You can also replace user with eng for a debug engineering build:</p>
114
115<pre class="prettyprint">
116% make -j4 PRODUCT-generic-eng
117</pre>
118
119
120<a name="androidBuildingCleaning"></a><h3>Cleaning Up</h3>
121
Reena Lee390be0e2009-04-30 17:22:16 -0700122<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 -0700123
124
125<a name="androidBuildingSpeeding"></a><h3>Speeding Up Rebuilds</h3>
126
Reena Lee390be0e2009-04-30 17:22:16 -0700127<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 -0700128<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>
129<pre class="prettyprint">
130% export USE_CCACHE=1
131</pre>
132<p>Doing so will force the build system to use the ccache compiler cache tool, which reduces recompiling all sources.</p>
133
Reena Lee390be0e2009-04-30 17:22:16 -0700134<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 -0700135
136
137<a name="androidBuildingTroubleshooting"></a><h3>Troubleshooting</h3>
138
139<p>The following error is likely caused by running an outdated version of Java.</p>
140<pre class="prettyprint">
141device Dex: core UNEXPECTED TOP-LEVEL ERROR:
142java.lang.NoSuchMethodError: method java.util.Arrays.hashCode with
143signature ([Ljava.lang.Object;)I was not found.
144 at com.google.util.FixedSizeList.hashCode(FixedSizeList.java:66)
145 at com.google.rop.code.Rop.hashCode(Rop.java:245)
146 at java.util.HashMap.hash(libgcj.so.7)
147[...]
148</pre>
149<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>
150<pre class="prettyprint">
151java version "1.5.0_07"
152Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-164)
153Java HotSpot(TM) Client VM (build 1.5.0_07-87, mixed mode, sharing)
154</pre>
155<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>
156
157
158<a name="androidSourceSetupBuildingKernel"></a><h2>Building the Android Kernel</h2>
159
160<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>
161
162
163<p>To build the kernel base, switch to the device directory (<code>/home/joe/android/device</code>) in order to establish variables and run:
164<pre class="prettyprint">
Reena Lee390be0e2009-04-30 17:22:16 -0700165% . build/envsetup.sh
David Warren5f6ca4f2009-04-30 17:11:58 -0700166% partner_setup generic
167</pre>
168<p>Then switch to the kernel directory <code>/home/joe/android/kernel</code>.
169
170
171<a name="androidSourceSetupBuildingKernelCheckingBranch"></a><h3>Checking Out a Branch</h3>
172
173<p>The default branch is always <code>android</code>. To check out a different branch, execute the following:</p>
174
175<pre class="prettyprint">
176% git checkout --track -b android-mydevice origin/android-mydevice
177 //Branch android-mydevice set up to track remote branch
178% refs/remotes/origin/android-mydevice.
179 //Switched to a new branch "android-mydevice"
180</pre>
181
182<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>
183
184
185<a name="androidSourceSetupBuildingKernelBranchLocation"></a><h3>Verifying Location</h3>
186
187<p>Find out which branches exist (both locally and remotely) and which one is active (marked with an asterisk) by executing the following:</p>
188<pre class="prettyprint">
189% git branch -a
190 android
191* android-mydevice
192 origin/HEAD
193 origin/android
194 origin/android-mydevice
195 origin/android-mychipset
196</pre>
197<p>To only see local branches, omit the <code>-a</code> flag.</p>
198
199
200<a name="androidSourceSetupBuildingKernelBuild"></a><h3>Building the Kernel</h3>
201
202<p>To build the kernel, execute:</p>
203<pre class="prettyprint">
204% make -j4
205</pre>