目前是Android应用开发,想学习下系统开发,本以为照着教程来会很简单,没想到踩了挺多坑,光编译运行成功前前后后花费了差不多一个星期左右,特此记录下
一、环境搭建
1.1 Ubuntu
因为电脑只有一台,故选择虚拟机运行Ubuntu的镜像
Ubuntu选择的是Ubuntu18.04
虚拟机VMware
- 内存:4G
- 处理器数量:4 、内核数量:2
- 硬盘:500G(硬盘要大点,光源码都要36G多)
如果编译完要运行模拟器的话,记得把 虚拟化Intel VT-x/EPT 或 AMD-V/RVI(V) 选项给勾选上
1.1.1 安装Vim
由于我们需要经常去改动代码,所以第一步需要去安装Vim编辑器,输入以下命令安装:
sudo apt-get install vim
1.1.2 更换仓库源
这里我换的是清华仓库镜像,修改sources.list
配置文件:
sudo vim /etc/apt/sources.list
//把原来的内容删掉,清华源复制上去,保存退出
更新缓存:
sudo apt-get update
sudo apt-get upgrade
//至此完成国内源的切换
1.1.3 安装Open-JDK 8
sudo apt-get update
sudo apt-get install openjdk-8-jdk
1.1.4 安装依赖包
//使用 ubuntu 14+,需要安装以下依赖包
sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev ccache libgl1-mesa-dev libxml2-utils xsltproc unzip
1.1.5 更改交换空间内存
Linux输入free -h
命令,可以看到Swap分区只有2个G,太小会导致编译中断,我这边是从Ubuntu的整个磁盘空间分16个G给Swap分区
swapoff /swapfile
rm /swapfile -rf
vi /etc/fstab //注释代码,UUID下面那行最前面加#
然后照着这个链接更换Swap分区大小
弄完后输入free -h
查看到Swap分区变成16G就表明更换完成了,至此环境就已经搭建的差不多了,咱这也只是刚开头
二 、aosp源码下载
2.1 安装Git
sudo apt-get install git
创建bin,并加入到PATH中:
mkdir ~/bin
PATH=~/bin:$PATH
2.2 安装CURL库
sudo apt-get install curl
2.3 repo安装并授予权限
curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo
chmod a+x ~/bin/repo
2.4 安装Python(Ubuntu自带有的话可跳过)
//repo初始化的时候会用到
sudo apt-get install python
2.5 下载源码
建立工作目录 :
mkdir aosp9.0
cd aosp9.0
repo默认是使用Google源更新,这里我们去更换成tuna的镜像源,将如下内容复制到你的~/.bashrc里:
vim ~/.bash
//编辑.bash这个文件
//将以下的内容复制进去,保存退出
export PATH=~/bin:$PATH
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'
//推出后执行命令使更改生效
source ~/.bashrc
添加身份,自己的姓名跟邮箱
git config --global user.email "YourEmailAddress@gmail.com"
git config --global user.name "YourName"
初始化仓库:
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest
初始化并且指定到特定分支(不用全部下载下来,节省空间)
//这里选择的是r43,尽量选择新的分支,能避免很多问题
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-9.0.0_r43
同步源码
repo sync -c -j8
如果同步时报错error: Cannot checkout...或者报not a git repository相关的,我建议是换个新点的分支去同步
三、编译
下载完成之后我们就可以开始编译了
//进入到aosp9.0目录下
cd aosp9.0
//配置编译环境
source build/envsetup.sh
// 编译前删除build文件夹A
make clobber
//选择编译目标,这里要跑模拟器,选择的是 5,图方便的话可以直接输入lunch 5
lunch
5
//编译
make -j6
编译速度得看虚拟机配置了,我们能做的就是看看编译期间出现什么错误,然后去解决
一般出现的错误有Java虚拟机内存不够Exception in thread "main" java.lang.OutofMemoryError: Java heap space
,输入以下命令:
declare -x _JAVA_OPTIONS="-Xmx8192m"
declare -x JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx8192m"
//查看Java虚拟机内存有没有变大
//有一种是通过JackServer去修改Java虚拟机内存的方式,我这边没见到JackServer,就没去用那种方式
java -version
然后重新输入make -j6
编译命令编译
四、运行虚拟机
源码编译完成后输入emulator
去启动模拟器
- 遇到
emulator: ERROR: x86_64 emulation currently requires hardware acceleration! Please ensure KVM is properly installed and usable. CPU acceleration status: KVM is not installed on this machine (/dev/kvm is missing).
错误
解决办法:关闭虚拟机,把Vm虚拟机的 虚拟化Intel VT-x/EPT 或 AMD-V/RVI(V) 选项勾上
- 遇到
emulator: ERROR: x86 emulation currently requires hardware acceleration! Please ensure KVM is properly installed and usable. CPU acceleration status: This user doesn't have permissions to use KVM (/dev/kvm).
- 或者提示
emulator: ERROR: x86 emulation currently requires hardware acceleration! Please ensure KVM is properly installed and usable. CPU acceleration status: Could not open /dev/kvm :Permission denied
解决办法:
使用 root 权限执行以下四条命令后注销当前用户或者直接重启电脑即可
su root
groupadd kvm
usermod -G kvm -a lin(lin这里是我的用户名,需要更改成你自己的)
echo 'KERNEL=="kvm",GROUP="kvm",MODE="0660"' >> /etc/udev/rules.d/androidUseKVM.rules
这三条命令的作用分别是:
#创建用户组kvm
#将用户sman添加到kvm用户组
#开机时自动赋予用户组kvm拥有0660的权限
输入emulator执行 最后附一张运行成功的图
有问题的地方欢迎指正,感谢:
Android AOSP基础(二)是时候下载Android9.0源码了