setting up android development environmentfaculty.winthrop.edu/.../settingupandroidsdk.pdf ·...

22
Setting up Android Development Environment This page describes the steps needed to set up a system for android development, both at application and OS/kernel level. It is based on a Ubuntu installation and a Nexus One device. This tutorial is being actively built. I will be adding further information about kernel development, etc. as soon as possible. Android Development Setup This tutorial describes the steps taken to provide a complete development environment for android. These are the steps I took for setting up a development environment for the Nexus One. A description of the system I used for this setup is shown below. Most google documentation is based on a 32-bit Ubuntu installation, which is the reason for my setup. Other systems may require different setups. A 64-bit linux system in particular requires some changes in order to support the 32-bit android SDK tools. Methods for other Android devices may differ also, particularly the steps related to the kernel, since these code branches are specific to each hardware. Thinkpad T410 with Core i5, 3 GB RAM Ubuntu 10.04 LTS, Lucid Lynx 32-bit Nexus One (came with stock Android 2.1 build ERD79) Pre setup Before installing the android SDK and source files, a couple of other items should be set up, such as java and eclipse. Java5 and above may be used for applications, but for source development, Java6 was not supported at the time of this writing due to incompatibilities with @Override. Unfortunately, Java5 (or java 1.5) is no longer in the Ubuntu repositories as of 10.04. It could be installed by downloading from the Sun website, but I used a work around found at this site. Be sure to install the Java JDK, not just the JRE. Installing Sun Java 1.5 on Ubuntu 10.04

Upload: others

Post on 23-Jul-2020

18 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

Setting up Android Development

Environment

This page describes the steps needed to set up a system for android development, both at

application and OS/kernel level. It is based on a Ubuntu installation and a Nexus One device.

This tutorial is being actively built. I will be adding further information about kernel

development, etc. as soon as possible.

Android Development Setup

This tutorial describes the steps taken to provide a complete development environment for

android. These are the steps I took for setting up a development environment for the Nexus

One. A description of the system I used for this setup is shown below. Most google

documentation is based on a 32-bit Ubuntu installation, which is the reason for my setup. Other

systems may require different setups. A 64-bit linux system in particular requires some changes

in order to support the 32-bit android SDK tools. Methods for other Android devices may differ

also, particularly the steps related to the kernel, since these code branches are specific to each

hardware.

Thinkpad T410 with Core i5, 3 GB RAM

Ubuntu 10.04 LTS, Lucid Lynx 32-bit

Nexus One (came with stock Android 2.1 build ERD79)

Pre setup

Before installing the android SDK and source files, a couple of other items should be set up, such

as java and eclipse. Java5 and above may be used for applications, but for source development,

Java6 was not supported at the time of this writing due to incompatibilities with

@Override. Unfortunately, Java5 (or java 1.5) is no longer in the Ubuntu repositories as of

10.04. It could be installed by downloading from the Sun website, but I used a work around

found at this site. Be sure to install the Java JDK, not just the JRE.

Installing Sun Java 1.5 on Ubuntu 10.04

Page 2: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

This method is only needed for installing Java SDK through Synaptic (or with APT). It can be

installed by downloading the Sun Java5 SDK deb file

from http://java.sun.com/javase/downloads/index_jdk5.jsp.

Add the following to /etc/apt/sources.list:

deb http://us.archive.ubuntu.com/ubuntu/ jaunty multiverse

deb http://us.archive.ubuntu.com/ubuntu/ jaunty-updates multiverse

This can also be accomplished by going to System->Administration->Software Sources, select

Other Software tab, click Add, enter complete line above beginning with deb and click Add

Source. Do this for both lines. Following this, there should be two new entries in the Other

Software tab: Ubuntu 9.04 'Jaunty Jackalope' and Recommended updates.

[You can probably uncheck the Ubuntu 9.04 'Jaunty Jackalope' entry after completing

installation of Java5, since you shouldn't need this included in your repository results anymore.]

Update the package list:

sudo apt-get update

Or open System->Administration->Synaptic Package Manager and click on Reload.

Install Sun Java 1.5:

sudo apt-get install sun-java5-jdk

Or, in Synaptic, search for java and select sun-java5-sdk and click Apply.

Depending on your setup you might need to set this as the default. Check what version is default:

java -version

Page 3: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

And list all Java versions installed:

sudo update-java-alternatives -l

Then if necessary:

sudo update-java-alternatives -s java-1.5.0-sun

Installing Eclipse

The android SDK needs Eclipse 3.4 or above, and recommends Eclipse 3.5 classic. This can be

acquired through eclipse's website, but luckily Eclipse 3.5 is the current version in the Ubuntu

10.04 repository. To install using Synaptic, search for eclipse, select eclipse (this should provide

the classic package and install any other needed eclipse packages, but you may choose to add

more eclipse packages - it shouldn't be needed for android development), and click Apply.

Installing Android SDK

The instructions for installing the SDK can be found at

http://developer.android.com/sdk/installing.html. Java and Eclipse should be installed prior to

these steps.

Downloading SDK starter package

First, download the SDK starter package. The starter package is not a full development

environment — it includes only the core SDK Tools, which you can use to download the rest of

the SDK components. You can get the latest version of the SDK starter package from the SDK

download page. Make sure to download the package that is appropriate for your development

computer.

Page 4: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

After downloading, unpack the Android SDK archive to a safe location on your machine. By

default, the SDK files are unpacked into a directory named android-sdk-<machine-platform>.

It's a good idea to add the location of the SDK's primary tools directory to your system PATH.

The primary tools/ directory is located at the root of the SDK folder. Adding tools to your path

lets you run Android Debug Bridge (adb) and the other command line tools without needing to

supply the full path to the tools directory.

o On my system, I added the following line to the end of ~/.profile.

PATH="<your_sdk_dir>/tools:$PATH"

o Alternatively, on Linux, edit your ~/.bash_profile or ~/.bashrc file. Look for a

line that sets the PATH environment variable and add the full path to the tools/

directory to it. If you don't see a line setting the path, you can add one:

export PATH=${PATH}:<your_sdk_dir>/tools

Installing the ADT plugin for eclipse

At the time of this writing, I installed ADT version 0.9.7 for Eclipse. The Update Manager

feature of eclipse can be used to install ADT. I used the following method to install ADT on

Eclipse 3.5. For other versions, consult http://developer.android.com/sdk/eclipse-adt.html to

verify that this method is still current.

1. Start Eclipse, then select Help > Install New Software.

2. In the Available Software dialog, click Add....

3. In the Add Site dialog that appears, enter a name for the remote site (for example,

"Android Plugin") in the "Name" field.

In the "Location" field, enter this URL:

https://dl-ssl.google.com/android/eclipse/

Note: If you have trouble acquiring the plugin, you can try using "http" in the URL,

instead of "https" (https is preferred for security reasons).

Click OK.

Page 5: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

4. Back in the Available Software view, you should now see "Developer Tools" added to

the list. Select the checkbox next to Developer Tools, which will automatically select the

nested tools Android DDMS and Android Development Tools. Click Next.

5. In the resulting Install Details dialog, the Android DDMS and Android Development

Tools features are listed. Click Next to read and accept the license agreement and install

any dependencies, then click Finish.

6. Restart Eclipse.

To point ADT to your Android SDK directory:

1. Select Window > Preferences... to open the Preferences panel.

2. Select Android from the left panel.

3. For the SDK Location in the main panel, click Browse... and locate your downloaded

SDK directory (this is the same as the <your_sdk_dir> in the previous installing SDK

tutorial, the folder which you extracted from the SDK starter package).

4. Click Apply, then OK.

Adding Android Platforms

The next step is to add the components for the platforms you want to develop for. You can add

whichever components you want, but if you want your applications to be compatible with older

versions of android, you should install all available platforms to test with. Initially, you may just

want to start with the platform you will have installed on your development phone. At the time

of this writing, my development Nexus One has Android 2.2 build FRF83. To install the desired

components, use the Android SDK and AVD Manager

tool in eclipse. To use the tool

1. Open Eclipse

2. Select Window > Android SDK and AVD Manager.

3. Select Available Packages in the left panel. This will reveal all of the components that

are currently available for download from the SDK repository.

4. Select the component(s) you'd like to install and click Install Selected. If you aren't sure

which packages to select, read Which components do I need?.

Page 6: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

5. Verify and accept the components you want and click Install Accepted. The components

will now be installed into your existing Android SDK directories.

My installation included the Android SDK Tools, revision 6. The Android 2.2 platform, the

Android API 8 (wich corresponds to Android 2.2), and the documentation and samples for API

8. New platforms are automatically saved into the <sdk>/platforms/ directory of your SDK;

new add-ons are saved in the <sdk>/add-ons/ directory; samples are saved in the

<sdk>/samples/android-<level>/; and new documentation is saved in the existing

<sdk>/docs/ directory (old docs are replaced).

After installing (or updating) these components, it is a good idea to check for updates to ADT in

eclipse, as sometimes this requires an update for the new components. To do this

1. Start Eclipse, then select Help > Check for Updates.

You now have everything you need to begin developing android applications. To get your hands

really messy and make modifications to the android system or kernel, continue to the following

sections.

Android Source Development

The instructions for downloading and installing the android source code can be found at

http://source.android.com/source/download.html. Google uses Repo, which is built on top of Git

to handle code branch management. For more information about Git and Repo, see

http://source.android.com/source/git-repo.html. The source is over 2 GB in size, and you will

need at least 6 GB of free space in order to build the android source.

Prerequisites

Page 7: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

To set up the android development environment, install the following packages using Synaptic or

apt-get. The version and package name of my installations are listed in parenthesis. Some of

these may already be present on your system.

Git 1.5.4 or newer (git-core 1.7.0.4)

GNU Privacy Guard (gnupg 1.4.10)

flex (flex 2.5.35)

bison (bison 2.4.1)

gperf (gperf 3.0.3)

libsdl-dev (libsdl1.2-dev 1.2.14)

libesd0-dev (libesd0-dev 0.2.41)

libwxgtk2.6-dev (optional) (libwxgtk2.6-dev 2.6.3.2.2)

build-essential (build-essential 11.4build1)

zip (zip 3.0)

curl (curl 7.19.7)

libncurses5-dev (libncurses5-dev 5.7)

zlib1g-dev (zlib1g-dev 1.2.3.3)

valgrind (valgrind 3.6.0)

lib32readline5-dev or above (libreadline6-dev 6.1)

Installing Repo

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

1. Make sure you have a ~/bin directory in your home directory, and check to be sure that

this bin directory is in your path: 2. $ cd ~ 3. $ mkdir bin

$ echo $PATH

You may need to exit bash and restart it after mkdir bin. Your profile controller likely

adds ~/bin to your PATH if it exists, but you will need to restart bash for it to recognize it

is there and add it to PATH.

4. Download the repo script and make sure it is executable: 5. $ curl http://android.git.kernel.org/repo >~/bin/repo

$ chmod a+x ~/bin/repo

Page 8: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

Initializing a Repo Client

1. Create an empty directory to hold your working files:

$ mkdir mydroid

$ cd mydroid

2. Be sure you are in your mydroid directory before this step. This is where the repository

will be installed. 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:

$ repo init -u git://android.git.kernel.org/platform/manifest.git

o If you would like to check out a branch other than "master", specify it with -b,

like:

$ repo init -u git://android.git.kernel.org/platform/manifest.git -b cupcake

A list of the available branches can be found by going to ~/mydroid/manifest/ and

entering:

$ git branch -r

The branch name is the title following origin/ for each item in the list. This could

only be run after running the above command to obtain the master branch, since

this installs the manifest folder and downloads manifest.git. Alternatively, this

may be possible by running

$ git clone git://android.git.kernel.org/platform/manifest.git

but I have not tried doing this before running the repo command, and I don't know

if that will cause a conflict with repo.

3. When prompted, configure Repo with your real name and email address. If you plan to

submit code, use an email address that is associated with a Google account.

A successful initialization will end with a message such as

repo initialized in /mydroid

Your client directory should now contain a .repo directory where files such as the manifest will

be kept. These steps simply set up and install the manifest data. The following step will install

the designated source files based on the manifest file.

Getting the files

Page 9: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

To pull down files to your working directory from the repositories as specified in the default

manifest, change to your mydroid directory and run

$ repo sync

[This step will take a long time. You are downloading a couple gigs worth of files, so use the

best internet connection you can for this step, and if possible, do it before going to bed or when

you plan on being away from your computer for a while.]

The Android source files will be located in your working directory under their project names.

Verifying Git Tags

The following step adds Google's PGP key to your system so that you can verify all files you

download from the Android Git repo.

To load the key, run:

$ gpg --import

then copy the full text below (from -----BEGIN to BLOCK-----) and paste it into the bash shell

below the previous command, and press Control-D to end the input and process the key.

-----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

Page 10: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

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

$ git tag -v tagname

Building the code

To build the files, run make from within your working directory:

~$ cd ~/mydroid

~/mydroid$ mkdir vendor

~/mydroid$ mkdir vendor/htc

~/mydroid$ mkdir vendor/htc/passion

~/mydroid$ cd device/htc/passion

~/mydroid$ ./extract-files.sh # extract proprietary files from Nexus

~/mydroid$ cd ../../..

~/mydroid$ make clean

~/mydroid$ . ./build/envsetup.sh

~/mydroid$ lunch full_passion-userdebug # configure build for Nexus

(passion)

~/mydroid$ make -j4

Page 11: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

~/mydroid$ make modules

If your build fails, complaining about a missing "run-java-tool", try setting the

ANDROID_JAVA_HOME env var to $JAVA_HOME before making.E.g.,

$ export ANDROID_JAVA_HOME=$JAVA_HOME

Android Kernel Development

This section is based on my experiences getting the Android linux kernel for development. As

far as I can tell, there was not a tutorial or documentation specifically for kernel development

from Google, only the above stated Android package source. The following section is based on

an aggregation of information I have found from multiple online sources (all of which I will try

to reference), some of which is based on older systems and devices. I will continue to update

this section to show what has been successful and best practices, but I can not currently verify

that this is the preferred method of acquiring and developing kernel source for the current release

or Nexus One.

HTC has a site with a list of the downloads for kernel source code for each of their android

devices at http://developer.htc.com/. Unfortunately, when I unpack the zip file for Nexus One T-

Mobile source, it is just the image files for the phone, not the kernel source tree. I'm not sure if

this is a mistake, as I have seen tutorials which direct readers to this site for the source tree of

other HTC devices, so perhaps this will be fixed in the future. Instead, I obtained the kernel

source from Google Android's Git repository. Most of the following information was obtained

from these two tutorials:

[HOW-TO] Build your own kernel package from source

Building Android kernel images

Page 12: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

Setting up ADB and fastboot

ADB and fastboot are tools that allow you to connect to your device through a terminal on your

development machine, sort of like you may do with ssh. They will be needed for a number of

development steps such as pulling images and files from the devices, pushing images and files to

the device, and attempting trial boots.

First, USB permissions need to be set up for Ubuntu to allow connection to the device when it is

in the bootloader. This information was found in the following thread, [HOW-TO]Set up

Android SDK/ADB/Fastboot on Ubuntu Linux.

Setting up UDEV to recognize HTC Device -

1. Type the following into a terminal (Applications > Accessories > Terminal):

Code:

gksudo gedit /etc/udev/rules.d/51-android.rules

2. Now add the following lines to the blank file:

Code:

SUBSYSTEM=="usb", SYSFS{idVendor}=="0bb4", MODE="0666"

SUBSYSTEM=="usb",ATTR{idVendor}=="0bb4",ATTR{idProduct}=="0c02",SYMLINK

+="android_adb"

SUBSYSTEM=="usb",ATTR{idVendor}=="0bb4",ATTR{idProduct}=="0c01",SYMLINK

+="android_fastboot"

3. Click save and close.

4. To restart udev, open up a terminal and enter:

Page 13: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

Code:

sudo restart udev

Preparing Nexus One for Development

The Nexus One should be updated to the Android build that you plan to develop with, so that the

config matches that of the developed kernel. The phone that I began with came stock from

Google with the initial Android 2.1 Build ERD79. At the time of this writing, I was updating to

the current Android 2.2 release, Build FRF83. In order to do this, the device must go through the

proper sequence of build updates. The order is:

ERD79 -> ERE27 -> FRF50 -> FRF83

Each update requires the same process, with a different zip file. The update zip file links are

listed below. For each, perform the following process:

1. First thing you have to do is rename your update file to update.zip.

2. Copy the update.zip file onto your microSD card.

3. With your Nexus One powered off, hold down the trackball and press the power button.

4. You'll be booted into a white screen with three Android robots on skateboards. Select

Bootloader 5. On the next screen, select Recovery

6. Your phone will reboot, giving you a picture of the Android robot and an exclamation

point inside a triangle.

7. Now press the power button and volume up button at the same time. It could take a

couple of tries.

8. Now (using the trackball this time) choose "Apply sdcard:update.zip" and let things run

their course.

This process was found at [GUIDE] Manually flashing an update image to your Google

Nexus One.

To get from ERD79 to ERE27, go to this site, and download the patch zip file under Official

update patch zip.

Page 14: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

To get from ERE27 to FRF50, go to this site, and download the zip file under Stock Nexus One

and want Android 2.2 without having to root labeled File One.

To get from FRF50 to FRF83, go to this site, and download the zip file under Stock Nexus One

and want Android 2.2 without having to root labeled File Two.

Rooting and Unlocking the Nexus One

Before images can be extracted and loaded onto the Nexus One, the phone must be unlocked and

rooted. Luckily, developers have made this process very easy and have provided simple scripts

to accomplish this. The following method to unlock the bootloader and root the phone are

provided by:

How to unlock the bootloader on your Nexus One

and

Superboot - rooting the Nexus One

and both are summarized by

[Complete guide with Images] Unlock Bootloader and Root Google Nexus One [Mac,

Windows, Linux]

CAUTION : All your data will be erased.

1. Download the superboot zip file for the Android build you are running from here (since I

had just updated to FRF83, I downloaded the first link, Superboot for FRF83).

2. Extract the zip file to a directory in your development folder.

3. Turn off your Nexus One and connect it to your PC/Mac.

Page 15: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

4. Hold the trackball down and hit the power button (while holding the trackball) to enter

bootloader. You should see a white screen with FASTBOOT USB in red and

BOOTLOADER in blue.

5. Open a terminal/command prompt and go to the folder you extracted previously.

6. Type ./fastboot-linux oem unlock and hit Enter (for windows use fastboot-windows

oem unlock or for Mac use ./fastboot-mac oem unlock as appropriate).

7. If performed correctly, your Nexus One should now have a screen titled Unlock

bootloader? with two radio button options for Yes and No. Use the volume buttons on

the side of the Nexus to choose Yes, then push the Power button to make this selection.

8. Great! You have successfully unlocked the bootloader! You will see the unlock image

while your phone boots up.

9. Turn off your Nexus One and if it's not already connected, connect it to your PC/Mac.

10. Hold the trackball down and hit the power button (while holding the trackball) to enter

bootloader. You should see a white screen with FASTBOOT USB in red and

BOOTLOADER in blue. (It now has ***UNLOCKED*** in purple at the top, if the

previous steps were completed correctly)

11. Open a terminal/command prompt and go to the folder you extracted previously.

12. Type chmod +x install-superboot-linux.sh and hit Enter, followed by ./install-

superboot-linux.sh (for Mac use ./install-superboot-mac.sh, for windows just double

click install-superboot-windows.bat)

13. Now use volume keys and power button to select reboot. Once the phone reboots, it

should be rooted.

Downloading kernel source

Kernel sources are also maintained by the Google git repository. To get a list of the available

kernel source branches, get the manifest for the kernel repository. Go to the mydroid directory

you created in the Android Source Development section and do the following:

Checkout the source code from the git repository. From mydroid directory, enter:

~/mydroid$ git clone git://android.git.kernel.org/kernel/msm.git kernel-nexus

This will create a directory called kernel-nexus, with the manifest for the kernel/msm

repository (used for Nexus kernels). You may use a different directory name if you

prefer, this is where the kernel source tree will be downloaded. Change to the kernel-

nexus directory and pull a list of available branches by entering:

Page 16: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

~/mydroid$ cd kernel-nexus/

~/mydroid/kernel-nexus$ git branch -a

remotes/origin/HEAD -> origin/android-msm-2.6.27

remotes/origin/android-msm-2.6.25

remotes/origin/android-msm-2.6.27

remotes/origin/android-msm-2.6.29

remotes/origin/android-msm-2.6.29-donut

remotes/origin/android-msm-2.6.29-nexusone

remotes/origin/android-msm-2.6.32

remotes/origin/android-msm-2.6.32-nexusonec

remotes/origin/android-msm-htc-2.6.25

remotes/origin/android-msm8k-2.6.29

remotes/origin/msm-2.6.25

Now checkout the desired branch. I checked out the latest froyo kernel for the nexus,

android-msm-2.6.32-nexusonec. To do this, enter:

~/mydroid/kernel-nexus$ git checkout --track -b android-msm-2.6.32-nexusonec

origin/android-

msm-2.6.32-nexusonec

Branch android-msm-2.6.32-nexusonec set up to track remote branch

refs/remotes/origin/android

-msm-2.6.32-nexusonec.

Switched to a new branch "android-msm-2.6.32-nexusonec"

The kernel tree you have checked out should now be in kernel-nexus. You can verify

which branch using git branch, which will display the checked out kernels and indicate

the active branch with a *.

~/mydroid/kernel-nexus$ git branch

* android-msm-2.6.32-nexusonec

Page 17: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

Compiling the Kernel

In order to build kernels for Android devices, we will need to set some environment variables to

cross-compile for the hardware on the devices. For the Nexus One, this is an ARM processor. I

placed all of the environment assignments needed for make in a script called armmake, and

placed it in my ~/bin folder, so that I could use this command instead of make. Here is the

contents of that script:

#!/bin/bash

PATH=$PATH:~/mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin

make ARCH=arm CROSS_COMPILE=arm-eabi- $1

This kernel tree should be clean already, but before doing anything else, it's a good idea to run:

~/mydroid/kernel-nexus$ armmake distclean

In order to compile the kernel, you will need a .config file to match the target device. This can

be generated automatically by entering:

~/mydroid/kernel-nexus$ armmake mahimahi_defconfig

This will generate a .config file for the Nexus running froyo. Alternatively, you can pull the

config file from the device. I generated the config file automatically, but if you prefer to pull the

config from the device, or if the automatic method isn't working, here's how to get the .config.

Connect the Nexus using the USB cable (don't mount the device), and go to Settings-

>Applications->Development and check USB debugging. Then, in a terminal on your PC, go

to the kernel directory and enter: ~/mydroid/kernel-nexus$ adb pull /proc/config.gz .

84 KB/s (7233 bytes in 0.083s)

~/mydroid/kernel-nexus$ gunzip config.gz

~/mydroid/kernel-nexus$ mv config .config

Page 18: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

Finally, to compile the kernel, run:

~/mydroid/kernel-nexus$ armmake

If everything compiles with no errors, you will now have a file named zImage in

~/mydroid/kernel-nexus/arch/arm/boot/. This is the kernel image. Next, we will show how to

load this image on the phone.

You may want to create a copy of the kernel-nexus folder for your development. Now, each time

you want to compile the kernel, you only need to run this short sequence of commands

~/mydroid/kernel-nexus$ armmake distclean

~/mydroid/kernel-nexus$ armmake mahimahi_defconfig

~/mydroid/kernel-nexus$ armmake

Loading kernel image onto Nexus One

Before loading our custom kernel onto the phone, the original boot and recovery images should

be backed up. The partition layout of android is summarized as:

#cat /proc/mtd

dev: size erasesize name

mtd0: 00040000 00020000 "misc"

mtd1: 00500000 00020000 "recovery"

mtd2: 00280000 00020000 "boot"

mtd3: 04380000 00020000 "system"

mtd4: 04380000 00020000 "cache"

mtd5: 04ac0000 00020000 "userdata"

For a good overview of android image layout, as well as how to unpack and re-pack images, see

HOWTO: Unpack, Edit, and Re-Pack Boot Images. NOTE: This tutorial provides a good

Page 19: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

overview, but is based on the G1. Most of the information is still accurate for the Nexus,

but keep in mind that some alterations may be needed.

Create a folder where you want to store your images, and go to that directory in the

terminal. Then use adb to pull the boot and recovery images from the phone. Connect the Nexus

using the USB cable (don't mount the device), and go to Settings->Applications->Development

and check USB debugging. Then, in the terminal on your PC, enter: ~/images$ adb shell

# cat /dev/mtd/mtd1 > /sdcard/mtd1.img

# cat /dev/mtd/mtd2 > /sdcard/mtd2.img

# exit

~/images$ adb pull /sdcard/mtd2.img .

1296 KB/s (2621440 bytes in 1.973s)

~/images$ adb pull /sdcard/mtd1.img .

1516 KB/s (5242880 bytes in 3.376s)

This will place mtd1.img and mtd2.img in the images folder you ran this from. The recovery

image is mtd1.img and mtd2.img is the boot image. Next, unpack the boot image to get the

kernel and ramdisk components. Go to this link, and download the two perl scripts under the

first note, (unpack-bootimg.pl, repack-bootimg.pl). Create a folder in your home directory for

android development scripts, and place these scripts in that folder. You will also need to copy

two executables from the compiled android source to this folder. The files are mkbootimg and

mkbootfs, which are located in ~/mydroid/out/host/linux-x86/bin. If you have not already

compiled the android source, you can do so by going to ~/mydroid and running make. Now add

this script folder to your path. I did this by adding the following line to my .profile file:

PATH="~/android/scripts:$PATH"

Now unpack the boot image by running

~/images$ unpack-bootimg.pl mtd2.img

kernel written to mtd2.img-kernel.gz

ramdisk written to mtd2.img-ramdisk.cpio.gz

488 blocks

gzip: ../mtd2.img-ramdisk.cpio.gz: decompression OK, trailing garbage ignored

extracted ramdisk contents to directory mtd2.img-ramdisk/

Page 20: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

Now copy the kernel image that you compiled to this folder. The kernel image is named

zImage, and is located in ~/mydroid/kernel-nexus/arch/arm/boot/. Before flashing the custom

kernel to the device, we can use fastboot to test the kernel image. This will boot the device with

the custom kernel, without overwriting the image, so once the phone reboots, it will restore the

original image. To do this, connect the Nexus to your PC in bootloader mode, as described in

the rooting and unlocking section. Power off the Nexus One and connect it to the PC using the

USB cable. Hold the trackball down and hit the power button (while holding the trackball) to

enter bootloader. You should see a white screen with FASTBOOT USB in red and

BOOTLOADER in blue. Then, in the terminal, enter:

~/images$ fastboot-linux boot zImage

The device will reboot, and if all went well, your custom built kernel will be running on the

device. You can check by going to Settings->About phone and checking the Kernel version

field, which should show the user@host where you compiled the kernel. Congratulations, you

have compiled a working kernel for android.

To flash the working kernel to the Nexus One, first build a boot image. Use the following

sequence of commands to create a compressed ramdisk image, and then combine the ramdisk

image with the kernel image to generate a boot image.

~/images$ cp zImage boot.img-kernel

~/images$ mkbootfs boot.img-ramdisk | gzip > ramdisk-boot

~/images$ mkbootimg --kernel boot.img-kernel --ramdisk ramdisk-boot --cmdline

"no_c

onsole_suspend=1 console=null" -o myBoot.img --base 0x20000000

This will generate a boot image named myBoot.img. To flash this image to the device, connect

the device to the PC in bootloader mode again, and in a terminal enter:

~/images$ fastboot-linux flash boot myBoot.img

~/images$ fastboot-linux reboot

When the phone reboots, it should now be running the new boot image with your custom kernel.

Page 21: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

Getting Wifi working on custom kernel

The android wifi modules must be compiled against the kernel they are running on. So when

you flashed your new kernel, you probably noticed wifi no longer works. To correct this, we

need to re-compile wlan.ko for our kernel, and copy it and bcm4329.ko to the modules folder in

the android system. The wlan.ko module source is in the main android source tree, under

~/mydroid/system/wlan/ti/sta_dk_4_0_4_32. We will need to go to this directory and compile

the module, while setting the kernel source target to our custom kernel source directory. The

bcm4329.ko module is compiled with the kernel build, and is located in ~/mydroid/kernel-

nexus/drivers/net/wireless/bcm4329. These two modules will need to be pushed to the system

partition of the Nexus. To do this, take the following steps:

~/mydroid$ export PATH=$PATH:~/mydroid/prebuilt/linux-x86/toolchain/arm-eabi-

4.4.0/bin

~/mydroid$ cd system/wlan/ti/sta_dk_4_0_4_32/

~/mydroid/system/wlan/ti/sta_dk_4_0_4_32$ make KERNEL_DIR=~/mydroid/kernel-

nexus clean

~/mydroid/system/wlan/ti/sta_dk_4_0_4_32$ make KERNEL_DIR=~/mydroid/kernel-

nexus

# following this step, there will be a wlan.ko in this folder

~/mydroid/system/wlan/ti/sta_dk_4_0_4_32$ adb shell mount -o remount,rw

/dev/block/mtdb

lock3 /system

# this mounts the system partition as read-write, so that we can write to it.

you

can use 'adb shell cat /proc/mounts' to verify what the mount location

for /system

is on your device.

~/mydroid/system/wlan/ti/sta_dk_4_0_4_32$ adb shell cp

/system/lib/modules/wlan.ko /syste

m/lib/modules/wlan.ko.backup

~/mydroid/system/wlan/ti/sta_dk_4_0_4_32$ adb push wlan.ko

/system/lib/modules/wlan.ko

# wlan.ko is now loaded on Nexus. repeat for bcm4329.ko

~/mydroid/system/wlan/ti/sta_dk_4_0_4_32$ cd ~/mydroid/kernel-

nexus/drivers/net/wireles

s/bcm4329/

~/mydroid/kernel-nexus/drivers/net/wireless/bcm4329$ adb shell cp

/system/lib/modules/bc

m4329.ko /system/lib/modules/bcm4329.ko.backup

~/mydroid/kernel-nexus/drivers/net/wireless/bcm4329$ adb push bcm4329.ko

/system/lib/m

odules/bcm4329.ko

~/mydroid/kernel-nexus/drivers/net/wireless/bcm4329$ adb reboot

Page 22: Setting up Android Development Environmentfaculty.winthrop.edu/.../SettingupAndroidSDK.pdf · Setting up Android Development Environment This page describes the steps needed to set

The phone should now reboot, and hopefully when it boots back up, Wifi is restored.