搭建Android源码调试环境(一)——下载编译Android源码

1. 环境准备工作

  1. 运行Ubuntu18.04系统的主机一台,或者更新的版本应该也可以。(我这里使用的是Linux mint 18.3,Ubuntu的衍生版本)
    内存配置越高越好。我的是i7-4710HQ + 16G内存
  2. 磁盘空间至少200G。
    源码,编译产生的文件,ccache加起来会占用很多空间
  3. 安装依赖组件
    sudo apt-get install libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-dev g++-multilib 
    sudo apt-get install -y git flex bison gperf build-essential libncurses5-dev:i386 
    sudo apt-get install tofrodos python-markdown libxml2-utils xsltproc zlib1g-dev:i386 
    sudo apt-get install dpkg-dev libsdl1.2-dev libesd0-dev
    sudo apt-get install git-core gnupg flex bison gperf build-essential  
    sudo apt-get install zip curl zlib1g-dev gcc-multilib g++-multilib 
    sudo apt-get install libc6-dev-i386 
    sudo apt-get install lib32ncurses5-dev x11proto-core-dev libx11-dev 
    sudo apt-get install libgl1-mesa-dev libxml2-utils xsltproc unzip m4
    sudo apt-get install lib32z-dev ccache
    
  4. 安装配置openjdk
    安装 openjdk8 (Android8.0使用)
    sudo apt-get update
    sudo apt-get install openjdk-8-jdk
    
    如果有多个jdk,则需要切换到 openjdk8,可以使用如下命令进行切换。也可以将openjdk8路径加入到PATH环境变量头部
      sudo update-alternative --config java
      sudo update-alternative --config javac
    
    验证是否已经是openjdk 8
    kevin@kevin-GS60-2PL /mnt/2ffc0bac-5896-499a-9ae6-79e610162482/aosp $ java -version
    openjdk version "1.8.0_191"
    OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-0ubuntu0.16.04.1-b12)
    OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
    
  5. 配置git
    设置 git 用户名和邮箱
    示例如下:
    git config --global user.email "[email protected]"
    git config --global user.name "Your Name"
    
  6. 设置ccache
    export USE_CCACHE=1
    export CCACHE_DIR=/<path_of_your_choice>/.ccache
    prebuilts/misc/linux-x86/ccache/ccache -M 50G
    
    建议的缓存大小为 50G 到 100G。
    请将以下内容添加到 .bashrc中:
    export USE_CCACHE=1

2. repo 下载

下载 repo 工具:

mkdir ~/bin
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo

3. 下载源码

新建工作目录

mkdir aosp
cd aosp

初始化仓库(我这里同步的是 android-8.1.0_r53)

repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-8.1.0_r53

同步源码

repo sync

同步源码的时间依网速而定

4. 编译

4.1 初始化编译环境
source build/envsetup.sh 
或者
. build/envsetup.sh 

选择构建目标,我这里构建x86的,以便模拟器获得更快的运行速度

lunch aosp_x86-eng

通过lunch指令设置编译目标,所谓的编译目标就是生成的镜像要运行在什么样的设备上.这里我们设置的编译目标是aosp_x86-eng,因此执行指令:
lunch aosp_x86-eng
编译目标格式说明
编译目标的格式:BUILD-BUILDTYPE,比如上面的aosp_x86-eng的BUILD是aosp_x86,BUILDTYPE是eng.

什么是BUILD

BUILD指的是特定功能的组合的特定名称,即表示编译出的镜像可以运行在什么环境.其中,aosp(Android Open Source Project)代表Android开源项目;arm表示系统是运行在arm架构的处理器上,arm64则是指64位arm架构;处理器,x86则表示x86架构的处理器;此外,还有一些单词代表了特定的Nexus设备,下面是常用的设备代码和编译目标,更多参考官方文档

受型号 设备代码 编译目标
Nexus 6P angler aosp_angler-userdebug
Nexus 5X bullhead aosp_bullhead-userdebug
Nexus 6 shamu aosp_shamu-userdebug
Nexus 5 hammerhead aosp_hammerhead-userdebug

提示:如果你没有Nexus设备,那么通常选择arm或者x86即可

什么是BUILDTYPE

BUILD TYPE则指的是编译类型,通常有三种:
-user:代表这是编译出的系统镜像是可以用来正式发布到市场的版本,其权限是被限制的(如,没有root权限,不鞥年dedug等)
-userdebug:在user版本的基础上开放了root权限和debug权限.
-eng:代表engineer,也就是所谓的开发工程师的版本,拥有最大的权限(root等),此外还附带了许多debug工具

4.2 开始编译
make -j8 

一般-j设置core*2就可以了,我的是四核八线程处理器,所以就是-j8

每次打开新的terminal切换到bash后都得将4.1执行一遍,才可以开始编译

模块编译

除了通过make命令可以编译整个android源码外,Google也为我们提供了相应的命令来支持单独模块的编译.

编译环境初始化(即执行source build/envsetup.sh)之后,我们可以得到一些有用的指令,除了上边用到的lunch,还有以下:

  • 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.
    其中mmm指令就是用来编译指定目录.通常来说,每个目录只包含一个模块.比如这里我们要编译Launcher2模块,执行指令:

mmm packages/apps/Launcher2/
稍等一会之后,如果提示:
make completed success fully
即表示编译完成,此时在out/target/product/generic_x86/system/priv-app/Launcher2就可以看到编译的Launcher2.apk文件了.

重新打包系统镜像
编译好指定模块后,如果我们想要将该模块对应的apk集成到系统镜像中,需要借助make snod指令重新打包系统镜像,这样我们新生成的system.img中就包含了刚才编译的Launcher2模块了.重启模拟器之后生效.

单独安装模块
我们在不断的修改某些模块,总不能每次编译完成后都要重新打包system.img,然后重启手机吧?有没有什么简单的方法呢?
在编译完后,借助adb install命令直接将生成的apk文件安装到设备上即可,相比使用make snod,会节省很多事件.

5. 运行模拟器

如果在新的终端打开,需要将4.1执行一遍,才能使用emulator启动模拟器

emulator

如果需要加入sdcard,则执行如下命令生成sdcard镜像

mksdcard -l sdcard 1024M ./out/target/product/generic_x86/sdcard.img

然后启动模拟器使用如下参数

emulator -memory 1024 -partition-size 3000 -sdcard sdcard.img

你可能感兴趣的:(android)