您的位置:首页 > 移动开发 > Android开发

在Ubuntu上下载、编译和安装Android最新源代码

2012-04-05 15:19 585 查看
此文大部分来至于转载,其中部分在本人实验下做了修改。

官网内容:


Initializing a Build Environment

The "Getting Started" section describes how to set up your local work environment, how to use Repo to get the Android files, and how to build the files on your machine. To build the Android source files, you will need to use Linux or Mac OS. Building under
Windows is not currently supported.

Note: The source download is approximately 6GB in size. You will need 25GB free to complete a single build, and up to 80GB (or more) for a full set of builds.

For an overview of the entire code-review and code-update process, see Life of a Patch.


Setting up a Linux build environment

The Android build is routinely tested in house on recent versions of Ubuntu LTS (10.04), but most distributions should have the required build tools available. Reports of successes or failures on other distributions are welcome.

Note: It is also possible to build Android in a virtual machine. If you are running Linux in a virtual machine, you will need at least 16GB of RAM/swap and 30GB or more of disk space in order to build the Android tree.

In general you will need:

Python 2.4 -- 2.7, which you can download from python.org.

JDK 6 if you wish to build Gingerbread or newer; JDK 5 for Froyo or older. You can download both from java.sun.com.

Git 1.7 or newer. You can find it at git-scm.com.

Detailed instructions for Ubuntu 10.04+ follow.


Installing the JDK

The Sun JDK is no longer in Ubuntu's main package repository. In order to download it, you need to add the appropriate repository and indicate to the system which JDK should be used.

Java 6: for Gingerbread and newer
[code]$ sudo add-apt-repository "deb http://archive.canonical.com/ lucid partner"
$ sudo apt-get update
$ sudo apt-get install sun-java6-jdk


Java 5: for Froyo and older
[code]$ sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu hardy main multiverse"
$ sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu hardy-updates main multiverse"
$ sudo apt-get update
$ sudo apt-get install sun-java5-jdk


Installing required packages

64-bit (recommended)
[code]$ sudo apt-get install git-core 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


On newer versions of Ubuntu such as 11.10 you may need to do the following:
[code]$ sudo ln -s /usr/lib/i386-linux-gnu/libX11.so.6 /usr/lib/i386-linux-gnu/libX11.so


32-bit (experimental)
[code]$ sudo apt-get install git-core gnupg flex bison gperf build-essential \
  zip curl zlib1g-dev libc6-dev libncurses5-dev x11proto-core-dev \
  libx11-dev libreadline6-dev libgl1-mesa-dev tofrodos python-markdown \
  libxml2-utils


Configuring USB Access

Under GNU/linux systems (and specifically under Ubuntu systems), regular users can't directly access USB devices by default. The system needs to be configured to allow such access.

The recommended approach is to create a file
/etc/udev/rules.d/51-android.rules
(as the root user) and to copy the following lines in it.must be replaced by the actual username of the user who
is authorized to access the phones over USB.
[code]# adb protocol on passion (Nexus One)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e12", MODE="0600", OWNER="<username>"
# fastboot protocol on passion (Nexus One)
SUBSYSTEM=="usb", ATTR{idVendor}=="0bb4", ATTR{idProduct}=="0fff", MODE="0600", OWNER="<username>"
# adb protocol on crespo/crespo4g (Nexus S)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e22", MODE="0600", OWNER="<username>"
# fastboot protocol on crespo/crespo4g (Nexus S)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e20", MODE="0600", OWNER="<username>"
# adb protocol on maguro (Galaxy Nexus)
SUBSYSTEM=="usb", ATTR{idVendor}=="04e8", ATTR{idProduct}=="6860", MODE="0600", OWNER="<username>"
# fastboot protocol on maguro (Galaxy Nexus)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e30", MODE="0600", OWNER="<username>"


Those new rules take effect the next time a device is plugged in. It might therefore be necessary to unplug the device and plug it back into the computer.

This is known to work on both Ubuntu Hardy Heron (8.04.x LTS) and Lucid Lynx (10.04.x LTS). Other versions of Ubuntu or other variants of GNU/linux might require different configurations.


Setting up a Mac OS X build environment

To build the Android files in a Mac OS environment, you need an Intel/x86 machine running MacOS 10.6 (Snow Leopard).

Android must be built on a case-sensitive file system because the sources contain files that differ only in case. We recommend that you build Android on a partition that has been formatted with the journaled file system HFS+. HFS+ is required to successfully
build Mac OS applications such as the Android Emulator for OS X.


Creating a case sensitive disk image

If you want to avoid partitioning/formatting your hard drive, you can use a case-sensitive disk image instead. To create the image, launch Disk Utility and select "New Image". A size of 25GB is the minimum to complete the build, larger numbers are more future-proof.
Using sparse images saves space while allowing to grow later as the need arises. Be sure to select "case sensitive, journaled" as the volume format.

You can also create it from a shell with the following command:
[code]# hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 40g ~/android.dmg


This will create a .dmg (or possibly a .dmg.sparsefile) file which, once mounted, acts as a drive with the required formatting for Android development. For a disk image named "android.dmg" stored in your home directory, you can add the following to your
~/.bash_profile
to
mount the image when you execute "mountAndroid":
[code]# mount the android file image
function mountAndroid { hdiutil attach ~/android.dmg -mountpoint /Volumes/android; }


Once mounted, you'll do all your work in the "android" volume. You can eject it (unmount it) just like you would with an external drive.


Installing required packages

Install XCode from the Apple developer site. We recommend version 3.1.4 or newer, i.e. gcc 4.2. Version 4.x could cause difficulties. If you are not already registered as an Apple developer,
you will have to create an Apple ID in order to download.

Install MacPorts from macports.org.

Note: Make sure that
/opt/local/bin
appears in your path BEFORE
/usr/bin
. If not, add
[code]export PATH=/opt/local/bin:$PATH


to your
~/.bash_profile
.

Get make, git, and GPG packages from MacPorts:
[code]$ POSIXLY_CORRECT=1 sudo port install gmake libsdl git-core gnupg


If using Mac OS 10.4, also install bison:
[code]$ POSIXLY_CORRECT=1 sudo port install bison



Reverting from make 3.82

There is a bug in gmake 3.82 that prevents android from building. You can install version 3.81 using MacPorts by taking the following steps:

Edit
/opt/local/etc/macports/sources.conf
and add a line that says
[code]file:///Users/Shared/dports


above the rsync line. Then create this directory:
[code]$ mkdir /Users/Shared/dports


In the new
dports
directory, run
[code]$ svn co --revision 50980 http://svn.macports.org/repository/macports/trunk/dports/devel/gmake/ devel/gmake/


Create a port index for your new local repository:
[code]$ portindex /Users/Shared/dports


Finally, install the old version of gmake with
[code]$ sudo port install gmake @3.81



Setting a file descriptor limit

On MacOS the default limit on the number of simultaneous file descriptors open is too low and a highly parallel build process may exceed this limit.

To increase the cap, add the following lines to your
~/.bash_profile
:
[code]# set the number of open files to be 1024
ulimit -S -n 1024


Downloading the Source Tree


Installing Repo

Repo is a tool that makes it easier to work with Git in the context of Android. For more information about Repo, see Version Control.

To install, initialize, and configure Repo, follow these steps:

Make sure you have a bin/ directory in your home directory, and that it is included in your path:
[code]$ mkdir ~/bin
$ PATH=~/bin:$PATH


Download the Repo script and ensure it is executable:
[code]$ curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > ~/bin/repo
$ chmod a+x ~/bin/repo


The SHA-1 checksum for repo is e1fd3bef059d152edf4d0522590725d317bc637f


Initializing a Repo client

After installing Repo, set up your client to access the android source repository:

Create an empty directory to hold your working files. If you're using MacOS, this has to be on a case-sensitive filesystem. Give it any name you like:
[code]$ mkdir WORKING_DIRECTORY
$ cd WORKING_DIRECTORY


Run
repo init
to bring down the latest version of Repo with all its most recent bug fixes. You must specify a URL for the manifest, which specifies where the various repositories included in
the Android source will be placed within your working directory.
[code]$ repo init -u https://android.googlesource.com/platform/manifest


To check out a branch other than "master", specify it with -b:
[code]$ repo init -u https://android.googlesource.com/platform/manifest-b android-4.0.1_r1


When prompted, please configure Repo with your real name and email address. To use the Gerrit code-review tool, you will need an email address that is connected with a registered Google account.
Make sure this is a live address at which you can receive messages. The name that you provide here will show up in attributions for your code submissions.

A successful initialization will end with a message stating that Repo is initialized in your working directory. Your client directory should now contain a
.repo
directory where files such as
the manifest will be kept.


Getting the files

To pull down files to your working directory from the repositories as specified in the default manifest, run
[code]$ repo sync


The Android source files will be located in your working directory under their project names. The initial sync operation will take an hour or more to complete. For more about
repo sync
and other
Repo commands, see Version Control.


Verifying Git Tags

Load the following public key into your GnuPG key database. The key is used to sign annotated tags that represent releases.
[code]$ gpg --import


Copy and paste the key(s) below, then enter EOF (Ctrl-D) to end the input and process the keys.
[code]-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

mQGiBEnnWD4RBACt9/h4v9xnnGDou13y3dvOx6/t43LPPIxeJ8eX9WB+8LLuROSV
lFhpHawsVAcFlmi7f7jdSRF+OvtZL9ShPKdLfwBJMNkU66/TZmPewS4m782ndtw7
8tR1cXb197Ob8kOfQB3A9yk2XZ4ei4ZC3i6wVdqHLRxABdncwu5hOF9KXwCgkxMD
u4PVgChaAJzTYJ1EG+UYBIUEAJmfearb0qRAN7dEoff0FeXsEaUA6U90sEoVks0Z
wNj96SA8BL+a1OoEUUfpMhiHyLuQSftxisJxTh+2QclzDviDyaTrkANjdYY7p2cq
/HMdOY7LJlHaqtXmZxXjjtw5Uc2QG8UY8aziU3IE9nTjSwCXeJnuyvoizl9/I1S5
jU5SA/9WwIps4SC84ielIXiGWEqq6i6/sk4I9q1YemZF2XVVKnmI1F4iCMtNKsR4
MGSa1gA8s4iQbsKNWPgp7M3a51JCVCu6l/8zTpA+uUGapw4tWCp4o0dpIvDPBEa9
b/aF/ygcR8mh5hgUfpF9IpXdknOsbKCvM9lSSfRciETykZc4wrRCVGhlIEFuZHJv
aWQgT3BlbiBTb3VyY2UgUHJvamVjdCA8aW5pdGlhbC1jb250cmlidXRpb25AYW5k
cm9pZC5jb20+iGAEExECACAFAknnWD4CGwMGCwkIBwMCBBUCCAMEFgIDAQIeAQIX
gAAKCRDorT+BmrEOeNr+AJ42Xy6tEW7r3KzrJxnRX8mij9z8tgCdFfQYiHpYngkI
2t09Ed+9Bm4gmEO5Ag0ESedYRBAIAKVW1JcMBWvV/0Bo9WiByJ9WJ5swMN36/vAl
QN4mWRhfzDOk/Rosdb0csAO/l8Kz0gKQPOfObtyYjvI8JMC3rmi+LIvSUT9806Up
hisyEmmHv6U8gUb/xHLIanXGxwhYzjgeuAXVCsv+EvoPIHbY4L/KvP5x+oCJIDbk
C2b1TvVk9PryzmE4BPIQL/NtgR1oLWm/uWR9zRUFtBnE411aMAN3qnAHBBMZzKMX
LWBGWE0znfRrnczI5p49i2YZJAjyX1P2WzmScK49CV82dzLo71MnrF6fj+Udtb5+
OgTg7Cow+8PRaTkJEW5Y2JIZpnRUq0CYxAmHYX79EMKHDSThf/8AAwUIAJPWsB/M
pK+KMs/s3r6nJrnYLTfdZhtmQXimpoDMJg1zxmL8UfNUKiQZ6esoAWtDgpqt7Y7s
KZ8laHRARonte394hidZzM5nb6hQvpPjt2OlPRsyqVxw4c/KsjADtAuKW9/d8phb
N8bTyOJo856qg4oOEzKG9eeF7oaZTYBy33BTL0408sEBxiMior6b8LrZrAhkqDjA
vUXRwm/fFKgpsOysxC6xi553CxBUCH2omNV6Ka1LNMwzSp9ILz8jEGqmUtkBszwo
G1S8fXgE0Lq3cdDM/GJ4QXP/p6LiwNF99faDMTV3+2SAOGvytOX6KjKVzKOSsfJQ
hN0DlsIw8hqJc0WISQQYEQIACQUCSedYRAIbDAAKCRDorT+BmrEOeCUOAJ9qmR0l
EXzeoxcdoafxqf6gZlJZlACgkWF7wi2YLW3Oa+jv2QSTlrx4KLM=
=Wi5D
-----END PGP PUBLIC KEY BLOCK-----


After importing the keys, you can verify any tag with
[code]$ git tag -v TAG_NAME


Building the System

The basic sequence of build commands is as follows:


Initialize

Initialize the environment with the
envsetup.sh
script. Note that replacing "source" with a single dot saves a few characters, and the short form is more commonly used in documentation.
[code]$ source build/envsetup.sh


or
[code]$ . build/envsetup.sh


Choose a Target

Choose which target to build with
lunch
. The exact configuration can be passed as an argument, e.g.
[code]$ lunch full-eng


The example above refers to a complete build for the emulator, with all debugging enabled.

If run with no arguments
lunch
will prompt you to choose a target from the menu.

All build targets take the form BUILD-BUILDTYPE, where the BUILD is a codename referring to the particular feature combination:
Build nameDeviceNotes
fullemulatorfully configured with all languages, apps, input methods
full_maguromaguro
full
build running on Galaxy Nexus GSM/HSPA+ ("maguro")
full_pandapanda
full
build running on PandaBoard ("panda")
and the BUILDTYPE is one of the following:
BuildtypeUse
userlimited access; suited for production
userdebuglike "user" but with root access and debuggability; preferred for debugging
engdevelopment configuration with additional debugging tools
For more information about building for and running on actual hardware, see Building for devices


Build the Code

Build everything with
make
. GNU make can handle parallel tasks with a
-jN
argument, and it's common to use a number of tasks
N that's between 1 and 2 times the number of hardware threads on the computer being used for the build. E.g. on a dual-E5520 machine (2 CPUs, 4 cores per CPU, 2 threads per core), the fastest builds are made with commands between
make
 -j16
and
make -j32
.
[code]$ make -j4


Run It!

You can either run your build on an emulator or flash it on a device. Please note that you have already selected your build target with
lunch
, and it is unlikely at best to run on a different
target than it was built for.


Flash a Device

To flash a device, you will need to use
fastboot
, which should be included in your path after a successful build. Place the device in fastboot mode either manually by holding the appropriate
key combination at boot, or from the shell with
[code]$ adb reboot bootloader


Once the device is in fastboot mode, run
[code]$ fastboot flashall -w


The
-w
option wipes the
/data
partition on the device; this is useful for your first time flashing a particular device, but
is otherwise unnecessary.

For more information about building for and running on actual hardware, see Building for devices


Emulate an Android Device

The emulator is added to your path automatically by the build process. To run the emulator, type
[code]$ emulator


Using ccache

ccache is a compiler cache for C and C++ that can help make builds faster. In the root of the source tree, do the following:
[code]$ export USE_CCACHE=1
$ export CCACHE_DIR=/<path_of_your_choice>/.ccache
$ prebuilt/linux-x86/ccache/ccache -M 20G


You can watch ccache being used by doing the following:
[code]$ watch -n1 -d prebuilt/linux-x86/ccache/ccache -s


On OSX, you should replace
linux-x86
with
darwin-x86
.


Troubleshooting Common Build Errors


Wrong Java Version

If you are attempting to build froyo or earlier with Java 1.6, or gingerbread or later with Java 1.5,
make
will abort with a message such as
[code]************************************************************
You are attempting to build with the incorrect version
of java.

Your version is: WRONG_VERSION.
The correct version is: RIGHT_VERSION.

Please follow the machine setup instructions at
    http://source.android.com/download ************************************************************


This may be caused by

failing to install the correct JDK as specified on the Initializing page. Building Android requires Sun JDK 5 or 6 depending on which release you are building.

another JDK that you previously installed appearing in your path. You can remove the offending JDK from your path with:
[code]$ export PATH=${PATH/\/path\/to\/jdk\/dir:/}



Python Version 3

Repo is built on particular functionality from Python 2.x and is unfortunately incompatible with Python 3. In order to use repo, please install Python 2.x:
[code]$ apt-get install python


Gmake Version 3.82

There is a bug in
make
version 3.82 on Mac OS that prevents building Android.

TODO: what the error looks like with GNU make 3.82 on older builds that don't explicitly detect it.

Follow the instructions on the Initializing page for reverting GNU make from 3.82 to 3.81.


Case Insensitive Filesystem

If you are building on an HFS filesystem on Mac OS X, you may encounter an error such as
[code]************************************************************
You are building on a case-insensitive filesystem.
Please move your source tree to a case-sensitive filesystem.
************************************************************


Please follow the instructions on the Initializing page for creating a case-sensitive disk image.


No USB Permission

On most Linux systems, unprivileged users cannot access USB ports by default. If you see a permission denied error, follow the instructions on the Initializing page
for configuring USB access.

If adb was already running and cannot connect to the device after getting those rules set up, it can be killed with
adb kill-server
. That will cause adb to restart with the new configuration.


Building for devices

This page complements the main page about Building with information that is specific to individual devices.

The only supported phone with the current release is the GSM/HSPA+ Galaxy Nexus, a.k.a. "maguro". GSM/HSPA+ Galaxy Nexus is currently the recommended device to use with the Android Open-Source Project.

In addition, PandaBoard a.k.a. "panda" is supported in the master branch only, but is currently considered experimental. The specific details to use a PandaBoard with the Android Open-Source
Project are in the file
device/ti/panda/README
in the source tree.

Nexus S, a.k.a. "crespo", and Nexus S 4G, a.k.a. "crespo4g", are supported with gingerbread, but can't currently be used with newer versions of the Android Open-Source Project.

Nexus One a.k.a. "passion" is obsolete, was experimental in gingerbread and unsupported, and can't be used with newer versions of the Android Open-Source Project.

Android Developer Phones (ADP1 and ADP2, a.k.a. "dream" and "sapphire") are obsolete, were experimental and unsupported in froyo, and can't be used with newer versions of the Android Open-Source Project.


Building fastboot and adb

If you don't already have those tools, fastboot and adb can be built with the regular build system. Follow the instructions on the page about building, and replace the
main
make
command with
[code]$ make fastboot adb


Booting into fastboot mode

During a cold boot, the following key combinations can be used to boot into fastboot mode, which is a mode in the bootloader that can be used to flash the devices:
DeviceKeys
maguroPress and hold both Volume Up and Volume Down, then press and hold Power
crespoPress and hold Volume Up, then press and hold Power
crespo4gPress and hold Volume Up, then press and hold Power
passionPress and hold the trackball, then press Power
sapphirePress and hold Back, then press Power
dreamPress and hold Back, then press Power
Also, on devices running froyo or later where adb is enabled, the command
adb reboot bootloader
can be used to reboot from Android directly into the bootloader with no key combinations.


Unlocking the bootloader

It's only possible to flash a custom system if the bootloader allows it.

This is the default setup on ADP1 and ADP2.

On Nexus One, Nexus S, Nexus S 4G, and Galaxy Nexus, the bootloader is locked by default. With the device in fastboot mode, the bootloader is unlocked with
[code]$ fastboot oem unlock


The procedure must be confirmed on-screen, and deletes the user data for privacy reasons. It only needs to be run once.

On Nexus One, the operation voids the warranty and is irreversible.

On Nexus S, Nexus S 4G, and Galaxy Nexus, the bootloader can be locked back with
[code]$ fastboot oem lock


Obtaining proprietary binaries

Starting with IceCreamSandwich, the Android Open-Source Project can't be used from pure source code only, and requires additional hardware-related proprietary libraries to run, specifically for hardware graphics acceleration.

Official binaries for Nexus S, Nexus S 4G, Galaxy Nexus, and PandaBoard can be downloaded from Google's Nexus driver page, which add access to additional hardware capabilities
with non-Open-Source code.

There are no official binaries for Nexus One, ADP2 or ADP1.


Extracting the proprietary binaries

Each set of binaries comes as a self-extracting script in a compressed archive. After uncompressing each archive, run the included self-extracting script from the root of the source tree, confirm that you agree to the terms of the enclosed license agreement,
and the binaries and their matching makefiles will get installed in the
vendor/
hierarchy of the source tree.

There's an additional step on Nexus S 4G. Build the signapk tool with
[code]$ make signapk


Then reassemble the proprietary applicatons with
[code]$ vendor/samsung/crespo4g/reassemble-apks.sh


Cleaning up when adding proprietary binaries

In order to make sure that the newly installed binaries are properly taken into account after being extracted, the existing output of any previous build needs to be deleted with
[code]$ make clobber


Picking and building the configuration that matches a device

The steps to configure and build the Android Open-Source Project are described in the page about Building.

The recommended builds for the various devices are available through the lunch menu, accessed when running the
lunch
command with no arguments:
DeviceBranchBuild configuration
maguroandroid-4.0.1_r1full_maguro-userdebug
pandamasterfull_panda-eng
crespoandroid-2.3.6_r1full_crespo-userdebug
crespo4gandroid-2.3.7_r1full_crespo4g-userdebug
passionandroid-2.3.6_r1full_passion-userdebug
sapphireandroid-2.2.2_r1full_sapphire-userdebug
dreamandroid-2.2.2_r1full_dream-userdebug


Flashing a device

Set the device in fastboot mode if necessary (see above).

Because user data is typically incompatible between builds of Android, it's typically better to delete it when flashing a new system.
[code]$ fastboot erase cache
$ fastboot erase userdata


An entire Android system can be flashed in a single command: this writes the boot, recovery and system partitions together after verifying that the system being flashed is compatible with the installed bootloader and radio, and reboots the system.
[code]$ fastboot flashall


On maguro, panda, crespo, crespo4g, sapphire and dream (but not on passion), the commands above can be replaced with a single command
[code]$ fastboot -w flashall


Nexus S, Nexus S 4G and Galaxy Nexus Bootloader and Cell Radio compatibility

On Nexus S, Nexus S 4G, and Galaxy Nexus, each version of Android has only been thoroughly tested with on specific version of the underlying bootloader and cell radio software. However, no compatibility issues are expected when running newer systems with older
bootloaders and radio images according to the following tables.

Nexus S (worldwide version "XX"):
Android VersionPreferred BootloaderPreferred RadioAlso possible
2.3 (GRH55)I9020XXJK1I9020XXJK8
2.3.1 (GRH78)I9020XXJK1I9020XXJK8
2.3.2 (GRH78C)I9020XXJK1I9020XXJK8
2.3.3 (GRI40)I9020XXKA3I9020XXKB1All previous versions
2.3.4 (GRJ22)I9020XXKA3I9020XXKD1All previous versions
2.3.5 (GRJ90)I9020XXKA3I9020XXKF1All previous versions
2.3.6 (GRK39F)I9020XXKA3I9020XXKF1All previous versions
Nexus S (850MHz version "UC"):
Android VersionPreferred BootloaderPreferred RadioAlso possible
2.3.3 (GRI54)I9020XXKA3I9020UCKB2
2.3.4 (GRJ22)I9020XXKA3I9020UCKD1All previous versions
2.3.5 (GRJ90)I9020XXKA3I9020UCKF1All previous versions
2.3.6 (GRK39C)I9020XXKA3I9020UCKF1All previous versions
2.3.6 (GRK39F)I9020XXKA3I9020UCKF1All previous versions
Nexus S (Korea version "KR"):
Android VersionPreferred BootloaderPreferred RadioAlso possible
2.3.3 (GRI54)I9020XXKA3I9020KRKB3
2.3.4 (GRJ22)I9020XXKA3M200KRKC1All previous versions
2.3.5 (GRJ90)I9020XXKA3M200KRKC1All previous versions
2.3.6 (GRK39F)I9020XXKA3M200KRKC1All previous versions
Nexus S 4G:
Android VersionPreferred BootloaderPreferred RadioAlso possible
2.3.4 (GRJ06D)D720SPRKC5D720SPRKC9
2.3.4 (GRJ22)D720SPRKC5D720SPRKD8All previous versions
2.3.5 (GRJ90)D720SPRKC5D720SPRKE5All previous versions
2.3.7 (GWK74)D720SPRKE1D720SPRKH1 (*)All previous versions
Galaxy Nexus (GSM/HSPA+):
Android VersionPreferred BootloaderPreferred RadioAlso possible
4.0.1 (ITL41D)PRIMEKJ10I9250XXKK1
If you're building a new version of Android, if your Nexus S, Nexus S 4G or Galaxy Nexus has an older bootloader and radio image that is marked as being also possible in the table above but is not recognized by fastboot, you can locally delete the
version-bootloader
and
version-baseband
lines
in
device/samsung/crespo/board-info.txt
or
device/samsung/crespo4g/board-info.txt
or
device/samsung/maguro/board-info.txt


(*) As a note, radio version D720SPRKH1 for Nexus S 4G sometimes erroneously reports version D720SPRKE1. If this is the case for your Nexus S 4G, you can locally modify the version-baseband line in
device/samsung/crespo4g/board-info.txt
accordingly.

一. 环境准备。

1. 磁盘空间预留20G左右,内存3G,因为一边要跑主机,一边要跑虚拟机,内存要求还是比较高的,这样才会比较流畅。

2. 安装VMWare 7.1.4。

3. 安装好VMWare后,接下来就安装Ubuntu系统了。我选择目前最新的版本ubuntu-11.04-alternate-i386,从网上查到的资料说,要编译Android源代码,Ubuntu的最低版本是8.04。下载好后,安装时采用一直默认安装即可。

4. 安装Git工具。Android源代码采用Git工具来管理,与SVN相比,这是一种分布式的源代码管理工具,而SVN是集中式的源代码管理工具。要安装Git工具,在Ubuntu上执行以下命令即可:

USER-NAME@MACHINE-NAME:~$ sudo apt-get install git-core gnupg

5. 安装Java SDK。在Ubuntu上执行以下命令:

USER-NAME@MACHINE-NAME:~$
sudo add-apt-repository ppa:ferramroberto/java



USER-NAME@MACHINE-NAME:~$



sudo apt-get update

USER-NAME@MACHINE-NAME:~$
sudo apt-get install sun-java6-jre sun-java6-plugin

USER-NAME@MACHINE-NAME:~$
sudo apt-get install sun-java6-jdk





6. 依赖的其它包。在Ubuntu上执行以下命令:


USER-NAME@MACHINE-NAME:~$ sudo apt-get install flex bison gperf libsdl-dev libesd0-dev libwxgtk2.6-dev build-essential zip curl

7. 调试工具。在Ubuntu上执行以下命令:


USER-NAME@MACHINE-NAME:~$ sudo apt-get install valgrind



二. 下载Android源代码工程

1.安装git

sudo apt-get install git-core

2.安装curl

sudo apt-get install git-core curl

3.安装repo, 通过curl下载repo

curl http://git-repo.googlecode.com/files/repo-1.12 > ~/bin/repo

4.给repo可执行权限

chmod a+x ~/bin/repo

5.修改~/bin/repo文件将第五行

REPO_URL='https://code.google.com/p/git-repo/'

改为

REPO_URL='http://code.google.com/p/git-repo/'

否则会出现error:Failed connect to code.google.com:443; Operation now in progress while accessing https://code.google.com/p/git-repo//info/refs
6.然后向PATH中添加bin路径:

export PATH=$PATH:~/bin

7.新建一个目录,然后进入该目录。

mkdir myAndroid

cd myAndroid

8.初始化版本库

在源码目录下,初始化版本库。

输入 repo init -u http://android.git.kernel.org/platform/manifest.git 将同步当前最新的Android代码

输入 repo init -u http://android.git.kernel.org/platform/manifest.git -b 版本标识 将同步Android特定版本的源代码分支,其中版本标识可为 eclair,froyo,gingerbread等,分别对应Android 2.1,2.2,2.3等版本。

输入以上命令后,开始进行版本库初始化工作,结束时可能要求配置用户名和邮箱,随意设置即可。

初始化完毕后,系统在源码目录下建立 .repo 目录,用于保存相关初始化信息。

执行repo init -u git://Android.git.linaro.org/platform/manifest.git -b android-2.3.4_r1

这一步大概有一分多钟就完成了。

9.找到myAndroid目录中的.repo下面的manifest.xml文件,该文件只是一个链接,实际上是manifest目录下面的default.xml文件,将

fetch="git://Android.git.kernel.org/"

改为

fetch="git://Android.git.linaro.org/"

10.执行repo sync

在源码目录下,输入 repo sync , 即开始同步Android源代码。

鉴于网络不稳定带来的随机中断问题,建议使用循环指令,使在源码同步中断后自动重试。在源码目录下新建文件 dl.sh ,该文件内容为:

#!/bin/sh

repo sync

while [ $? -ne 0 ]

do

repo sync

done

之后,通过输入 ./dl.sh 即可执行包含自动重试机制的源码同步过程。

三. 编译Android源代码。

开始正式编译,在源码目录下make即可。

make

我完全不知道跑了个啥,只知道超级卡。完成后产生的out文件夹有3.9G。

镜像生成在out/target/product/generic下:android源码编译后得到system.img,ramdisk.img,userdata.img映像文件。其中, ramdisk.img是emulator的文件系统,system.img包括了主要的包、库等文件,userdata.img包括了一些用户数据,emulator加载这3个映像文件后,会把 system和 userdata分别加载到 ramdisk文件系统中的system和 userdata目录下。

当然编译是件很痛苦的事情:

错误1:

************************************************************

You are attempting to build on a 32-bit system.

Only 64-bit build environments are supported beyond froyo/2.2.

************************************************************

解决:

需要进行如下修改即可,将

./external/clearsilver/cgi/Android.mk

./external/clearsilver/java-jni/Android.mk

./external/clearsilver/util/Android.mk

./external/clearsilver/cs/Android.mk

四个文件中的

LOCAL_CFLAGS += -m64

LOCAL_LDFLAGS += -m64

注释掉,或者将“64”换成“32”

LOCAL_CFLAGS += -m32

LOCAL_LDFLAGS += -m32

然后,将./build/core/main.mk 中的

ifneq (64,$(findstring 64,$(build_arch)))

改为:

ifneq (i686,$(findstring i686,$(build_arch)))

错误2:

host C++: libutils <= frameworks/base/libs/utils/RefBase.cpp

frameworks/base/libs/utils/RefBase.cpp: In member function ‘void android::RefBase::weakref_type::trackMe(bool, bool)’:

frameworks/base/libs/utils/RefBase.cpp:483:67: error: passing ‘const android::RefBase::weakref_impl’ as ‘this’ argument of ‘void android::RefBase::weakref_impl::trackMe(bool, bool)’ discards qualifiers [-fpermissive]

make: *** [out/host/linux-x86/obj/STATIC_LIBRARIES/libutils_intermediates/RefBase.o] 错误 1

解决:

gedit frameworks/base/libs/utils/Android.mk

Change the line:

LOCAL_CFLAGS += -DLIBUTILS_NATIVE=1 $(TOOL_CFLAGS)

To:

LOCAL_CFLAGS += -DLIBUTILS_NATIVE=1 $(TOOL_CFLAGS) –fpermissive

错误3:

host Executable: aapt (out/host/linux-x86/obj/EXECUTABLES/aapt_intermediates/aapt)

out/host/linux-x86/obj/STATIC_LIBRARIES/libcutils_intermediates/libcutils.a(threads.o): In function `thread_store_get':

/home/leno/works/android_dev/bin/system/core/libcutils/threads.c:27: undefined reference to `pthread_getspecific'

out/host/linux-x86/obj/STATIC_LIBRARIES/libcutils_intermediates/libcutils.a(threads.o): In function `thread_store_set':

/home/leno/works/android_dev/bin/system/core/libcutils/threads.c:36: undefined reference to `pthread_key_create'

/home/leno/works/android_dev/bin/system/core/libcutils/threads.c:44: undefined reference to `pthread_setspecific'

collect2: ld returned 1 exit status

make: *** [out/host/linux-x86/obj/EXECUTABLES/aapt_intermediates/aapt] 错误 1

解决:

打开 frameworks/base/tools/aapt/Android.mk

然后打开文件Android.mk,编辑下面一行:

ifeq ($(HOST_OS),linux)

#LOCAL_LDLIBS += -lrt       把这行注释掉,改为下面一行。

LOCAL_LDLIBS += -lrt -lpthread

endif

错误4:

target Dex: core

#

# An unexpected error has been detected by HotSpot Virtual Machine:

#

# SIGSEGV (0xb) at pc=0x4003d848, pid=7668, tid=2889534320

#

# Java VM: Java HotSpot(TM) Client VM (1.5.0_22-b03 mixed mode)

# Problematic frame:

# C [libpthread.so.0+0xa848] pthread_cond_timedwait+0x168

#

# An error report file with more information is saved as hs_err_pid7668.log

#

# If you would like to submit a bug report, please visit:

# http://java.sun.com/webapps/bugreport/crash.jsp
#

make: *** [out/target/common/obj/J***A_LIBRARIES/core_intermediates/classes.dex] 已放弃 (core dumped)

解决:

虚拟机给的内存512太小,给个1G试试。然后在本机里打开任务管理器,找到虚拟机进程,优先级设置为实时。给他最多东西。

错误 5:

out/host/linux-x86/obj/STATIC_LIBRARIES/libcutils_intermediates/libcutils.a(threads.o): In function `thread_store_get':

/home/leno/works/android_dev/bin/system/core/libcutils/threads.c:27: undefined reference to `pthread_getspecific'

out/host/linux-x86/obj/STATIC_LIBRARIES/libcutils_intermediates/libcutils.a(threads.o): In function `thread_store_set':

/home/leno/works/android_dev/bin/system/core/libcutils/threads.c:36: undefined reference to `pthread_key_create'

/home/leno/works/android_dev/bin/system/core/libcutils/threads.c:44: undefined reference to `pthread_setspecific'

collect2: ld returned 1 exit status

make: *** [out/host/linux-x86/obj/EXECUTABLES/localize_intermediates/localize] 错误 1

解决:类似问题3

修改./framework/base/tools/localize/Android.mk文件

ifeq ($(HOST_OS),linux)

#LOCAL_LDLIBS += -lrt       把这行注释掉,改为下面一行。

LOCAL_LDLIBS += -lrt -lpthread

endif

错误 6:Undefined reference to `__dso_handle'

external/stlport/src/monetary.cpp:39: undefined reference to `__dso_handle'

out/target/product/vm/obj/SHARED_LIBRARIES/libstlport_intermediates/src/locale.o: In function `__static_initialization_and_destruction_0':

external/stlport/src/locale.cpp:29: undefined reference to `__dso_handle'

out/target/product/vm/obj/SHARED_LIBRARIES/libstlport_intermediates/src/locale_impl.o: In function `__static_initialization_and_destruction_0':

external/stlport/src/locale_impl.cpp:31: undefined reference to `__dso_handle'

out/target/product/vm/obj/SHARED_LIBRARIES/libstlport_intermediates/src/locale_impl.o: In function `std::_Locale_impl::make_classic_locale()':

external/stlport/src/locale_impl.cpp:670: undefined reference to `__dso_handle'

external/stlport/src/locale_impl.cpp:667: undefined reference to `__dso_handle'

out/target/product/vm/obj/SHARED_LIBRARIES/libstlport_intermediates/src/locale_impl.o:external/stlport/src/locale_impl.cpp:604: more undefined

references to `__dso_handle' follow

collect2: ld returned 1 exit status

修改external/stlport/dll_main.cpp,加入以下声明:

extern "C" {

void * __dso_handle = 0;

}

四. 编译SDK,这一步是可选的。

执行$sudo make PRODUCT-sdk-sdk命令,生成对应于该版本源代码的用于生产环境的sdk。

编译的SDK版本,实际位置是./out/host/linux-x86/sdk/android-sdk_eng.root_linux-x86。

因为它比较常用,我们给它高优先级:

update-alternatives --install /usr/bin/AndroidSDK AndroidSDK ./out/host/linux-x86/sdk/android-sdk_eng.root_linux-x86 255

然后使用update-alternatives --display AndroidSDK查看当前配置情况;

如果要切换配置,使用update-alternatives --config AndroidSDK。

配置AndroidSDK环境变量。终端中执行gedit ~/.bashrc

在文件最后添加下面三行:

# set android environment

export ANDROID_SDK_HOME=/usr/bin/AndroidSDK

export PATH=$ANDROID_SDK_HOME/tools:$PATH

保存文件。在终端中执行source ~/.bashrc

2. 编译过程中可能会遇到的问题。

问题一:找不到bios.bin和vgabios-cirrus.bin文件

couldn't locate source file: usr/share/pc-bios/bios.bin

couldn't locate source file: usr/share/pc-bios/vgabios-cirrus.bin

注意,这里的usr/share目录指的是~/Android/out/host/linux-x86目录下的usr/share目录,修改办法是复制~/Android/prebuilt/common下的pc-bios文件夹到~/Android/out/host/linux-x86/usr/share即可:

USER-NAME@MACHINE-NAME:~/Android$ cp ~/Android/prebuilt/common/pc-bios ~/Android/out/host/linux-x86/usr/share

问题二:找不到ddmlib-tests.jar、 ninepath-tests.jar 、common-tests.jar 和sdkuilib-tests.jar文件

在~/Android/out/host/linux-x86/framework这个目录下,可以找到以下几个文件common.jar、ddmlib.jar、ninepatch.jar、sdkuilib.jar这四个文件,然后将它们分别复制一份,并重命名,命名的原则很简单,就是在原有的名字后面跟上-tests即可。

五. 安装编译好的Android镜像到模拟器上。

1. 设置环境变量:

USER-NAME@MACHINE-NAME:~/Android$ export PATH=$PATH:~/Android/out/host/linux-x86/bin

USER-NAME@MACHINE-NAME:~/Android$ export ANDROID_PRODUCT_OUT=~/Android/out/target/product/generic

其中,~/Android/out/host/linux-x86/bin有我们要执行的emulator命令,而~/Android/out/target/product/generic是Android镜像存放目录,下面执行emulator命令时会用到。

2. 运行模拟器。

USER-NAME@MACHINE-NAME:~/Android$ emulator

模拟器运行需要四个文件,分别是Linux Kernel镜像zImage和Android镜像文件system.img、userdata.img和ramdisk.img。执行emulator命令时,如果不带任何参数,则Linux Kernel镜像默认使用~/Android/prebuilt/android-arm/kernel目录下的kernel-qemu文件,而Android镜像文件则默认使用ANDROID_PRODUCT_OUT目录下的system.img、userdata.img和ramdisk.img,也就是我们刚刚编译出来的镜像问题。

当然,我们也可以以指定的镜像文件来运行模拟器,即运行emulator时,即:

USER-NAME@MACHINE-NAME:~/Android$ emulator -kernel ./prebuilt/android-arm/kernel/kernel-qemu -sysdir ./out/target/product/generic -system system.img -data userdata.img -ramdisk ramdisk.img

到这里,我们就可以在模拟器上运行我们自己编译的Android镜像文件了,是不是很酷呢?但是注意,这里说的Android镜像文件,只是包括system.img、userdata.img和ramdisk.img这三个文件,而Linux Kernel镜像用的是Android为我们预编译好的kernel-qemu镜像。那么,有没有办法使用我们自己编译的Linux Kernel镜像呢?答案上肯定的,这样我们就可以完全DIY自己的Android系统了

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: