android底层

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


 一. 环境准备。

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

     2. 安装VMWare 7.1.4。我的操作系统是Win7,VMWare的版本要新一点的,旧版本的VMWare在网络支持上比较差,由于要在虚拟机上下载Android源代码,没有网络是万万不行的。

     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. 下载repo工具。在Ubuntu上执行以下命令:

     USER-NAME@MACHINE-NAME:~$ wget  http://android.git.kernel.org/repo
     USER-NAME@MACHINE-NAME:~$ chmod 777 repo
     USER-NAME@MACHINE-NAME:~$ cp repo /bin/

链接http://android.git.kernel.org/repo已经失效了,用这个https://dl-ssl.google.com/dl/googlesource/git-repo/repo
关于 repo官网详细解析http://source.android.com/source/downloading.htm

      2. 下载Android最新版本源代码。在Ubuntu上执行以下命令:

     USER-NAME@MACHINE-NAME:~$ mkdir Android

     USER-NAME@MACHINE-NAME:~$ cd Android
     USER-NAME@MACHINE-NAME:~/Android$ repo init -u git:// android.git.kernel.org/platform/manifest.git
     USER-NAME@MACHINE-NAME:~/Android$ repo sync


     经过漫长的等待(我下载了两三天)后,就可以把Android源代码下载下来了。其间可能还有经历下载中断的情况,这时只要重新执行repo sync就可以了。
     三. 编译Android源代码。
     1. 编译。在Android目录下执行以下命令:
      USER-NAME@MACHINE-NAME:~/Android$ make
     第一次编译要等待比较久的时间,编译成功后,可以看到下面的输出:
     Target system fs image:    out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img
     Install system fs image: out/target/product/generic/system.img
     Target ram disk: out/target/product/generic/ramdisk.img
     Target userdata fs image: out/target/product/generic/userdata.img
     Installed file list: out/target/product/generic/installed-files.txt 


 四. 编译SDK,这一步是可选的。
     1. 编译。执行以下命令:
     USER-NAME@MACHINE-NAME:~/Android$ make sdk


五. 安装编译好的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系统了!我将在下一篇文章描述如果编译自己的Linux Kernel镜像,敬请期待~






二. 下载Linux Kernel for Android源代码。

      1. 使用GIT工具下载,执行以下命令:

      USER-NAME@MACHINE-NAME:~/Android$ mkdir kernel

      USER-NAME@MACHINE-NAME:~/Android$ cd kernel
      USER-NAME@MACHINE-NAME:~/Android/kernel$ git clone git://android.git.kernel.org/kernel/common.git

下载模拟器使用的内核地址为:        git clone https://android.googlesource.com/kernel/goldfish.git

      同样是经过漫长的等待后,在kernel目录下有一个common目录,Linux内核代码就在这里了。
      2. 下载完成后,可以查看下载的内核代码版本:
     USER-NAME@MACHINE-NAME:~/Android/kernel$ cd  common
      USER-NAME@MACHINE-NAME:~/Android/kernel/common$ git  branch
      android-2.6.36
      3. 上面得到的是主线上的内核源代码,现在我们需要适用于模拟器用的内核,因此,我们需 要checkout goldfish版本:
      USER-NAME@MACHINE-NAME:~/Android/kernel/common$ git branch -a
      * android-2.6.36
      remotes/origin/HEAD -> origin/android-2.6.36
      remotes/origin/android-2.6.35
      remotes/origin/android-2.6.36
      remotes/origin/archive/android-2.6.25
      remotes/origin/archive/android-2.6.27
      remotes/origin/archive/android-2.6.29
      remotes/origin/archive/android-2.6.32
      remotes/origin/archive/android-gldfish-2.6.29
      remotes/origin/archive/android-goldfish-2.6.27
      选择android-gldfish-2.6.29:
      USER-NAME@MACHINE-NAME:~/Android/kernel/common$ git checkout remotes/origin/archive/android-gldfish-2.6.29
     三. 编译内核代码。
     1. 导出交叉编译工具目录到$PATH环境变量中去:
        USER-NAME@MACHINE-NAME:~/Android/kernel/common$ export PATH=$PATH:~/Android/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin
     2.  修改common目录下的Makefile文件的以下两行为:
      # ARCH ?= (SUBARCH)
      # CROSS_COMPILE ?= 
      ARCH ?= arm  #体系结构为arm
      CROSS_COMPILE     ?= arm-eabi- #交叉编译工具链前缀, 参考~/Android/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin目录 
      3. 开始编译:
      USER-NAME@MACHINE-NAME:~/Android/kernel/common$ make goldfish_defconfig
       USER-NAME@MACHINE-NAME:~/Android/kernel/common$ make
     编译成功后,可看到下面两行:
      OBJCOPY arch/arm/boot/zImage
      Kernel: arch/arm/boot/zImage is ready
      在执行make命令前,你也可以执行make menuconfig先配置一下编译选项。
     四. 在模拟器中运行编译好的内核。
     1. 在启动模拟器之前,先设置模拟器的目录到环境变量$PATH中去:
      USER-NAME@MACHINE-NAME:~/Android$ export PATH=$PATH:~/Android/out/host/linux-x86/bin
      2. 设置ANDROID_PRODUCT_OUT环境变量
      USER-NAME@MACHINE-NAME:~/Android$ export ANDROID_PRODUCT_OUT=~/Android/out/target/product/generic
      3. 在后台中指定内核文件启动模拟器:
       USER-NAME@MACHINE-NAME:~/Android$  emulator -kernel ./kernel/common/arch/arm/boot/zImage &
      4. 用adb工具连接模拟器,查看内核版本信息,看看模拟器上跑的内核是不是我们刚才编译出来的内核:
      USER-NAME@MACHINE-NAME:~/Android$ adb shell
      这时候如果是第一次运行 adb shell命令,会看到以下输出,不用管它,再运行一次adb shell命令就可以了。
      * daemon not running. start it now on port 5037 *
      * daemon started successfully *
      error: device offline
      切换到proc目录:
      root@android:/ # cd proc
      root@android:/proc # cat version
      Linux version 2.6.29-gb0d93fb-dirty (luo@ubuntu-11-04) (gcc version 4.4.3 (GCC) ) #1 Fri Jun 3 23:17:24 HKT 2011
    从机器名luo@ubuntu-11-04和日期1 Fri Jun 3 23:17:24 HKT 2011可以看出,模拟器使用的内核即为刚刚编译出来的内核。


链接http://android.git.kernel.org/repo已经失效了,用这个https://dl-ssl.google.com/dl/googlesource/git-repo/repo
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值