blob: b63248b6c36e5d2c583b848df8f51792b31edb80 [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/>
13<a href="#androidBuildSystemProductDefFiles">Product Definition Files</a><br/></div>
14<a href="#androidSourceSetupBuildingCodeBase">Building the Android Platform</a><br/><div style="padding-left:40px">
15
16<a href="#androidSourceSetupBuildingDeviceCodeBase">Device Code</a><br/>
17<a href="#androidBuildingCleaning">Cleaning Up</a><br/>
18<a href="#androidBuildingSpeeding">Speeding Up Rebuilds</a><br/>
19<a href="#androidBuildingTroubleshooting">Troubleshooting</a><br/></div>
20<a href="#androidSourceSetupBuildingKernel">Building the Android Kernel</a><br/><div style="padding-left:40px">
21
22<a href="#androidSourceSetupBuildingKernelCheckingBranch">Checking Out a Branch</a><br/>
23<a href="#androidSourceSetupBuildingKernelBranchLocation">Verifying Location</a><br/>
24<a href="#androidSourceSetupBuildingKernelBuild">Building the Kernel</a><br/></div></div></font>
25
26<a name="androidBuildSystemIntroduction"></a><h2>Introduction</h2>
27
28<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>
29<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>
30
31
32<a name="androidBuildSystemUnderstanding"></a><h2>Understanding Android's Build System</h2>
33
34
35
36<a name="androidBuildSystemOverview"></a><h3>Understanding the makefile</h3>
37
38<p>A makefile defines how to build a particular application. Makefiles typically include all of the following elements:</p>
39<ol>
40 <li>Name: Give your build a name (<code>LOCAL_MODULE := &lt;build_name&gt;</code>).</li>
41 <li>Local Variables: Clear local variables with CLEAR_VARS (<code>include $(CLEAR_VARS)</code>).</li>
42 <li>Files: Determine which files your application depends upon (<code>LOCAL_SRC_FILES := main.c</code>).</li>
43 <li>Tags: Define tags, as necessary (<code>LOCAL_MODULE_TAGS := eng development</code>).</li>
44 <li>Libraries: Define whether your application links with other libraries (<code>LOCAL_SHARED_LIBRARIES := cutils</code>).</li>
45 <li>Template file: Include a template file to define underlining make tools for a particular target (<code>include $(BUILD_EXECUTABLE)</code>).</li>
46</ol>
47
48<p>The following snippet illustrates a typical makefile.</p>
49<pre class="prettyprint">
50LOCAL_PATH := $(my-dir)
51include $(CLEAR_VARS)
52LOCAL_MODULE := &lt;buil_name&gt;
53LOCAL_SRC_FILES := main.c
54LOCAL_MODULE_TAGS := eng development
55LOCAL_SHARED_LIBRARIES := cutils
56include $(BUILD_EXECUTABLE)
57(HOST_)EXECUTABLE, (HOST_)JAVA_LIBRARY, (HOST_)PREBUILT, (HOST_)SHARED_LIBRARY,
58 (HOST_)STATIC_LIBRARY, PACKAGE, JAVADOC, RAW_EXECUTABLE, RAW_STATIC_LIBRARY,
59 COPY_HEADERS, KEY_CHAR_MAP
60</pre>
61<p>The snippet above includes artificial line breaks to maintain a print-friendly document.</p>
62
63
64<a name="androidBuildSystemLayers"></a><h3>Layers</h3>
65
66<p>The build hierarchy includes the abstraction layers described in the table below.</p>
67
68<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>
69
70<table border=1 cellpadding=2 cellspacing=0>
71 <tbody><tr>
72 <th scope="col">Layer</th>
73 <th scope="col">Example</th>
74 <th scope="col">Description</th>
75 </tr>
76 <tr>
77 <td valign="top">Product</td>
78 <td valign="top">myProduct, myProduct_eu, myProduct_eu_fr, j2, sdk</td>
79 <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>
80 </tr>
81 <tr>
82 <td valign="top">Device</td>
83 <td valign="top">myDevice, myDevice_eu, myDevice_eu_lite</td>
84 <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>
85 </tr>
86 <tr>
87 <td valign="top">Board</td>
88 <td valign="top">sardine, trout, goldfish </td>
89 <td valign="top">The board layer represents the bare schematics of a product. You may still connect peripherals to the board layer. </td>
90 </tr>
91 <tr>
92 <td valign="top">Arch</td>
93 <td valign="top">arm (arm5te) (arm6), x86, 68k </td>
94 <td valign="top">The arch layer describes the processor running on your board. </td>
95 </tr>
96</table>
97
98
99<a name="androidBuildSystemProductDefFiles"></a><h3>Product Definition Files</h3>
100
101<p>Product-specific variables are defined in product definition files. A product definition file can inherit from other product definition files, thus reducing the need to copy and simplifying maintenance.</p>
102<p>Variables maintained in a product definition files include:</p>
103<p><ul>
104<li><code>PRODUCT_DEVICE</code></LI>
105<LI><code>LOCALES</code></LI>
106<LI><code>BRANDING_PARTNER</code></LI>
107<LI><code>PROPERTY_OVERRIDES</code></LI>
108</UL>
109</P>
110<p>The snippet below illustrates a typical product definition file.</p>
111<PRE class="prettyprint">
112//device/target/product/core.mk
113PRODUCT_PACKAGES := Home SettingsProvider ...
114//device/target/product/generic.mk
115PRODUCT_PACKAGES := Calendar Camera SyncProvider ...
116$(call inherit-product, target/product/core.mk)
117PRODUCT_NAME := generic
118//device/partner/google/products/core.mk
119PRODUCT_PACKAGES := Maps GoogleAppsProvider ...
120$(call inherit-product, target/product/core.mk)
121//device/partner/google/products/generic.mk
122PRODUCT_PACKAGES := Gmail GmailProvider ...
123$(call inherit-product, partner/google/products/core.mk)
124$(call inherit-product, target/product/generic.mk)
125PRODUCT_NAME := google_generic
126
127</pre>
128
129
130<a name="androidSourceSetupBuildingCodeBase"></a><h2>Building the Android Platform</h2>
131
132<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>
133
134
135<a name="androidSourceSetupBuildingDeviceCodeBase"></a><h3>Device Code</h3>
136
137<p>Of the two options below, the first tends to yield more consistent results.</p>
138
139
140<a name="androidSourceSetupBuildingDeviceCodeBaseOption2"></a><h4>Option 1</h4>
141
142<p>Create a local version of buildspec.mk. The easiest way to do so is to change to your device directory and execute the following:</p>
143<pre class="prettyprint">% cp buildspec.mk.default buildspec.mk ; chmod u=rw buildspec.mk</pre>
144<p>The default <code>buildspec.mk</code>. file is written so that all options appear commented. In order to establish a personal configuration environment, edit <code>buildspec.mk</code> as desired.</p>
145<p>Once you have established your configuration environment, you can build the device code base by executing make in order to build the Android binaries. This may take a long time the first time you issue this command. On a dual-core machine, consider using '-j2' (or even '-j4') to speed up the build.</p>
146<pre class="prettyprint">% make -j2</pre>
147
148
149<a name="androidSourceSetupBuildingDeviceCodeBaseOption1"></a><h4>Option 2</h4>
150
151<p>To do a generic build of android, source <code>//device/envsetup.sh</code>, which contains necessary variable and function definitions, as described below.</p>
152<pre class="prettyprint">
153% cd $TOP
154
155% . envsetup.sh
156
157% partner_setup generic
158 //select generic as the product
159
160% make -j4 PRODUCT-generic-user
161</pre>
162<p>You can also replace user with eng for a debug engineering build:</p>
163
164<pre class="prettyprint">
165% make -j4 PRODUCT-generic-eng
166</pre>
167
168
169<a name="androidBuildingCleaning"></a><h3>Cleaning Up</h3>
170
171<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>//device/out/</code> directory where all generated files are stored.</p>
172
173
174<a name="androidBuildingSpeeding"></a><h3>Speeding Up Rebuilds</h3>
175
176<p> The binaries of each combo are stored as distinct sub-directories of <code>//device/out/</code>, making it possible to quickly switch between combos without having to recompile all sources each time. </p>
177<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>
178<pre class="prettyprint">
179% export USE_CCACHE=1
180</pre>
181<p>Doing so will force the build system to use the ccache compiler cache tool, which reduces recompiling all sources.</p>
182
183<p><code>ccache</code> binaries are provided in <code>//device/prebuilt/...</code> and don't need to get installed on your system.</p>
184
185
186<a name="androidBuildingTroubleshooting"></a><h3>Troubleshooting</h3>
187
188<p>The following error is likely caused by running an outdated version of Java.</p>
189<pre class="prettyprint">
190device Dex: core UNEXPECTED TOP-LEVEL ERROR:
191java.lang.NoSuchMethodError: method java.util.Arrays.hashCode with
192signature ([Ljava.lang.Object;)I was not found.
193 at com.google.util.FixedSizeList.hashCode(FixedSizeList.java:66)
194 at com.google.rop.code.Rop.hashCode(Rop.java:245)
195 at java.util.HashMap.hash(libgcj.so.7)
196[...]
197</pre>
198<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>
199<pre class="prettyprint">
200java version "1.5.0_07"
201Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-164)
202Java HotSpot(TM) Client VM (build 1.5.0_07-87, mixed mode, sharing)
203</pre>
204<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>
205
206
207<a name="androidSourceSetupBuildingKernel"></a><h2>Building the Android Kernel</h2>
208
209<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>
210
211
212<p>To build the kernel base, switch to the device directory (<code>/home/joe/android/device</code>) in order to establish variables and run:
213<pre class="prettyprint">
214% . envsetup.sh
215% partner_setup generic
216</pre>
217<p>Then switch to the kernel directory <code>/home/joe/android/kernel</code>.
218
219
220<a name="androidSourceSetupBuildingKernelCheckingBranch"></a><h3>Checking Out a Branch</h3>
221
222<p>The default branch is always <code>android</code>. To check out a different branch, execute the following:</p>
223
224<pre class="prettyprint">
225% git checkout --track -b android-mydevice origin/android-mydevice
226 //Branch android-mydevice set up to track remote branch
227% refs/remotes/origin/android-mydevice.
228 //Switched to a new branch "android-mydevice"
229</pre>
230
231<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>
232
233
234<a name="androidSourceSetupBuildingKernelBranchLocation"></a><h3>Verifying Location</h3>
235
236<p>Find out which branches exist (both locally and remotely) and which one is active (marked with an asterisk) by executing the following:</p>
237<pre class="prettyprint">
238% git branch -a
239 android
240* android-mydevice
241 origin/HEAD
242 origin/android
243 origin/android-mydevice
244 origin/android-mychipset
245</pre>
246<p>To only see local branches, omit the <code>-a</code> flag.</p>
247
248
249<a name="androidSourceSetupBuildingKernelBuild"></a><h3>Building the Kernel</h3>
250
251<p>To build the kernel, execute:</p>
252<pre class="prettyprint">
253% make -j4
254</pre>