赞
踩
开启Android Framework之旅,一步步记录自己学习过程。
我自己电脑配置是16G + 1T,算是 满足最低要求,想着无非编译慢点,先搞起来再说。
采用 VirtualBox + Ubuntu 的方式。
虚拟机安装时,内存分配了8G + 450G,处理器设置为4核,多了担心主机会卡,运行内存8G不够,增加Swap交换空间来凑,后续会有介绍。
安装完ubuntu系统后,会带有一些不需要的软件,把用不到的全都卸载掉。
sudo命令用户可能没有权限,使用root权限编辑 /etc/sudoer 中的内容,添加用户sudo权限。
root ALL=(ALL:ALL) ALL
mx ALL=(ALL:ALL) ALL
因为Ubuntu默认的软件仓库地址在国内可能会方便比较慢,甚至会失败的情况,所以下载软件之前需要先设置一下软件仓库镜像源。
使用该指令打开文件
#
sudo gedit /etc/apt/sources.list
将下面内容粘贴进去
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu/ bionic-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ bionic-security main restricted universe multiverse
然后执行
sudo apt-get update
sudo apt-get -f install
sudo apt-get upgrade
由于编译Android系统需要安装很多环境,所以我直接将所需要的环境整理成了sh脚本,直接运行该脚本即可。
在Ubuntu里面新建一个env.sh脚本,将以下内容复制进去
#base tools of ubuntu sudo apt install net-tools gitk tree vim terminator synergy expect minicom cutecom #openjdk sudo apt-get install openjdk-8-jdk 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 libgl1-mesa-dev libxml2-utils xsltproc unzip libc6-dev tofrodos python3-markdown libssl-dev libxml-simple-perl mingw-w64 #install adb and fastboot sudo apt-get install android-tools-adb android-tools-fastboot #install ssh server in order to enable ssh login and scp sudo apt-get install openssh-server openssh-client #install python sudo apt-get install python python-dev python-protobuf protobuf-compiler python-virtualenv python-pip #install libssl-dev to fix such error "fatal error: openssl/opensslv.h: No such file or directory" sudo apt-get install libssl-dev #install audit2allow sudo apt-get install policycoreutils-python-utils sudo apt-get install m4 #安装perl环境,否则可能会报错Can't locate XML/Simple.pm in @INC (you may need to install the XML::Simple module) sudo cpan install XML::Simple #解决no such file or directory以及Cannot generate supplementary makefiles的问题 sudo apt install lsb sudo apt-get install lib32stdc++6 sudo apt-get install ia32-libs sudo apt-get install lib32ncurses5 sudo apt-get install lib32z1
给该脚本设置执行权限,之后执行即可
sudo chmod 777 env.sh
free -m
可以看到内存为8G,我本地扩大到了32G的虚拟内存,默认swap大小是2G的。
$ sudo -i
# 输入密码后,切换root。
$ mkdir /swap
# 建立swap文件夹
$ cd /swap/
# 进入到swap文件夹
$ sudo dd if=/dev/zero of=swapfile bs=32M count=1k
# 建立swapfile,大小为bs*count = 32M * 1k = 32G
#将生成的文件转换为Swap文件
$ sudo mkswap -f swapfile
# 最后激活swap文件
$ sudo swapon swapfile
使用 $ free -m 查看内存是否扩展成功。
设置为永久swap,否则重启后失效。
# 执行
sudo gedit /etc/fstab
#将下列内容 复制到文件的后面,保存即可
/swap/swapfile swap swap sw 0 0
Android源码同时使用git和repo进行管理,repo是基于git的代码管理工具,类似github、gitee,所以需要同时安装git和repo。
执行
sudo apt-get update
sudo apt-get install repo
安装完之后 执行
repo verison
如果提示,repo command not found 说明没安装成功。
需要接着操作:
# 1.先创建个文件夹,隐藏的 sudo mkdir ~/.bin # 设置下权限 sudo chmod 777 ~/.bin # 2.环境变量配置一下 PATH=~/bin:$PATH # 3.使用镜像下载repo #使用清华大学镜像 sudo curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/.bin/repo chmod a+x ~/bin/repo # 将repo配置到环境变量 sudo gedit ~/.bashrc # 在.bashrc文件末添加下面的内容保存退出 # repo alias repo="~/.bin/repo" # 4.同步文件变化 source .bashrc
再次执行
repo verison
# 出现下面提示,则安装成功了
<repo not installed>
repo launcher version 2.15
(from /usr/bin/repo)
创建一个放置源码的目录asop。
# 1.创建目录 mkdir aosp cd aosp export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/' # 2. 设置git用户名、邮箱 git config --global user.email “usergmail@gmail.com” git config --global user.name “username” # 3.初始化仓库,并设置下载的源码版本为安卓9 repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-9.0.0_r8 # 4.~/aosp/.repo$ 中查看 manifest.xml指定的下载url,如果指的是 <manifest> <include name="default.xml" /> </manifest> # 打开目录manifest,查看default.xml,将下载url替换成清华的镜像 https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest <remote name="aosp" fetch=".." review="https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest" /> <default revision="refs/tags/android-9.0.0_r8" remote="aosp" sync-j="4" /> ...
之后执行
#同步源码
repo sync
静静地等待下载,直到下载完成,提示
repo sync has finished successfully。
下载完成之后,方可看到完整的目录:
source build/envsetup.sh
lunch
执行完lunch会列出所有的目标版本,我这边显示50个。
不同的目标版本对应的设备也不同,因为不同的设备硬件配置也不一样,比如有的设备处理器架构是x86的,有的是arm64的,我选了第6个。
最后开始编译
执行
m
我没有设置线程数,默认自动配置。之后就是等待它编译完成。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。