Android开发环境搭建_编译_运行

学习于 百问网韦东山在线课堂

1 Android 开发环境搭建

1.1 环境要求

Android 源码下载时,会包含 Git 代码库,所以要检出代码,至少需要 250 GB 可用磁盘空间;如果要进行构建,则还需要 150 GB。如果要进行多次构建,则需要更多空间。环境要求如下:

  • CPU:x86核越多越好
  • RAM:16G
  • 磁盘:400G
  • 操作系统:Ubuntu 18.04
  • 操作系统交换分区: 4G 以上

从 2021 年 6 月起,Google 使用 72 核机器,内置 RAM 为 64 GB,完整构建过程大约需要 40 分钟。

1.2 环境搭建

1.2.1 安装OpenJDK
$ sudo apt-get update
$ sudo apt-get install openjdk-8-jdk
1.2.2 安装所需的软件包 (Ubuntu 18.04)
sudo apt-get install git-core gnupg flex bison build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z1-dev libgl1-mesa-dev libxml2-utils xsltproc unzip fontconfig
1.2.3 芯片原厂支持

一般芯片原厂会先拿到 Android 源码在自己的芯片平台上适配,然后客户会从芯片原厂拿到 SDK 来开发产品。芯片原厂会提供开发文档,会有搭建开发环境的用户手册,按照手册上做就行。

2 Android 源码下载

2.1 版本说明

谷歌发布新版本源码之后,芯片原厂会根据自己的芯片特性进行适配,因为原厂适配 Android 系统更专业和定,所以在实际开发中,都是从原厂或者方案公司那里获取到针对某个特定芯片的特定版本源码,芯片原厂适配的会长一些,作为个人而言,如果想更早的获取到 Android 源码,提前了解最新版本源码和技术,可以直接去下载谷歌官方的源码。

Android 版本说明:官网地址

Android O(8.0) 之后的版本, 相比之前的版本,设计了新的系统框架, 名为 Treble 项目,将框架和厂商实现部分严格隔离出来,如 HIDLVNDK 技术, selinux 权限管控, 并新增加了很多功能技术, 如 System-as-root,强制 A/B 设备, 动态分区,APEX 模块化升级等。

2.2 源码下载

Android 源码中有非常多的 git 子仓库,为了方便有效的管理多 git 仓库,谷歌利用 repo 对所有 git 仓库进行管理,并且让用户通过 repo 工具批量下载 Android 源码,所以下载 Android 源码之前必须先安装 repo 工具,同时需要注意的是,因为国内网络的问题,无法直接访问谷歌安卓源码服务器,所以国内都是通过 清华大学开源软件镜像站 或者 中科大 下载。同时 谷歌官方下载地址

这里是参考 AOSP 镜像使用帮助

2.3.1 安装 Repo
  • 确保主目录中有一个 bin/ 目录,并且它包含在 PATCH

    $ mkdir ~/bin
    $ PATH=~/bin:$PATH
    
  • 下载 Repo 启动器,并确保它可执行

    $ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
    ## 如果上述 URL 不可访问,可以用下面的:
    ## curl -sSL  'https://gerrit-googlesource.proxy.ustclug.org/git-repo/+/master/repo?format=TEXT' |base64 -d > ~/bin/repo
    $ chmod a+x ~/bin/repo
    
2.3.2 配置 Repo,下载
  • 创建一个空目录来存工作文件

    $ mkdir android10
    $ cd android10
    
  • 添加账户

    $ git config --global user.name relax
    $ git config --global user.email wt.relax@foxmail.com
    
  • 指定 repo 仓库服务器

    $ sudo vim ~/bin/repo
    # 把 REPO_URL 一行替换成下面的
    # REPO_URL = 'https://gerrit-googlesource.proxy.ustclug.org/git-repo'
    
    # 或者
    $ export REPO_URL='https://gerrit-googlesource.proxy.ustclug.org/git-repo'
    
  • 下载 repo 仓库,并指定 android 版本

    $ repo init -u git://mirrors.ustc.edu.cn/aosp/platform/manifest -b android-10.0.0_r45
    
  • 下载 android 源码

    $ repo sync -c -j8
    

2.4 错误处理

2.4.1 没有 Python
  • 错误

    /usr/bin/env: 'python': No such file or directory
    
  • 解决方案

    $ apt-get install python
    
2.4.2 Python 版本过低
  • 错误

    repo: error: Python 2 is no longer supported; Please upgrade to Python 3.5+.
    
  • 解决方案

    #1、下载Python3,会下载在当前目录,所以要提前选好目录
    $ wget http://www.python.org/ftp/python/3.9.5/Python-3.9.5.tgz
    
    #2、下载完成后,解压 
    $ tar -xvzf Python-3.9.5.tgz
    
    #3、解压完成后,切换到Python3文件夹中 
    $ cd ./Python-3.9.5
    
    #4、分别执行 
    $ ./configure --with-ssl
    $ make
    $ make install
    
    #5、输入Python3验证 
    $ python3
    
2.4.3 repo init 报错
  • 错误

    Downloading Repo source from https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/
    fatal: Cannot get https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/clone.bundle
    fatal: error unknown url type: httpsfatal: cloning the git-repo repository failed, will remove '.repo/repo'
    
  • 解决方案

    $ apt-get install openssh-server
    

2.5 Repo 工作原理讲解

谷歌其实是建立了一个 manifests 仓库来维护 Android 源码,而 mainfests 仓库保存这一个 default.xml 文件,该文件就记录了 Android 源码中的各个子仓库的地址,以及下载到本地哪个地方,repo 是用 python 编写的一个脚本,repo 脚本就会根据 default.xml 中配置进行批量下载。

manifest 的仓库可以单独下载

$ git clone git://mirrors.ustc.edu.cn/aosp/platform/manifest

可以自行查看 .repo/manifests/default.xml 的内容。管理的工程可以单独下载,例如build目录

$ git clone https://mirrors.ustc.edu.cn/aosp/platform/build.git

3 Android 源码编译

3.1 编译步骤

  • 使用 envsetup.sh 脚本初始化环境

    $ source build/envsetup.sh
    
  • 选择产品

    $ lunch
    You're building on LinuxLunch menu... pick a combo:	
    
    		# ......	
    		26. aosp_x86-eng
    		27. aosp_x86_64-eng	
    		28. # ......	
    		29. poplar-user	
    		30. poplar-userdebug	
    		31. qemu_trusty_arm64-userdebug
    		
    		Which would you like? [aosp_arm-eng]  #选择 27
    
  • 编译

    $ make -j6
    

3.2 错误处理

3.2.1 rsync: command not found
  • 错误

    FAILED: out/target/product/generic_x86_64/ramdisk-debug.img
    /bin/bash -c "(mkdir -p out/target/product/generic_x86_64/debug_ramdisk ) && (touch out/target/product/generic_x86_64/debug_ramdisk/force_debuggable ) && (rsync -a out/target/product/generic_x86_64/ramdisk/ out/target/product/generic_x86_64/debug_ramdisk ) && (out/host/linux-x86/bin/mkbootfs -d out/target/product/generic_x86_64/system out/target/product/generic_x86_64/debug_ramdisk | out/host/linux-x86/bin/minigzip > out/target/product/generic_x86_64/ramdisk-debug.img )"
    /bin/bash: rsync: command not found
    00:38:41 ninja failed with: exit status 1
    
  • 解决方案

    $ apt-get install rsync
    

3.3 构建变体

参考 添加新设备类型
在这里插入图片描述

4 AOSP 镜像运行

4.1 emulator

Android 源码编译出来的镜像,可以通过 Android 自带的 emulator 来运行。emulator 就是平常所说的模拟器,在用户没有真机的情况,通过启动模拟器来完成对系统和程序的调试和开发。

emulator 内部是基于 Qemu+KVM 虚拟化技术,通过 Qemu 模拟出 CPU ,硬件总线和外设,emulator 在传统的 qemu 技术上,增加了各种传感器的模拟,如重力传感器,gps,电话模块等,从而实现模拟出一个虚拟机的手机。

4.2 运行

  • emulator 启动

    $ source build/envsetup.sh
    $ lunch aosp_x86_64-eng
    $ emulator -verbose
    
  • 启动过程中会打印如下信息

    kernel.path = /mnt/ext-disk1/a10-aosp/out/target/product/generic_x86_64/kernel-ranchudisk.ramdisk.path = /mnt/ext-disk1/a10-aosp/out/target/product/generic_x86_64/ramdisk.imgdisk.systemPartition.path = /mnt/ext-disk1/a10-aosp/out/target/product/generic_x86_64/system-qemu.imgdisk.systemPartition.size = 3083mdisk.vendorPartition.path = /mnt/ext-disk1/a10-aosp/out/target/product/generic_x86_64/vendor-qemu.imgdisk.vendorPartition.size = 800mdisk.dataPartition.path = /mnt/ext-disk1/a10-aosp/out/target/product/generic_x86_64/userdata-qemu.imgdisk.dataPartition.size = 2gdisk.encryptionKeyPartition.path = /mnt/ext-disk1/a10-aosp/out/target/product/generic_x86_64/encryptionkey.imgVERBOSE: AdbInterface.cpp:234: Adb: /mnt/ext-disk1/a10-aosp/out/soong/host/linux-x86/bin/adb
    

4.3 错误处理

  • 错误

    emulator: CPU Acceleration: DISABLED
    emulator: CPU Acceleration status: /dev/kvm is not found: VT disabled in BIOS or KVM kernel module not loaded
    emulator: ERROR: x86_64 emulation currently requires hardware acceleration!
    Please ensure KVM is properly installed and usable.
    CPU acceleration status: /dev/kvm is not found: VT disabled in BIOS or KVM kernel module not loaded
    
  • 解决方案

    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值