zoukankan      html  css  js  c++  java
  • 【转】在Ubuntu下编译Android源码并运行Emulator

    原文网址:http://www.mcuos.com/thread-4553-1-1.html

    建立编译环境

    1.在VirtualBox上安装Ubuntu

    2.安装JDK
       $ sudo apt-get install sun-java5-jdk  或
       $ sudo apt-get install sun-java6-jdk (donut 1.6)
    3.安装flex,bison,gperf,libsdl-dev,libesd0-dev,libwxgtk2.6-dev(可选),build-essential,zip,curl。
       $ sudo apt-get install flex bison gperf libsdl-dev libesd0-dev libwxgtk2.6-dev build-essential zip curl libncurses5-dev zlib1g-dev
    4.安装Valgrind(可选),此工具可以帮你查找内存泄漏、堆栈破坏以及数组访问越界等错误。
       $ sudo apt-get install valgrind
    5.下载Android源代码

    从Android官方下载donut分支的Android源代码
    sudo apt-get install git-core curl
    curl http://android.git.kernel.org/repo >~/bin/repo
    chmod a+x ~/bin/repo

    cd ~/bin/
       $ ./repo init -u git://android.git.kernel.org/platform/manifest.git -b android-1.6_r2
       $ ./repo sync

    6.在 ~/.bashrc 加环境变量。
       $ vim ~/.bashrc
    在.bashrc文件的最后面加入如下2行,即将JDK的安装路径加入到环境变量中
           export JAVA_HOME=/usr/lib/jvm/java-6-sun
           export ANDROID_JAVA_HOME=$JAVA_HOME
    完成后,$source ~/.bashrc即可生效。

    运行   $ java -version 将出现下面提示:
          [root@Neil java]# java -version
          java version "1.6.0_11"
          Java(TM) SE Runtime Environment (build 1.6.0_11-b03)
          Java HotSpot(TM) Client VM (build 11.0-b16, mixed mode, sharing)
          [root@Neil java]# 
    说明Java已经成功被安装了

    通过
    $ update-alternatives --config javadoc
    $ sudo update-alternatives --config java
    $ sudo update-alternatives --config javac
    然后选一个编号,就切换过去了。



    编译Android  

    $ cd android            //(进入android目录)
      $ make                  //(编译android)

    注意:编译时要确保系统的内存在1.2GM以上,且有足够的硬盘可用空间,否则会编译失败终止。
        Android编译环境提供了”showcommands”选项来显示编译命令行,如:
        $ make showcommands


    1.5_r2版编译出错的解决方法:
    The Android code contains a bug that hasn’t been solved up to the date of this article.
    So before you start compiling the code, you’ll need a few modifications, or the build will fail (after consuming some of your time and patience). The error is:
    external/qemu/sockets.c: In function 'sock_address_init_resolve':
    external/qemu/sockets.c:637: error: 'EAI_NODATA' undeclared (first use
    in this function)
    external/qemu/sockets.c:637: error: (Each undeclared identifier is
    reported only once
    external/qemu/sockets.c:637: error: for each function it appears in.)
    make: *** [out/host/linux-x86/obj/EXECUTABLES/emulator_intermediates/
    sockets.o] Error 1
    To fix this, before compiling the android code, open ~/mydroid/external/qemu/sockets.c and add
    #define __USE_GNU
    just before the #include <netdb.h>

    新编译的system.img不能正常使用sdcard 的解决办法:
        $ mount -t vfat /dev/block//vold/179:0 /sdcard
        也可以更改system/core/vold程序


    运行emulator
    我们这里使用已经编译好的模拟器kernel镜像,即kernel-qemu,位于android/prebuilt/android-arm/kernel目录下,然后运行模拟器。

    1.在 ~/.bashrc 加环境变量。(本项可选)
       $ vim ~/.bashrc
    在.bashrc文件的最后面加入如下2行,即将Android工具的路径
       export PATH=/archer/external/android/out/host/linux-x86/binPATH
       export ANDROID_PRODUCT_OUT=/archer/external/android/out/target/product/generic

       export ANDROID_SWT=/android/out/host/linux-x86/framework 

    2.创建Android虚拟设备
       $ cd android/out/host/linux-x86/bin    (进入模拟器程序所在目录)
       $ ./android create avd -t 2 -n g1
    其中 -t 指定TargetID (Android 1.5 SDK的ID为2,Android 1.0 SDK的ID为1),-n指定创建的Android虚拟设备名字。

    3.运行emulator
       $ cd android/out/host/linux-x86/bin    (进入模拟器程序所在目录)
       $ ./emulator -avd g1                     或者
       $ ./emulator -avd g1 -scale 0.8
    其中 -avd指定Android设备名,-scale指定缩放比例。
    按 Ctrl + F12 可以使模拟器屏幕旋转90度,即横屏、竖屏切换。
    4.删除创建的Android虚拟设备
       $ ./android delete avd -n g1


    编译linux kernel


    直接编译Android源码时,并没有编译 linux kernel。如果只运行模拟器,不用编译 linux kernel。
    从emulator获取内核编译参数的配置文件:
      启动模拟器
      $ adb pull /proc/config.gz .
       解压缩config.gz  $ gzip -d config.gz
       将config文件替换kernel文件夹下的.config文件。
    根据需要,修订config与Makefile配置文件


    编译模块

    Android中的一个应用程序可以单独编译,编译后需要重新生成system.img。
    在Android目录下运行 
    $ . build/envsetup.sh  或者 
    $ source build/envsetup.sh ,然后就会多出几个可用的命令:

    - 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.
    - printconfig: 当前build的配置情况.
    可以使用 --help查看用法。
    如:在修改了某一个模块以后,可以使用 $ mmm <目录>  来重新编译所有在<目录>中的所有模块,使用 $ mm  编译当前目录中的所有模块。
    编完之后,即修改了Android系统以后,可以使用 $ make snod 重新生成system.img。
  • 相关阅读:
    多线程
    JavaSE
    SpringBoot是什么,可以做什么?
    SSM框架中如何简便上传文件表单
    SQL语句全解,非常棒!
    关于HttpSession 和 Hibernate框架中 session异同点的简单解析
    关于Javascript中页面动态钟表的简单实现
    Java 面向对象 知识点基础浅谈
    Eclipse无法正常启动,弹出对话框内容为 A Java Runtime...
    关于Java中面向对象章节、IO 流中的重点基础知识。
  • 原文地址:https://www.cnblogs.com/wi100sh/p/4338219.html
Copyright © 2011-2022 走看看