blob: d51e8324ad432d3192cb96b0ff776bc6e25582d3 [file] [log] [blame] [view]
Skyler Kaufman991ae4d2011-04-07 12:30:41 -07001# Android Build System #
2
3<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>
4<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>
5
6
7
8
9<a name="androidBuildSystemOverview"></a><h4>Understanding the makefile</h4>
10
11<p>A makefile defines how to build a particular application. Makefiles typically include all of the following elements:</p>
12<ol>
13 <li>Name: Give your build a name (<code>LOCAL_MODULE := &lt;build_name&gt;</code>).</li>
14 <li>Local Variables: Clear local variables with CLEAR_VARS (<code>include $(CLEAR_VARS)</code>).</li>
15 <li>Files: Determine which files your application depends upon (<code>LOCAL_SRC_FILES := main.c</code>).</li>
16 <li>Tags: Define tags, as necessary (<code>LOCAL_MODULE_TAGS := eng development</code>).</li>
17 <li>Libraries: Define whether your application links with other libraries (<code>LOCAL_SHARED_LIBRARIES := cutils</code>).</li>
18 <li>Template file: Include a template file to define underlining make tools for a particular target (<code>include $(BUILD_EXECUTABLE)</code>).</li>
19</ol>
20
21<p>The following snippet illustrates a typical makefile.</p>
22<pre class="prettyprint">
23LOCAL_PATH := $(my-dir)
24include $(CLEAR_VARS)
25LOCAL_MODULE := &lt;buil_name&gt;
26LOCAL_SRC_FILES := main.c
27LOCAL_MODULE_TAGS := eng development
28LOCAL_SHARED_LIBRARIES := cutils
29include $(BUILD_EXECUTABLE)
30(HOST_)EXECUTABLE, (HOST_)JAVA_LIBRARY, (HOST_)PREBUILT, (HOST_)SHARED_LIBRARY,
31 (HOST_)STATIC_LIBRARY, PACKAGE, JAVADOC, RAW_EXECUTABLE, RAW_STATIC_LIBRARY,
32 COPY_HEADERS, KEY_CHAR_MAP
33</pre>
34<p>The snippet above includes artificial line breaks to maintain a print-friendly document.</p>
35
36
37<a name="androidBuildSystemLayers"></a><h4>Layers</h4>
38
39<p>The build hierarchy includes the abstraction layers described in the table below.</p>
40
41<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>
42
43<table border=1 cellpadding=2 cellspacing=0>
44 <tbody><tr>
45 <th scope="col">Layer</th>
46 <th scope="col">Example</th>
47 <th scope="col">Description</th>
48 </tr>
49 <tr>
50 <td valign="top">Product</td>
51 <td valign="top">myProduct, myProduct_eu, myProduct_eu_fr, j2, sdk</td>
52 <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>
53 </tr>
54 <tr>
55 <td valign="top">Device</td>
56 <td valign="top">myDevice, myDevice_eu, myDevice_eu_lite</td>
57 <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>
58 </tr>
59 <tr>
60 <td valign="top">Board</td>
61 <td valign="top">sardine, trout, goldfish </td>
62 <td valign="top">The board layer represents the bare schematics of a product. You may still connect peripherals to the board layer. </td>
63 </tr>
64 <tr>
65 <td valign="top">Arch</td>
66 <td valign="top">arm (arm5te) (arm6), x86, 68k </td>
67 <td valign="top">The arch layer describes the processor running on your board. </td>
68 </tr>
69</table>
70
71<a name="androidSourceSetupBuildingCodeBase"></a><h3>Building the Android Platform</h3>
72
73<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>
74
75
76<a name="androidSourceSetupBuildingDeviceCodeBase"></a><h4>Device Code</h4>
77
78<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>
79<pre class="prettyprint">
80% cd $TOP
81
82% . build/envsetup.sh
83
84# pick a configuration using choosecombo
85% choosecombo
86
87% make -j4 PRODUCT-generic-user
88</pre>
89<p>You can also replace user with eng for a debug engineering build:</p>
90
91<pre class="prettyprint">
92% make -j4 PRODUCT-generic-eng
93</pre>
94
95<p>These <a href="#androidBuildVariants">Build Variants</a> differ in terms of debug options and packages installed.
96
97
98<a name="androidBuildingCleaning"></a><h4>Cleaning Up</h4>
99
100<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>
101
102
103<a name="androidBuildingSpeeding"></a><h4>Speeding Up Rebuilds</h4>
104
105<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>
106<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>
107<pre class="prettyprint">
108% export USE_CCACHE=1
109</pre>
110<p>Doing so will force the build system to use the ccache compiler cache tool, which reduces recompiling all sources.</p>
111
112<p><code>ccache</code> binaries are provided in <code>//prebuilt/...</code> and don't need to get installed on your system.</p>
113
114
115<a name="androidBuildingTroubleshooting"></a><h4>Troubleshooting</h4>
116
117<p>The following error is likely caused by running an outdated version of Java.</p>
118<pre class="prettyprint">
119device Dex: core UNEXPECTED TOP-LEVEL ERROR:
120java.lang.NoSuchMethodError: method java.util.Arrays.hashCode with
121signature ([Ljava.lang.Object;)I was not found.
122 at com.google.util.FixedSizeList.hashCode(FixedSizeList.java:66)
123 at com.google.rop.code.Rop.hashCode(Rop.java:245)
124 at java.util.HashMap.hash(libgcj.so.7)
125[...]
126</pre>
127<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>
128<pre class="prettyprint">
129java version "1.5.0_07"
130Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-164)
131Java HotSpot(TM) Client VM (build 1.5.0_07-87, mixed mode, sharing)
132</pre>
133<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>
134
135
136<a name="androidSourceSetupBuildingKernel"></a><h3>Building the Android Kernel</h3>
137
138<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>
139
140
141<p>To build the kernel base, switch to the device directory (<code>/home/joe/android/device</code>) in order to establish variables and run:
142<pre class="prettyprint">
143% . build/envsetup.sh
144% partner_setup generic
145</pre>
146<p>Then switch to the kernel directory <code>/home/joe/android/kernel</code>.
147
148
149<a name="androidSourceSetupBuildingKernelCheckingBranch"></a><h4>Checking Out a Branch</h4>
150
151<p>The default branch is always <code>android</code>. To check out a different branch, execute the following:</p>
152
153<pre class="prettyprint">
154% git checkout --track -b android-mydevice origin/android-mydevice
155 //Branch android-mydevice set up to track remote branch
156% refs/remotes/origin/android-mydevice.
157 //Switched to a new branch "android-mydevice"
158</pre>
159
160<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>
161
162
163<a name="androidSourceSetupBuildingKernelBranchLocation"></a><h4>Verifying Location</h4>
164
165<p>Find out which branches exist (both locally and remotely) and which one is active (marked with an asterisk) by executing the following:</p>
166<pre class="prettyprint">
167% git branch -a
168 android
169* android-mydevice
170 origin/HEAD
171 origin/android
172 origin/android-mydevice
173 origin/android-mychipset
174</pre>
175<p>To only see local branches, omit the <code>-a</code> flag.</p>
176
177
178<a name="androidSourceSetupBuildingKernelBuild"></a><h4>Building the Kernel</h4>
179
180<p>To build the kernel, execute:</p>
181<pre class="prettyprint">
182% make -j4
183</pre>
184
185<a name="androidBuildVariants"></a><h3>Build Variants</h3>
186
187<p>
188When building for a particular product, it's often useful to have minor
189variations on what is ultimately the final release build. These are the
190currently-defined build variants:
191</p>
192
193<table border=1>
194<tr>
195 <td>
196 <code>eng <code>
197 </td>
198 <td>
199 This is the default flavor. A plain <code>make</code> is the
200 same as <code>make eng</code>.
201 <ul>
202 <li>Installs modules tagged with: <code>eng</code>, <code>debug</code>,
203 <code>user</code>, and/or <code>development</code>.
204 <li>Installs non-APK modules that have no tags specified.
205 <li>Installs APKs according to the product definition files, in
206 addition to tagged APKs.
207 <li><code>ro.secure=0</code>
208 <li><code>ro.debuggable=1</code>
209 <li><code>ro.kernel.android.checkjni=1</code>
210 <li><code>adb</code> is enabled by default.
211 </td>
212</tr>
213<tr>
214 <td>
215 <code>user <code>
216 </td>
217 <td>
218 <code>make user</code>
219 <p>
220 This is the flavor intended to be the final release bits.
221 <ul>
222 <li>Installs modules tagged with <code>user</code>.</li>
223 <li>Installs non-APK modules that have no tags specified.</li>
224 <li>Installs APKs according to the product definition files; tags
225 are ignored for APK modules.</li>
226 <li><code>ro.secure=1</code> </li>
227 <li><code>ro.debuggable=0</code> </li>
228 <li><code>adb</code> is disabled by default.</li>
229 </td>
230</tr>
231<tr>
232 <td>
233 <code>userdebug <code>
234 </td>
235 <td>
236 <code>make userdebug</code>
237 <p>
238 The same as <code>user</code>, except:
239 <ul>
240 <li>Also installs modules tagged with <code>debug</code>.
241 <li><code>ro.debuggable=1</code>
242 <li><code>adb</code> is enabled by default.
243 </td>
244</tr>
245</table>
246
247<p>
248If you build one flavor and then want to build another, you should run
249<code>make installclean</code> between the two makes to guarantee that
250you don't pick up files installed by the previous flavor. <code>make
251clean</code> will also suffice, but it takes a lot longer.
252</p>