2014年5月22日 星期四

Using Shell Script on Android

Preface

Shell Script is a good tool to control Android device. You can run the script either by connecting PC or by disconnecting.PC. In order to enter Android's console mode, the ADB is required. Therefore, you can start to use Shell Script on Android. Shell Script is well known on Linux PC to do some scenario control. The kernel of Android is Linux Based. Hence, you can use similar script to control your Android device.

Introduction

Let's learn how to run a Shell Script on Android at first. I'll give an example.
In begging, you need a edit tool for shell script on PC. GEdit is a good choice for you. Note that you need to save your shell script on Linux Format rather than Windows.

Let's get start on writing shell script to run on your Android.
=================================================
#!/bin/sh
#The comments you can write here follows #
#Assign Value 1 to variable named val
val=1
#Assign a String "Hello World" to a variable str1 by export command
export str1="Hello World"
echo "$str1"
#Append a String to its variable by ":"
export str1=$str1:" Hello World again"

#The Screen will show you "Hello World Hello World again"
echo "$str1"

#The Screen will show you "Hello World Hello World again" by infinite loop
while [ true ]
do
   echo "$str1"
done
=================================================

Save this file as Linux format and push this file to Android device by following command.
adb push filename /data/local/tmp/
Execute this shell by following command.
adb shell sh /data/local/tmp/filenmae

The output would be as follows,
Hello World
Hello World: Hello World again
Hello World: Hello World again
Hello World: Hello World again
...

Note
It's unnecessary to have root permission to run the shell script if you place files in the folder of /data/local/tmp/ and don't use the none permitted command.

Keyword: Shell Script, Android

2014年5月6日 星期二

在Android做LTP (Linux Test Project)測試

在Android做LTP (Linux Test Project)測試

序:

LTP是一個測試Linux核心的開源(Open Source)測試套件, 一開始由SGI發起, 由 IBM, Cisco, Fujitsu, SUSE, Red Hat 等公司開發與維護. 目前能持續的進版中. LTP針對Linux系統做可靠度, 強健度, 與穩定度做系統測試, 內涵許多的Test Case可供測試. 測試Linux伺服器時, 也可以採用這樣的測試套件來為你購買的硬體設備做燒機(Burn/Run in)測試.

Android的底層核心是Linux系統, 可透過Android ADB的連線進到大家熟悉的Linux Console環境. LTP套件基本上的設計是基於PC的系統架構與權限條件. 但Android的架構與X86是不一樣的, 因此要把LTP佈署在Android的裝置上, 必須採用可在X86環境上編譯Android架構可用的編譯器(Compiler), 稱作跨平台編譯器(Cross Compiler). 由於LTP的源碼(Source Code)的架構與參數設定, 是給X86架構使用的, 因此在編譯前, 還必須做些參數上的調整.當中有相當多的觀念可以學習.

介紹:

環境建置: 在開始編譯一個合適你的Android系統測試用的LTP前, 環境建置是必須的. 以下分別介紹要安裝那些套件.

1. 安裝Ubuntu64 bit在你的電腦上, 我是用12版.
2. 到 sourcery.mentor.com 下載Cross Compiler. 也可以用新版的Cross Compiler.
3. 於Ubuntu上安裝以下套件.

  • sudo apt-get install git-core flex bison gperf libesd0-dev libwxgtk2.6-dev zlib1g-dev build-essential libstdc++5 tofrodos
  • sudo apt-get install x-dev
  • sudo apt-get install libx11-dev
  • sudo apt-get install libncurses5-dev
  • sudo apt-get install libreadline-dev
  • sudo apt-get install g++-multilib (build kernel用) 
  • Ubuntu 64bit
  • apt-get install ia32-libs ia32-libs-gtk
  • sudo dpkg-reconfigure dash  Selection 'NO‘
  • ./arm-2012.09-64-arm-none-linux-gnueabi.bin
3. 於sourceforge.net下載LTP原始碼

4. 編譯環境設定.
  • 設定Cross Compiler的路徑到環境變數中. 以下路徑需要修改為你放置與安裝Cross Compiler的正確資料夾. 注意這些環境設定是適用於ARM平台.
export PATH=$PATH:/home/carl/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_GNU_Linux/bin/
export CC=/home/carl/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_GNU_Linux/bin/arm-none-linux-gnueabi-gcc
export LD=/home/carl/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_GNU_Linux/bin/arm-none-linux-gnueabi-ld
export AR=/home/carl/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_GNU_Linux/bin/arm-none-linux-gnueabi-ar
export LIB_DIR=/home/carl/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_GNU_Linux/lib
export AS=/home/carl/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_GNU_Linux/bin/arm-none-linux-gnueabi-as
export RANLIB=/home/carl/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_GNU_Linux/bin/arm-none-linux-gnueabi-ranlib
export STRIP=/home/carl/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_GNU_Linux/bin/arm-none-linux-gnueabi-strip

編譯與安裝LTP
1. 設定你的LTP.
./configure AR=arm-none-linux-gnueabi-ar CC=arm-none-linux-gnueabi-gcc RANLIB=arm-none-linux-gnueabi-ranlib STRIP=arm-none-linux-gnueabi-strip --host=arm-linux --target=arm-linux CFLAGS="-static" LDFLAGS="-static -pthread“
2. 為了要讓LTP能順利在ARM平台下的Linux執行, 需要做一些參數上的調整. 這邊參考了網路一些零碎的資料, 實做了, 是可行的. 以下以20120104的LTP版本為例. 實驗過2013版本也是可行的.
  • 在 ltp-full-20120104/testcases/kernel/controllers/libcontrollers/libcontrollers.h 裡增加 LINE_MAX 的 define, 這邊是設定為 255.
  • 將 ltp-full-20120104/testcases/kernel/fs/fs-bench 裡 random-access.c, random-del-create.c & random-access-del-create.c 這三個檔案裡的 openlog[] 陣列名稱修改成其他的名稱, 避免衝突.
  • 在 ltp-full-20120104/testcases/kernel/sched/sched_stress/sched_driver.c 和 ltp-full-20120104/testcases/kernel/fs/ftest/libftest.c 這兩個檔案裡, 增加 PATH_MAX 的 define 值, 這裡是設定成 1024.
  • 在 ltp-full-20120104/testcases/kernel/sched/cfs-scheduler/hackbench.c 裡加上 PTHREAD_STACK_MIN 的 define, 這兒是設成 1024.
  • make
  • make install prefix=/[Install Folder]
3. 開始編譯與安裝在指定的資料夾. 這會需要一段很長的時間, 視電腦速度而異. 編譯出來約1GByte的大小. 若只有100M上下的檔案大小, 就是沒有編譯成功.

  • make
  • make install prefix=/[Install Folder]

佈署LTP到你的Android裝置

1. 下載Busybox, 並且推到你的Android中. 因為Android裝置內的Linux指令已經精簡過了, 因此少了很多Shell Command. 你需要補強幾個LTP會用到的Linux Shell Command.
adb push busybox /data/local/tmp
2. 把編譯出的LTP整個資料夾推送到Android裝置內的/data/local/tmp/資料夾.
3. 讓你的Busybox內的command可以實體化. 以下只舉幾個例子, 最好可以把busybox內的所有command都做一次以下動作.

  • busybox ln -f busybox readlink
  • busybox ln -f busybox basename
  • busybox ln -f busybox mktemp
  • busybox ln -f busybox dirname
  • busybox ln -f busybox grep
  • busybox ln -f busybox sed
  • busybox ln -f busybox awk
  • busybox ln -f busybox head
  • busybox ln -f busybox free
  • busybox ln -f busybox uname

4. 於Android ADB命令列下設定執行LTP環境. 例如我編譯出的LTP都推到Android內的 /data/local/tmp/LTP13/資料夾內.

  • export PATH=/data/local/tmp/busyboxdir:$PATH
  • export PATH=/data/local/tmp/LTP13:$PATH
  • export PATH=/data/local/tmp/LTP13/testcases/bin:$PATH
  • export PATH=/data/local/tmp/LTP13/runtest:$PATH
開始執行LTP
sh ./runltp -p -l 130326report1ipc.log -o 130326output1ipc.log  -d /data/local/tmp/LTP13/tmp -f ipc

後記

由於LTP的Source Code中有些程式碼不符合Android的一些限制, 需要做修改後方可正常執行. 若有興趣的人, 在留言板上留言, 我們再來討論吧.

至於LTP的Soruce Code是用甚麼語言寫的, 主要分為兩種, 一種是C語言, 另外一種是Shell Script配合Shell Command執行.

參考資料:

Internet Q&A

關鍵字: LTP, Android, Linux Test Project, Stress Test, Android Reliability, Automation Test. 分享到Line

Linux Test Project (LTP) on Android

Perform LTP on Android

Preface

LTP WiKi said, "The Linux Test Project is a joint project started by SGI, developed and maintained by IBM, Cisco, Fujitsu, SUSE, Red Hat and others, that has a goal to deliver test suites to the open source community that validate the reliability, robustness, and stability of Linux. The LTP testsuite contains a collection of tools for testing the Linux kernel and related features."

The kernel layer of Android is based on Linux. User can connect to Linux Console by Android ADB. Download the LTP from official website, it can run on Linux PC. However, it can not run on ARM platform such as Android because of its original design on X86. Therefore, if you would like to use LTP on Android, the Cross Compiler is required.

Introduction

Configuration Environment
1. Prepare a PC and install Ubuntu64 bit on it. (version 12 is applicable.)
2. Download the Cross Compiler from sourcery.mentor.com. (Newer version is applicable.)
3. Install following program on Ubuntu
  • sudo apt-get install git-core flex bison gperf libesd0-dev libwxgtk2.6-dev zlib1g-dev build-essential libstdc++5 tofrodos
  • sudo apt-get install x-dev
  • sudo apt-get install libx11-dev
  • sudo apt-get install libncurses5-dev
  • sudo apt-get install libreadline-dev
  • sudo apt-get install g++-multilib (For build kernel) 
  • Ubuntu 64bit
  • apt-get install ia32-libs ia32-libs-gtk
  • sudo dpkg-reconfigure dash  Selection 'NO‘
  • ./arm-2012.09-64-arm-none-linux-gnueabi.bin
3. Download LTP source code from sourceforge.net

4. Environment settings for Cross Compiler
  • You need to modify the file path for your own. Note that settings here is for ARM platform.
export PATH=$PATH:/home/carl/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_GNU_Linux/bin/
export CC=/home/carl/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_GNU_Linux/bin/arm-none-linux-gnueabi-gcc
export LD=/home/carl/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_GNU_Linux/bin/arm-none-linux-gnueabi-ld
export AR=/home/carl/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_GNU_Linux/bin/arm-none-linux-gnueabi-ar
export LIB_DIR=/home/carl/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_GNU_Linux/lib
export AS=/home/carl/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_GNU_Linux/bin/arm-none-linux-gnueabi-as
export RANLIB=/home/carl/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_GNU_Linux/bin/arm-none-linux-gnueabi-ranlib
export STRIP=/home/carl/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_GNU_Linux/bin/arm-none-linux-gnueabi-strip

Compile and install LTP
1. Configuration
./configure AR=arm-none-linux-gnueabi-ar CC=arm-none-linux-gnueabi-gcc RANLIB=arm-none-linux-gnueabi-ranlib STRIP=arm-none-linux-gnueabi-strip --host=arm-linux --target=arm-linux CFLAGS="-static" LDFLAGS="-static -pthread“
2. Some values of parameter should be changed for ARM platform. Here I illustrate what should be changed by the version in 2012. (The version of 2013 is applicable)
  • Find and edit the file ltp-full-20120104/testcases/kernel/controllers/libcontrollers/libcontrollers.h and add one line for LINE_MAX and define its value as 255.
  • Change the varialbe name of array in random-access.c, random-del-create.c and random-access-del-create.c which files are located at ltp-full-20120104/testcases/kernel/fs/fs-bench/
  • Find and edit the files named ltp-full-20120104/testcases/kernel/sched/sched_stress/sched_driver.c and ltp-full-20120104/testcases/kernel/fs/ftest/libftest.c and add one line for PATH_MAX and define its value as1024.
  • Find and edit the file ltp-full-20120104/testcases/kernel/sched/cfs-scheduler/hackbench.c and add one line for PTHREAD_STACK_MIN and define its value as 1024.
  • make
  • make install prefix=/[Install Folder]
3. After the install, there are some files in your Install Folder and the total file size is about 1GByte. It takes long time to perform compiling work.


Deploy LTP into your Android device

1. Download Busybox and push it to your Android in order to have necessary shell command on Android. adb push busybox /data/local/tmp
2. Push all the installed LTP folder (Install Folder) to Android. I always push the files to /data/local/tmp.
3. Ln all the command from busybox. Following examples only shows you some pieces, you need to ln all the commands that busybox have.
  • busybox ln -f busybox readlink
  • busybox ln -f busybox basename
  • busybox ln -f busybox mktemp
  • busybox ln -f busybox dirname
  • busybox ln -f busybox grep
  • busybox ln -f busybox sed
  • busybox ln -f busybox awk
  • busybox ln -f busybox head
  • busybox ln -f busybox free
  • busybox ln -f busybox uname

4. Setting the LTP variable environment in Adroid. You need to change the path to where your file located in Android.
  • export PATH=/data/local/tmp/busyboxdir:$PATH
  • export PATH=/data/local/tmp/LTP13:$PATH
  • export PATH=/data/local/tmp/LTP13/testcases/bin:$PATH
  • export PATH=/data/local/tmp/LTP13/runtest:$PATH
Run LTP on Android
sh ./runltp -p -l 130326report1ipc.log -o 130326output1ipc.log  -d /data/local/tmp/LTP13/tmp -f ipc


Reference

Internet Q&A

Keyword: LTP, Android, Linux Test Project, Stress Test, Android Reliability, Automation Test. Share to Line

2013年1月2日 星期三

Android Power State Information Acquired


Introduction

Android Power state can be found by using console/ADB command - adb shell dumpsys power, then the message would be printed as follows,

Power Manager State:
  mIsPowered=true mPowerState=3 mScreenOffTime=958909 ms
  mPartialCount=1
  mWakeLockState=SCREEN_BRIGHT_BIT SCREEN_ON_BIT
  mUserState=
  mPowerState=SCREEN_BRIGHT_BIT SCREEN_ON_BIT
  mLocks.gather=SCREEN_BRIGHT_BIT SCREEN_ON_BIT
  mNextTimeout=66689 now=1022321 -955s from now
  mDimScreen=true mStayOnConditions=3 mPreparingForScreenOn=false mSkippedScreenOn=false
  mScreenOffReason=2 mUserState=0
  mBroadcastQueue={-1,-1,-1}
  mBroadcastWhy={0,0,0}
  mPokey=0 mPokeAwakeonSet=false
  mKeyboardVisible=false mUserActivityAllowed=false
  mKeylightDelay=6000 mDimDelay=47000 mScreenOffDelay=7000
  mPreventScreenOn=false  mScreenBrightnessOverride=-1  mButtonBrightnessOverride=-1
  mScreenOffTimeoutSetting=60000 mMaximumScreenOffTimeout=2147483647
  mLastScreenOnTime=1019715
  mBroadcastWakeLock=UnsynchronizedWakeLock(mFlags=0x1 mCount=0 mHeld=false)
  mStayOnWhilePluggedInScreenDimLock=UnsynchronizedWakeLock(mFlags=0x6 mCount=0 mHeld=true)
  mStayOnWhilePluggedInPartialLock=UnsynchronizedWakeLock(mFlags=0x1 mCount=0 mHeld=true)
  mPreventScreenOnPartialLock=UnsynchronizedWakeLock(mFlags=0x1 mCount=0 mHeld=false)
  mProximityPartialLock=UnsynchronizedWakeLock(mFlags=0x1 mCount=0 mHeld=false)
  mProximityWakeLockCount=0
  mProximitySensorEnabled=false
  mProximitySensorActive=false
  mProximityPendingValue=-1
  mLastProximityEventTime=0
  mLightSensorEnabled=false mLightSensorAdjustSetting=0.0
  mLightSensorValue=-1.0 mLightSensorPendingValue=-1.0
  mHighestLightSensorValue=-1 mWaitingForFirstLightSensor=false
  mLightSensorPendingDecrease=false mLightSensorPendingIncrease=false
  mLightSensorScreenBrightness=-1 mLightSensorButtonBrightness=-1 mLightSensorKeyboardBrightness=-1
  mUseSoftwareAutoBrightness=true
  mAutoBrightessEnabled=false
mScreenBrightnessAnimator:
  animating: start:0, end:102, duration:960, current:102
  startSensorValue:-1 endSensorValue:-1
  startTimeMillis:1019791 now:1022324
  currentMask:SCREEN_BRIGHT_BIT

mLocks.size=3:
  SCREEN_DIM_WAKE_LOCK           'StayOnWhilePluggedIn Screen Dim' activated (minState=1, uid=1000, pid=622)
  PARTIAL_WAKE_LOCK              'StayOnWhilePluggedIn Partial' activated (minState=0, uid=1000, pid=622)
  FULL_WAKE_LOCK                 'keyguard'ACQUIRE_CAUSES_WAKEUP  activated (minState=3, uid=1000, pid=622)

mPokeLocks.size=0:

The power state is listed by parameter mPowerState which has the flag value as follows,

  1. // flags for setPowerState
  2.     private static final int ALL_LIGHTS_OFF         = 0x00000000;
  3.     private static final int SCREEN_ON_BIT          = 0x00000001;
  4.     private static final int SCREEN_BRIGHT_BIT      = 0x00000002;
  5.     private static final int BUTTON_BRIGHT_BIT      = 0x00000004;
  6.     private static final int KEYBOARD_BRIGHT_BIT    = 0x00000008;
  7.     private static final int BATTERY_LOW_BIT        = 0x00000010;

If device is on suspend mode, it would print 0 for mPowerState parameter.

When device is on screen on mode, the value 3 is shown for parameter mPowerState because of following reason.

  1. // SCREEN_BRIGHT == screen on, screen backlight bright
  2.     private static final int SCREEN_BRIGHT      = SCREEN_ON_BIT | SCREEN_BRIGHT_BIT;

where SCREEN_ON_BIT and SCREEN_BRIGHT_BIT are performed or operation. 

References
https://android.googlesource.com/platform/frameworks/base/+/android-4.1.2_r1/services/java/com/android/server/PowerManagerService.java

2012年11月19日 星期一

Extending monkeyrunner with Plugins - Jar File


Preface
As you can see the description on Android Developer Site, you can extend MonkeyRunner by writing  java programming language and integrate to an Jar file and create more capacities for your control purpose. In following section, using 21 steps to guide you how to create a monkeyrunner extension JAR file.

How to create a Jar component for MonkeyRunner?

Before we start to know how to create a Jar Component for MonkeyRunner, there're something you need to do in advance. The J2SDK installation on your computer is required and you need to know how to program  Java based program. Eclipse is an option, however it's very useful. Android SDK installation is required. 

Here I'll show you an example by using Eclipse to create a Jar component for MonkeyRunner Extension.

1. Using Eclipse to create a Java Project through File->New->Java Project. Fill the project name.
2. Press Next button.



 3. Tap Libraries.



4. Tap "Add External JARs..." button.



5. Add Jar files - Pick chimpchat.jar, guavalib.jar, jython.jar, and monkeyrunner.jar in the folder - /android_sdk_folder/tools/lib/.
6. Press Finish button.



7. Fill the package name - com.android.monkeyrunner.
8. Fill the class name.
9. Press Finish button.  



10.  If you need to initial some variable for monkeyrunner runtime environment, you can implement a interface named Predicate and write the initial thing in apply method.
11. In this example, I create three static methods for interacting with Android device. They're tap, PressButton, and saveScreenToFile respectively. These functions are to trigger a touch event, and a button event, and to save run-time screen to a file. In order to implement these function to communicate with Android device, we need to import MonkyDevice and MonkeyImage classes. The usage is similar with what you wrote on monkeyrunner console to control Android devices. The difference is that you need to use Python Object class for given parameters. For instance, to invoke a touch event, you can use touch method in MonkeyDevice class. But you need to give a value which is belong to the type of PyObject. Hence, we declare a PyObject variable and assign two integer and one string as their values that is needed by declared PyObject variable. For given parameters, the two integer values are used to assign where to touch and the string value is used to assign which action we're going to use for touch event (E.g. DOWN, UP, or DOWN_AND_UP). Note that, you can not use int as integer's type in PyObject's initialization. To assign an integer value to PyObject class, the type of PyInteger Object is needed. To assign a string value to PyObject class, the type of PyString Object is needed. 



12. The Eclipse is very useful for developing MonkeyRunner Extension Jar file, because you can see the tip when your mouse move cursor to the method. Hence, you can know what type and  you should use and how many parameters you should provide for particular method.

13. Start to export your wrote files to a Jar file for MonkeyRunner after you finish the coding work.
14. Move mouse to project name on Eclipse and right click the mouse.
15. Click Export option.  


16. Select JAR file and press Next button.



 17. Select the files which are going to be exported and press Next button.



18. Press Next Button.



19. If you don't have a MANIFEST file existed already, you can select option of "Generate the manifest file" and press Browser button to select where to store the MANIFEST file. You can give a name like MANIFEST.MF and place in root folder of project. If you have a MANIFEST file already, you can select option - Use existing manifest from workspace and select your own file.
20. Press Finish button.



21. Edit the MANIFEST file you used at last step and add a command - MonkeyRunnerStartupRunner: com.android.monkeyrunner.monkeyrunnerExt 
to MANIFEST file. The string of MonkeyRunnerStartupRunner: is followed by full package name of your class. Finally, save your modified MANIFEST file to your JAR file.


How to import your own Jar file for MonkeyRunner?

Copy your Jar file to /android-sdk_folder/tools/lib before running monkeyrunner.bat. Double click monkeyrunner.bat at the folder of android-sdk/tools/ to bring up a MonkeyRunner console as follows. And import the necessary class and your own class as follows. Then you can use your own class to do something on MonkeyRunner run-time environment.




Reference
http://developer.android.com/tools/help/monkeyrunner_concepts.html#Plugins

2012年11月8日 星期四

Build an Android SDK

Preface

To build a customized Android SDK is a option to develop your SDK tools. In following section will talk about how to build a Android SDK. Before you read this paragraph, you need to know how to build a Android Build Environment in advance.

Description

Android SDK is very easy to build up. After you finish both the Android Build Environment setup and  a particular version of Android Source Code downloaded, you can do it by following command.

1. Go to root folder of Android Source Code.
2. Execute Console Command to build a Linix Android SDK - 

$ . build/envsetup.sh
$ lunch sdk-eng
$ make sdk


The Android SDK on Linux is started to build. Note that in case you want to build a Windows Android SDK, to perform the above actions in advance. Or the build process of Windows Android SDK would fail because of lack of something it needs from Linux Android SDK.

3. In case you want to build a Windows Android SDK, perform the following commands.

$ sudo apt-get install mingw32 tofrodos

$ . build/envsetup.sh
$ lunch sdk-eng
$ make win_sdk

The build process will finish after a long time. It depends on the performance of your build machine. You will see the following screen once the build process is successfully.

Reference
http://tools.android.com/build 

2012年11月7日 星期三

Android Build Environment Setup

Preface

Following Instructions will lead you to know how to build a Android build Environment.

Setup Your Ubuntu

In case you'd like to have both of Windows and Ubuntu can work on a computer concurrently, you can install Oracle VM VirtualBox in your Computer/Notebook and then install Ubuntu on VirtualBox. You can follow the instruction below to setup your Android Build Environment. If you want to install Ubutun on a computer only, you can ignore installation of VirtualBox.

1. Download Oracle VirtualBox from https://www.virtualbox.org/.
2. Download Ubuntu 10.04 LTE from http://releases.ubuntu.com/lucid/ and choose the file named ubuntu-10.04.4-desktop-amd64.iso.
3. Install VirtualBox and Create a Virtual Ubuntu System on VirtualBox.
4. Install Ubuntu on VirtualBox.

Configure Your Ubuntu

1. Launch a terminal on Ubuntu.
2. Follow the instructions that Google said at  http://source.android.com/source/initializing.html to install the required tools.

Detailed instructions for Ubuntu and MacOS follow. In general you will need:
  • Python 2.5 -- 2.7, which you can download from python.org.
  • GNU Make 3.81 -- 3.82, which you can download from gnu.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.

Install the following tools by executing console command below.

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 xsltproc

Because Canonical is no longer to support Oracle Java, you need to install the Oracle JDK6 manually. It means you cannot use apt-get tool to install Oracle JDK6. Follow the instruct at following section - Installation Instruction of Oracle J2SDK 6 on Ubuntu 10.04 64 bit to install the Oracle J2SDK6.

3. Download and install repo tool from Google - http://source.android.com/source/downloading.html.


Make sure you have a bin/ directory in your home directory, and that it is included in your path:

$ mkdir ~/bin
$ PATH=~/bin:$PATH
Download the Repo script and ensure it is executable:

$ curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > ~/bin/repo
$ chmod a+x ~/bin/repo

4. 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:

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

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

$ repo init -u https://android.googlesource.com/platform/manifest -b android-4.1.2_r1

Note that the parameter -b is followed by Android version. The Android version can be assigned to the latest version or previous version that Google released.  

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.

5. Get the source code by repo
$ repo sync

6. After several hours, you may receive 6 to 8 GByte file in your storage.


Installation Instruction of Oracle J2SDK 6 on Ubuntu 10.04 64 bit 


1. Download binary file from http://www.oracle.com/technetwork/java/javase/downloads/jdk6u37-downloads-1859587.html and click jdk-6u37-linux-x64.bin to download

2. Root permission is required to install J2SDK.
3. Copy the file jdk-6u37-linux-x64.bin to /usr/lib/jvm folder.
# cp /Download_Folder_Path/jdk-6u37-linux-x64.bin /usr/lib/jvm/jdk-6u37-linux-x64.bin
# cd usr/lib/jvm/
# sh jdk-6u37-linux-x64.bin

4. After folder jdk1.6.0_37 is extracted, install it by using update-alternatives as follows,

update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.6.0_37/bin/java 1000
update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.6.0_37/bin/javac 1000
update-alternatives --install /usr/bin/javaws javaws /usr/lib/jvm/jdk1.6.0_37/bin/javaws 1000
update-alternatives --install /usr/bin/jar jar /usr/lib/jvm/jdk1.6.0_37/bin/jar 1000

The command of update-alternatives is a tool which can select particular tool version to be run on Linux. To view the manual by executing console command - "man update-alternatives" on Ubuntu.

5. Choose the option of Oracle Java by following command in case your Ubuntu has installed other Java VM (E.g. OpenJava).

#update-alternatives --config java

6. Check the Java version you used on your Ubuntu by following command.
# java -version
java version "1.6.0_37"
Java(TM) SE Runtime Environment (build 1.6.0_37-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.12-b01, mixed mode)

Reference:
https://www.virtualbox.org/