1 <html devsite> 2 <head> 3 <title>Establishing a Build Environment</title> 4 <meta name="project_path" value="/_project.yaml" /> 5 <meta name="book_path" value="/_book.yaml" /> 6 </head> 7 <body> 8 <!-- 9 Copyright 2017 The Android Open Source Project 10 11 Licensed under the Apache License, Version 2.0 (the "License"); 12 you may not use this file except in compliance with the License. 13 You may obtain a copy of the License at 14 15 http://www.apache.org/licenses/LICENSE-2.0 16 17 Unless required by applicable law or agreed to in writing, software 18 distributed under the License is distributed on an "AS IS" BASIS, 19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 See the License for the specific language governing permissions and 21 limitations under the License. 22 --> 23 24 <p>This section describes how to set up your local work environment to build 25 the Android source files. You will need to use Linux or Mac OS. Building under 26 Windows is not currently supported.</p> 27 <p>For an overview of the entire code-review and code-update process, see <a 28 href="life-of-a-patch.html">Life of a Patch</a>.</p> 29 <p class="note"><strong>Note:</strong> All commands in this site are preceded 30 by a dollar sign ($) to differentiate them from output or entries within files. 31 You may use the <em>Click to copy</em> feature at the top right of each command 32 box to copy all lines without the dollar signs or triple-click each line to 33 copy it individually without the dollar sign.</p> 34 <h2 id="choosing-a-branch">Choosing a Branch</h2> 35 <p>Some of the requirements for your build environment are determined by which 36 version of the source code you plan to compile. See 37 <a href="build-numbers.html">Build Numbers</a> for a full listing of branches you may 38 choose from. You may also choose to download and build the latest source code 39 (called <code>master</code>), in which case you will simply omit the branch specification 40 when you initialize the repository.</p> 41 <p>Once you have selected a branch, follow the appropriate instructions below to 42 set up your build environment.</p> 43 <h2 id="setting-up-a-linux-build-environment">Setting up a Linux build environment</h2> 44 <p>These instructions apply to all branches, including <code>master</code>.</p> 45 <p>The Android build is routinely tested in house on recent versions of 46 Ubuntu LTS (14.04), but most distributions should have the required 47 build tools available. Reports of successes or failures on other 48 distributions are welcome.</p> 49 <p>For Gingerbread (2.3.x) and newer versions, including the <code>master</code> 50 branch, a 64-bit environment is required. Older versions can be 51 compiled on 32-bit systems.</p> 52 <p class="note"><strong>Note:</strong> See the <a 53 href="requirements.html">Requirements</a> for the complete list of hardware and 54 software requirements. Then follow the detailed instructions for Ubuntu and Mac 55 OS below.</p> 56 57 <h3 id="installing-the-jdk">Installing the JDK</h3> 58 <p>The <code>master</code> branch of Android in the <a 59 href="https://android.googlesource.com/">Android Open Source Project (AOSP)</a> 60 requires Java 8. On Ubuntu, use <a 61 href="http://openjdk.java.net/install/">OpenJDK</a>.</p> 62 <p>See <a href="requirements.html#jdk">JDK Requirements</a> for older 63 versions.</p> 64 65 <h4 id="for-ubuntu-15-04">For Ubuntu >= 15.04</h4> 66 <p>Run the following:</p> 67 <pre class="devsite-click-to-copy"> 68 <code class="devsite-terminal">sudo apt-get update</code> 69 <code class="devsite-terminal">sudo apt-get install openjdk-8-jdk</code> 70 </pre> 71 72 <h4 id="for-ubuntu-14-04">For Ubuntu LTS 14.04</h4> 73 <p>There are no available supported OpenJDK 8 packages for Ubuntu 14.04. The 74 <strong>Ubuntu 15.04 OpenJDK 8</strong> packages have been used successfully 75 with Ubuntu 14.04. <em>Newer package versions (e.g. those for 15.10, 16.04) were 76 found not to work on 14.04 using the instructions below.</em></p> 77 <ol> 78 <li> 79 <p>Download the <code>.deb</code> packages for 64-bit architecture from 80 <a href="http://archive.ubuntu.com/ubuntu/pool/universe/o/openjdk-8/">archive.ubuntu.com</a>:</p> 81 <ul> 82 <li><a 83 href="http://archive.ubuntu.com/ubuntu/pool/universe/o/openjdk-8/openjdk-8-jre-headless_8u45-b14-1_amd64.deb">openjdk-8-jre-headless_8u45-b14-1_amd64.deb</a> 84 with SHA256 <code>0f5aba8db39088283b51e00054813063173a4d8809f70033976f83e214ab56c0</code></li> 85 <li><a 86 href="http://archive.ubuntu.com/ubuntu/pool/universe/o/openjdk-8/openjdk-8-jre_8u45-b14-1_amd64.deb">openjdk-8-jre_8u45-b14-1_amd64.deb</a> 87 with SHA256 <code>9ef76c4562d39432b69baf6c18f199707c5c56a5b4566847df908b7d74e15849</code></li> 88 <li><a 89 href="http://archive.ubuntu.com/ubuntu/pool/universe/o/openjdk-8/openjdk-8-jdk_8u45-b14-1_amd64.deb">openjdk-8-jdk_8u45-b14-1_amd64.deb</a> 90 with SHA256 <code>6e47215cf6205aa829e6a0a64985075bd29d1f428a4006a80c9db371c2fc3c4c</code></li> 91 </ul> 92 </li> 93 <li> 94 <p>Optionally, confirm the checksums of the downloaded files against the SHA256 95 string listed with each package above.</p> 96 <p>For example, with the <code>sha256sum</code> tool:</p> 97 <pre class="devsite-terminal devsite-click-to-copy"> 98 sha256sum {downloaded.deb file} 99 </pre> 100 </li> 101 <li> 102 <p>Install the packages:</p> 103 <pre class="devsite-terminal devsite-click-to-copy"> 104 sudo apt-get update 105 </pre> 106 <p>Run <code>dpkg</code> for each of the .deb files you downloaded. It may produce errors due to 107 missing dependencies:</p> 108 <pre class="devsite-terminal devsite-click-to-copy"> 109 sudo dpkg -i {downloaded.deb file} 110 </pre> 111 <p>To fix missing dependencies:</p> 112 <pre class="devsite-terminal devsite-click-to-copy"> 113 sudo apt-get -f install 114 </pre> 115 </li> 116 </ol> 117 118 <h4 id="default-java-version">Update the default Java version - optional</h4> 119 120 <p>Optionally, for the Ubuntu versions above update the default Java version by 121 running:</p> 122 <pre class="devsite-click-to-copy"> 123 <code class="devsite-terminal">sudo update-alternatives --config java</code> 124 <code class="devsite-terminal">sudo update-alternatives --config javac</code> 125 </pre> 126 127 <p>If, during a build, you encounter version errors for Java, set its 128 path as described in the <a href="building.html#wrong-java-version">Wrong 129 Java Version</a> section.</p> 130 131 <h3 id="installing-required-packages-ubuntu-1404">Installing required packages (Ubuntu 14.04)</h3> 132 133 <p>You will need a 64-bit version of Ubuntu. Ubuntu 14.04 is recommended.</p> 134 135 <pre class="devsite-terminal devsite-click-to-copy"> 136 sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev ccache libgl1-mesa-dev libxml2-utils xsltproc unzip 137 </pre> 138 139 <p class="note"><strong>Note:</strong> To use SELinux tools for policy 140 analysis, also install the <code>python-networkx</code> package.</p> 141 142 <p class="note"><strong>Note:</strong> If you are using LDAP and want 143 to run ART host tests, also install the <code>libnss-sss:i386</code> 144 package.</p> 145 146 <h3 id="installing-required-packages-ubuntu-1204">Installing required packages 147 (Ubuntu 12.04)</h3> 148 149 <p>You may use Ubuntu 12.04 to build older versions of Android. Version 12.04 150 is not supported on master or recent releases.</p> 151 152 <pre class="devsite-click-to-copy"> 153 <code class="devsite-terminal">sudo apt-get install git gnupg flex bison gperf build-essential zip curl libc6-dev libncurses5-dev:i386 x11proto-core-dev libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-glx:i386 libgl1-mesa-dev g++-multilib mingw32 tofrodos python-markdown libxml2-utils xsltproc zlib1g-dev:i386</code> 154 <code class="devsite-terminal">sudo ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 /usr/lib/i386-linux-gnu/libGL.so</code> 155 </pre> 156 157 <h3 id="installing-required-packages-ubuntu-1004-1110">Installing required 158 packages (Ubuntu 10.04 -- 11.10)</h3> 159 <p>Building on Ubuntu 10.04-11.10 is no longer supported, but may be useful for 160 building older releases of AOSP.</p> 161 162 <pre class="devsite-terminal devsite-click-to-copy"> 163 sudo apt-get install git gnupg flex bison gperf build-essential zip curl zlib1g-dev libc6-dev lib32ncurses5-dev ia32-libs x11proto-core-dev libx11-dev lib32readline5-dev lib32z-dev libgl1-mesa-dev g++-multilib mingw32 tofrodos python-markdown libxml2-utils xsltproc 164 </pre> 165 166 <p>On Ubuntu 10.10:</p> 167 168 <preclass="devsite-terminal devsite-click-to-copy"> 169 sudo ln -s /usr/lib32/mesa/libGL.so.1 /usr/lib32/mesa/libGL.so 170 </pre> 171 172 <p>On Ubuntu 11.10:</p> 173 174 <pre class="devsite-terminal devsite-click-to-copy"> 175 sudo apt-get install libx11-dev:i386 176 </pre> 177 178 <h3 id="configuring-usb-access">Configuring USB Access</h3> 179 180 <p>Under GNU/Linux systems (and specifically under Ubuntu systems), 181 regular users can't directly access USB devices by default. The 182 system needs to be configured to allow such access.</p> 183 <p>The recommended approach is to create a file at 184 <code>/etc/udev/rules.d/51-android.rules</code> (as the root user).</p> 185 186 <p>To do this, run the following command to download the <a 187 href="51-android.txt">51-android.txt</a> file attached to this site, modify 188 it to include your username, and place it in the correct location:</p> 189 190 <pre class="devsite-terminal devsite-click-to-copy"> 191 wget -S -O - http://source.android.com/source/51-android.txt | sed "s/<username>/$USER/" | sudo tee >/dev/null /etc/udev/rules.d/51-android.rules; sudo udevadm control --reload-rules 192 </pre> 193 194 <p>Those new rules take effect the next time a device is plugged in. 195 It might therefore be necessary to unplug the device and plug it 196 back into the computer.</p> 197 198 <h3 id="using-a-separate-output-directory">Using a separate output directory</h3> 199 200 <p>By default, the output of each build is stored in the <code>out/</code> 201 subdirectory of the matching source tree.</p> 202 203 <p>On some machines with multiple storage devices, builds are 204 faster when storing the source files and the output on 205 separate volumes. For additional performance, the output 206 can be stored on a filesystem optimized for speed instead 207 of crash robustness, since all files can be re-generated 208 in case of filesystem corruption.</p> 209 210 <p>To set this up, export the <code>OUT_DIR_COMMON_BASE</code> variable 211 to point to the location where your output directories 212 will be stored.</p> 213 214 <pre class="devsite-terminal devsite-click-to-copy"> 215 export OUT_DIR_COMMON_BASE=<path-to-your-out-directory> 216 </pre> 217 218 <p>The output directory for each separate source tree will be 219 named after the directory holding the source tree.</p> 220 221 <p>For instance, if you have source trees as <code>/source/master1</code> 222 and <code>/source/master2</code> and <code>OUT_DIR_COMMON_BASE</code> is set to 223 <code>/output</code>, the output directories will be <code>/output/master1</code> 224 and <code>/output/master2</code>.</p> 225 226 <p>It's important in that case to not have multiple source 227 trees stored in directories that have the same name, 228 as those would end up sharing an output directory, with 229 unpredictable results.</p> 230 231 <p>This is only supported on Jelly Bean (4.1) and newer, 232 including the <code>master</code> branch.</p> 233 234 <h2 id="setting-up-a-mac-os-x-build-environment">Setting up a Mac OS build 235 environment</h2> 236 237 <p>In a default installation, Mac OS runs on a case-preserving but 238 case-insensitive filesystem. This type of filesystem is not supported by git 239 and will cause some git commands (such as <code>git status</code>) to behave 240 abnormally. Because of this, we recommend that you always work with the AOSP 241 source files on a case-sensitive filesystem. This can be done fairly easily 242 using a disk image, discussed below.</p> 243 244 <p>Once the proper filesystem is available, building the <code>master</code> 245 branch in a modern Mac OS environment is very straightforward. Earlier 246 branches require some additional tools and SDKs.</p> 247 248 <h3 id="creating-a-case-sensitive-disk-image">Creating a case-sensitive disk image</h3> 249 250 <p>You can create a case-sensitive filesystem within your existing Mac OS environment 251 using a disk image. To create the image, launch Disk 252 Utility and select "New Image". A size of 25GB is the minimum to 253 complete the build; larger numbers are more future-proof. Using sparse images 254 saves space while allowing to grow later as the need arises. Be sure to select 255 "case sensitive, journaled" as the volume format.</p> 256 257 <p>You can also create it from a shell with the following command:</p> 258 <pre class="devsite-click-to-copy"> 259 <span class="no-select"># </span>hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 40g ~/android.dmg 260 </pre> 261 262 <p>This will create a <code>.dmg</code> (or possibly a 263 <code>.dmg.sparseimage</code>) file which, once mounted, acts as a drive with 264 the required formatting for Android development.</p> 265 266 <p>If you need a larger volume later, you can also resize the sparse image with 267 the following command:</p> 268 269 <pre class="devsite-click-to-copy"> 270 <span class="no-select"># </span>hdiutil resize -size <new-size-you-want>g ~/android.dmg.sparseimage 271 </pre> 272 273 <p>For a disk image named <code>android.dmg</code> stored in your home 274 directory, you can add helper functions to your <code>~/.bash_profile</code>:</p> 275 276 <ul> 277 <li> 278 To mount the image when you execute <code>mountAndroid</code>:</p> 279 280 <pre class="devsite-click-to-copy"> 281 # mount the android file image 282 function mountAndroid { hdiutil attach ~/android.dmg -mountpoint /Volumes/android; } 283 </pre> 284 285 <p class="note"><strong>Note:</strong> If your system created a 286 <code>.dmg.sparseimage</code> file, replace <code>~/android.dmg</code> with 287 <code>~/android.dmg.sparseimage</code>.</p> 288 </li> 289 290 <li> 291 <p>To unmount it when you execute <code>umountAndroid</code>:</p> 292 <pre class="devsite-click-to-copy"> 293 # unmount the android file image 294 function umountAndroid() { hdiutil detach /Volumes/android; } 295 </pre> 296 </li> 297 </ul> 298 299 <p>Once you've mounted the <code>android</code> volume, you'll do all your work 300 there. You can eject it (unmount it) just like you would with an external 301 drive.</p> 302 303 <h3 id="installing-the-mac-jdk">Installing the JDK</h3> 304 305 <p>See <a href="requirements.html">Requirements</a> for the version of Java to 306 use when developing various versions of Android.</p> 307 308 <h4 id="installing-required-packages">Installing required packages</h4> 309 310 <ol> 311 <li> 312 <p>Install Xcode command line tools with: 313 <pre class="devsite-terminal devsite-click-to-copy"> 314 xcode-select --install 315 </pre> 316 317 <p>For older versions of Mac OS (10.8 or earlier), you need to install Xcode from 318 <a href="http://developer.apple.com/">the Apple developer site</a>. 319 If you are not already registered as an Apple developer, you will have to 320 create an Apple ID in order to download.</p> 321 </li> 322 323 <li> 324 <p>Install MacPorts from <a 325 href="http://www.macports.org/install.php">macports.org</a>.</p> 326 327 <p class="note"><strong>Note:</strong> Make sure that 328 <code>/opt/local/bin</code> appears in your path <strong>before</strong> 329 <code>/usr/bin</code>. If not, please add the following to your 330 <code>~/.bash_profile</code> file:</p> 331 332 <pre class="devsite-click-to-copy"> 333 export PATH=/opt/local/bin:$PATH 334 </pre> 335 336 <p class="note"><strong>Note:</strong> If you do not have a 337 <code>.bash_profile</code> file in your home directory, create one.</p> 338 </li> 339 340 <li> 341 <p>Get make, git, and GPG packages from MacPorts:</p> 342 343 <pre class="devsite-terminal devsite-click-to-copy"> 344 POSIXLY_CORRECT=1 sudo port install gmake libsdl git gnupg 345 </pre> 346 347 <p>If using Mac OS X v10.4, also install bison:</p> 348 <pre class="devsite-terminal devsite-click-to-copy"> 349 POSIXLY_CORRECT=1 sudo port install bison 350 </pre> 351 </li> 352 </ol> 353 354 <h4 id="reverting-from-make-382">Reverting from make 3.82</h4> 355 356 <p>In Android 4.0.x (Ice Cream Sandwich) and earlier, a bug exists in gmake 3.82 357 that prevents android from building. You can install version 3.81 using 358 MacPorts with these steps:</p> 359 360 <ol> 361 <li> 362 <p>Edit <code>/opt/local/etc/macports/sources.conf</code> and add a line that says:</p> 363 <pre class="devsite-click-to-copy"> 364 file:///Users/Shared/dports 365 </pre> 366 367 <p>above the rsync line. Then create this directory:</p> 368 <pre class="devsite-terminal devsite-click-to-copy"> 369 mkdir /Users/Shared/dports 370 </pre> 371 </li> 372 373 <li> 374 <p>In the new <code>dports</code> directory, run:</p> 375 <pre class="devsite-terminal devsite-click-to-copy"> 376 svn co --revision 50980 http://svn.macports.org/repository/macports/trunk/dports/devel/gmake/ devel/gmake/ 377 </pre> 378 </li> 379 380 <li> 381 <p>Create a port index for your new local repository:</p> 382 383 <pre class="devsite-terminal devsite-click-to-copy"> 384 portindex /Users/Shared/dports 385 </pre> 386 </li> 387 388 <li> 389 <p>Install the old version of gmake with:</p> 390 <pre class="devsite-terminal devsite-click-to-copy"> 391 sudo port install gmake @3.81 392 </pre> 393 </li> 394 </ol> 395 396 <h4 id="setting-a-file-descriptor-limit">Setting a file descriptor limit</h4> 397 398 <p>On Mac OS, the default limit on the number of simultaneous file descriptors 399 open is too low and a highly parallel build process may exceed this limit.</p> 400 401 <p>To increase the cap, add the following lines to your <code>~/.bash_profile</code>: </p> 402 <pre class="devsite-click-to-copy"> 403 # set the number of open files to be 1024 404 ulimit -S -n 1024 405 </pre> 406 407 <h2 id="optimizing-a-build-environment">Optimizing a build environment (optional)</h2> 408 409 <h3 id="setting-up-ccache">Setting up ccache</h3> 410 411 <p>You can optionally tell the build to use the ccache compilation tool, which 412 is a compiler cache for C and C++ that can help make builds faster. It 413 is especially useful for build servers and other high-volume production 414 environments. Ccache acts as a compiler cache that can be used to speed up rebuilds. 415 This works very well if you use <code>make clean</code> often, or if you frequently 416 switch between different build products.</p> 417 418 <p class="note"><strong>Note:</strong> If you're instead conducting incremental 419 builds (such as an individual developer rather than a build server), ccache may 420 slow your builds down by making you pay for cache misses.</p> 421 422 <p>To use ccache, issue these commands in the root of the source tree:</p> 423 424 <pre class="devsite-click-to-copy"> 425 <code class="devsite-terminal">export USE_CCACHE=1</code> 426 <code class="devsite-terminal">export CCACHE_DIR=/<path_of_your_choice>/.ccache</code> 427 <code class="devsite-terminal">prebuilts/misc/linux-x86/ccache/ccache -M 50G</code> 428 </pre> 429 430 <p>The suggested cache size is 50-100G.</p> 431 432 <p>Put the following in your <code>.bashrc</code> (or equivalent):</p> 433 434 <pre class="devsite-click-to-copy"> 435 export USE_CCACHE=1 436 </pre> 437 438 <p>By default the cache will be stored in <code>~/.ccache</code>. 439 If your home directory is on NFS or some other non-local filesystem, 440 you will want to specify the directory in your <code>.bashrc</code> file too.</p> 441 442 <p>On Mac OS, you should replace <code>linux-x86</code> with <code>darwin-x86</code>:</p> 443 444 <pre class="devsite-click-to-copy"> 445 prebuilts/misc/darwin-x86/ccache/ccache -M 50G 446 </pre> 447 448 <p>When building Ice Cream Sandwich (4.0.x) or older, ccache is in 449 a different location:</p> 450 451 <pre class="devsite-click-to-copy"> 452 prebuilt/linux-x86/ccache/ccache -M 50G 453 </pre> 454 455 <p>This setting is stored in the CCACHE_DIR and is persistent.</p> 456 457 <p>On Linux, you can watch ccache being used by doing the following:</p> 458 459 <pre class="devsite-terminal devsite-click-to-copy"> 460 watch -n1 -d prebuilts/misc/linux-x86/ccache/ccache -s 461 </pre> 462 463 <h2 id="next-download-the-source">Next: Download the source</h2> 464 465 <p>Your build environment is good to go! Proceed to <a 466 href="downloading.html">downloading the source</a>.</p> 467 468 </body> 469 </html> 470