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

        看完了前面说的几本书之后,对Linux Kernel和Android有一定的认识了,是不是心里蠢蠢欲动,想小试牛刀自己编译一把Android源代码了呢?一直习惯使用Windows系统,而Android源代码是不支持在Windows上编译上,于是决定使用虚拟机安装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上执行以下命令即可:

     [email protected]:~$ sudo apt-get install git-core gnupg

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

     [email protected]:~$  sudo add-apt-repository ppa:ferramroberto/java       [email protected]:~$  sudo apt-get update      [email protected]:~$  sudo apt-get install sun-java6-jre sun-java6-plugin      [email protected]:~$  sudo apt-get install sun-java6-jdk                    6. 依赖的其它包。在Ubuntu上执行以下命令:
     [email protected]:~$ sudo apt-get install flex bison gperf libsdl-dev libesd0-dev libwxgtk2.6-dev build-essential zip curl
     7. 调试工具。在Ubuntu上执行以下命令:
     [email protected]:~$ sudo apt-get install valgrind
    二. 下载Android源代码工程。
     1. 下载repo工具。在Ubuntu上执行以下命令:
     [email protected]:~$ wget http://android.git.kernel.org/repo      [email protected]:~$ chmod 777 repo      [email protected]:~$ cp repo /bin/      2. 下载Android最新版本源代码。在Ubuntu上执行以下命令:

     [email protected]:~$ mkdir Android

     [email protected]:~$ cd Android      [email protected]:~/Android$ repo init -u git:// android.git.kernel.org/platform/manifest.git      [email protected]:~/Android$ repo sync      经过漫长的等待(我下载了两三天)后,就可以把Android源代码下载下来了。其间可能还有经历下载中断的情况,这时只要重新执行repo sync就可以了。      三. 编译Android源代码。      1. 编译。在Android目录下执行以下命令:      [email protected]:~/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       2. 编译过程中可能会遇到的问题。      问题一:You are attempting to build on a 32-bit system.      两个地方需要个修改:      1)修改build/core目录下的main.mk文件:      ifeq ($(BUILD_OS),linux)      build_arch := $(shell uname -m)      #Change the following line for building on a 32-bit system.      #ifneq (64,$(findstring 64,$(build_arch)))      ifneq (i686,$(findstring i686,$(build_arch)))      $(warning ************************************************************)      $(warning You are attempting to build on a 32-bit system.)      $(warning Only 64-bit build environments are supported beyond froyo/2.2.)      2)找到下列文件:      /external/clearsilver/cgi/Android.mk      /external/clearsilver/cs/Android.mk      /external/clearsilver/java-jni/Android.mk      /external/clearsilver/util/Android.mk      修改LOCAL_CFLAGS和LOCAL_LDFLAGS变量:      # This forces a 64-bit build for Java6      # Change the following two lines for building on a 32-bit system.      # LOCAL_CFLAGS += -m64      # LOCAL_LDFLAGS += -m64      LOCAL_CFLAGS += -m32      LOCAL_LDFLAGS += -m32      问题二: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,这一步是可选的。      1. 编译。执行以下命令:      [email protected]:~/Android$ make sdk      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即可:       [email protected]:~/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. 设置环境变量:      [email protected]:~/Android$ export PATH=$PATH:~/Android/out/host/linux-x86/bin        [email protected]:~/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. 运行模拟器。      [email protected]:~/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时,即:     [email protected]:~/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镜像,敬请期待~  


PS:主线上最新源代码是不稳定版本,使用过程可能会有问题

在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)

      在前一篇文章提到,从源代码树下载下来的最新Android源代码,是不包括内核代码的,也就是Android源代码工程默认不包含Linux Kernel代码,而是使用预先编译好的内核,也就是prebuilt/android-arm/kernel/kernel-qemu文件。那么,如何才能DIY自己的内核呢?这篇文章一一道来。

      一. 首选,参照前一篇在Ubuntu上下载、编译和安装Android最新源代码准备好Android源代码目录。

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

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

      [email protected]:~/Android$ mkdir kernel

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

如何单独编译Android源代码中的模块

      第一次下载好Android源代码工程后,我们通常是在Android源代码工程目录下执行make命令,经过漫长的等待之后,就可以得到Android系统镜像system.img了。以后如果我们修改了Android源代码中的某个模块或者在Android源代码工程新增一个自己的模块,是不是还是执行make命令呢?答案是否定的,Google为我们准备了另外的命令来支持编译单独的模块,以及重新打包system.img的命令。在继续学习Android源代码之前,就让我们先来看看这个命令吧。

      一. 首先在Android源代码目录下的build目录下,有个脚本文件envsetup.sh,执行这个脚本文件后,就可以获得一些有用的工具:

      [email protected]:~/Android$ .  ./build/envsetup.sh

      注意,这是一个source命令,执行之后,就会有一些额外的命令可以使用:

      - croot: Changes directory to the top of the tree.

      - m: Makes from the top of the tree.       - mm: Builds all of the modules in the current directory.       - mmm: Builds all of the modules in the supplied directories.       - cgrep: Greps on all local C/C++ files.       - jgrep: Greps on all local Java files.       - resgrep: Greps on all local res/*.xml files.       - godir: Go to the directory containing a file.       这些命令的具体用法,可以在命令的后面加-help来查看,这里我们只关注mmm命令,也就是可以用它来编译指定目录的所有模块,通常这个目录只包含一个模块。       二. 使用mmm命令来编译指定的模块,例如Email应用程序:        [email protected]:~/Android$ mmm packages/apps/Email/       编译完成之后,就可以在out/target/product/generic/system/app目录下看到Email.apk文件了。Android系统自带的App都放在这具目录下。另外,Android系统的一些可执行文件,例如C编译的可执行文件,放在out/target/product/generic/system/bin目录下,动态链接库文件放在out/target/product/generic/system/lib目录下,out/target/product/generic/system/lib/hw目录存放的是硬件抽象层(HAL)接口文件,后面的文章里面,我们会陆续提及到,敬请关注。       三. 编译好模块后,还要重新打包一下system.img文件,这样我们把system.img运行在模拟器上时,就可以看到我们的程序了。        [email protected]:~/Android$ make snod       四. 参照 Ubuntu上下载、编译和安装Android最新源代码一文介绍的方法运行模拟器:        [email protected]:~/Android$ emulator       这样一切就搞定了。 来自: http://blog.csdn.net/luoshengyang/article/details/6566662

 

更多相关文章

  1. android 常用的调试方法
  2. (二) RIL 层分析
  3. adb常用命令、monkey测试
  4. Android(安卓)studio获取sha1
  5. 安装Termux的手机上运行Python
  6. 浅谈Android系统开发中LOG的使用
  7. android内核编译方法[转贴]
  8. 初识 Android(安卓)Native Development Kit (NDK)
  9. Android(安卓)系统架构

随机推荐

  1. Android的BUG(三) - 广为人知的诟病:频繁重
  2. Android(安卓)EditView属性详细介绍
  3. Android(安卓)activity启动模式理解
  4. Android使用Thread+Handler实现非UI线程
  5. 10个android开源项目
  6. 打造android ORM框架opendroid(四)——优
  7. 【Android手机游戏】贪吃蛇(1)
  8. Android(安卓)面试题
  9. Android四款系统架构工具
  10. Android(安卓)中屏幕点击事件的实现