安卓源码AOSP下载使用的正确姿势

安卓源码AOSP下载使用的正确姿势

从同步源码到编译完成,整个过程应至少准备200G空间。

编译时需要的内存数与编译线程数相关,博主实测比较极限的配置是4核8G,超过这个范围将触发swap交换导致编译速度急剧下降。

开始搞,注:以下*号所有内容替换成G00gle,至于为什么连单词都写错了你懂的。

0x01 前置准备

# 下载repo
sudo apt-get install repo

# 下载git并设置用户名邮箱
sudo apt-get install git
git config --global user.name "bryan sun"
git config --global user.email "hitsjt@gmail.com"

# 挑选需要同步的AOSP分支
https://source.android.*.cn/setup/build-numbers

# 配置全局git加速
git config --global url.https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/.insteadof https://android.*source.com
git config --global url."https://hub.fastgit.org".insteadOf https://github.com
# 取消全局配置
# git config --global --unset http.proxy

# 三行科普repo是个什么东西
当有了版本控制思想,你用上了git。
aosp由几百个git子项目组成,如果需要每个子项目都让你手动去挑选版本分支,然后git pull估计得崩溃。
repo作为git的控制封装脚本,可以对每个安卓分支版本需要搭配什么git子项目进行列表配置,随后批量操作这些git子项目,这就是为什么*设计了repo,一切为了操作便捷。
*在同步源码时查看文件.repo/manifests/default.xml能更好体会

# 一把安装环境
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 m4 bc -y
sudo apt-get install bc bison build-essential ccache curl flex g++-multilib gcc-multilib git gnupg gperf imagemagick lib32ncurses5-dev lib32readline-dev lib32z1-dev liblz4-tool libncurses5-dev libsdl1.2-dev libssl-dev libxml2 libxml2-utils lzop pngcrush rsync schedtool squashfs-tools xsltproc zip zlib1g-dev -y

# oracle jdk8
# https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.8.0_271/bin/java 180271
update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.8.0_271/bin/javac 180271
update-alternatives --install /usr/bin/javadoc javadoc /usr/lib/jvm/jdk1.8.0_271/bin/javadoc 180271

# openjdk8
# https://www.openlogic.com/openjdk-downloads
# 链接: https://pan.baidu.com/s/1cNtYW-bbqDEPQf8IGIk0Jw 提取码: ghdv openjdk-8u272
# 链接: https://pan.baidu.com/s/1r85kLAipjWZxG9DUaKo7sA 提取码: k9r8 openjdk-8u262
update-alternatives --install /usr/bin/java java /usr/lib/jvm/openlogic-openjdk-8u272-b10-linux-x64/bin/java 180272
update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/openlogic-openjdk-8u272-b10-linux-x64/bin/javac 180272
update-alternatives --install /usr/bin/javadoc javadoc /usr/lib/jvm/openlogic-openjdk-8u272-b10-linux-x64/bin/javadoc 180272

# 虚拟机设置,真机不用理会
apt-get install open-vm-tools-desktop fuse  # 安装vmware-tools
mkdir -p /mnt/hgfs/VMShare  # 创建挂载目录
/usr/bin/vmhgfs-fuse .host:/VMShare /mnt/hgfs/VMShare -o subtype=vmhgfs-fuse,allow_other  # 挂载共享目录,host指定的名字就是虚拟机共享目录的名字

# 设置开机自动挂载
gedit /etc/systemd/system/rc-local.service
[Unit]
 Description=/etc/rc.local Compatibility
 ConditionPathExists=/etc/rc.local

[Service]
 Type=forking
 ExecStart=/etc/rc.local start
 TimeoutSec=0
 StandardOutput=tty
 RemainAfterExit=yes
 SysVStartPriority=99

[Install]
 WantedBy=multi-user.target

gedit /etc/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

sudo /usr/bin/vmhgfs-fuse .host:/VMShare /mnt/hgfs/VMShare -o subtype=vmhgfs-fuse,allow_other

exit 0

chmod 755 /etc/rc.local  # 设置执行权限
systemctl enable rc-local.service  # 开启rc.local开机自启动服务
systemctl is-active rc-local.service  # 查看服务状态
systemctl restart rc-local.service  # 重启服务

0x02 获取AOSP源码包

# 方法1.通过下载TUNA完整增量月包,下载完后解压只需要简单操作就能获得大部分源码,剩下的同步只需要少量操作即可完成
# 博主未完整实测,下一半挂了,逼着用完整同步
https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar
https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar.md5
# 解压后得到.repo文件夹,在根目录repo sync即可迁出文件
# 如果repo sync操作出现源代码被改动提示可以用以下命令修复
repo sync -d                         # 将所有git 仓库的HEAD重置为manifest文件的指定版本
repo forall -c 'git reset --hard'    # Remove all working directory (and staged) changes.
repo forall -c 'git clean -f -d'     # Clean untracked files

# 方法2.同步完整的源码
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest --repo-url=https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/ -b android-8.1.0_r15
# 开始同步
repo sync -c -f --no-tags --no-clone-bundle -j4

-c 或者--current-branch表示只拉取当前分支代码,坑爹啊,我在init指定了分支,同步的时候,你却悄悄给我拉些没用的。
--no-tags 不拉取tags,tag虽然不大,但架不住多
--no-clone-bundle 不使用clone.bundle,clone.bundle是git bundle一样的打包文件,使用bundle文件可以做cdn下载的分流,cdn听上去不错,但是如果cdn到google的服务器,或者clone.bundle本来就占空间,不是很划算,所以不使用clone.bundle
-f 如果sync失败,继续同步
--force-sync 如果文件目录有差异,强制覆盖掉

# 附赠一个git只克隆当前分支的办法,和指定分支无关的部分将不会克隆
git clone -b mybranch --single-branch git://sub.domain.com/repo.git

# 使用7z最高压缩比打包源码
7za a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on yourfile.7z yourfile &

0x03 切换不同版本的AOSP

# 将repo配置重新指定为相关版本
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest --repo-url=https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/ -b android-6.0.1_r67

# rm -rf有个特性不会删除隐藏目录,切换版本操作也就是只需要保留.repo目录
rm -rf *

# 再次同步即可
repo sync

# 查看分支列表
cd .repo/manifests
git branch -av

# 其中分支代码也可以在这里查
https://blog.csdn.net/bt_leo/article/details/78706608

0x04 编译模拟器版本aosp并用模拟器测试

# 基于android6.0.1, Ubuntu 16.04 LTS

# 安装openjdk7,oracle java7不能被aosp识别所以不用
sudo add-apt-repository ppa:openjdk-r/ppa
# 20200419再次使用发现执行失败,进而Error: retrieving gpg key timed out.
# 然后通过你懂的方式上网就行了,Ubuntu这年头都能墙,某网部你牛逼
sudo apt-get update
sudo apt-get install openjdk-7-jdk  // OpenJdk 7安装

# 跳过CCLANG的一个不兼容选项,博主实际编译也遇到过
# 如果不修改的话会在编译libart.so的时候error停下。
修改 art/build/Android.common_build.mk 文件,定位到77行
将:
ART_HOST_CLANG := true
改为:
ART_HOST_CLANG := false

# 设置高速编译启用标志位(占硬盘,空间小可以不执行,空间换时间)
echo export USE_CCACHE=1 >> ~/.bashrc
aosp/prebuilts/misc/linux-x86/ccache/ccache -M 50G

# 导入编译环境
source build/envsetup.sh

# 设置编译选项
# lunch aosp_sailfish-user
lunch

# 开干,-j参数为编译线程数,幸运的话睡一觉起来就OK啦。
sudo make -j4

# 一键设置环境变量(该命令由build/envsetup.sh导入)
set_stuff_for_environment

# 进入安卓源码编译生成目录
cd out/target/product/generic

# 启动模拟器
emulator

0x05 编译真机并刷入

# 在官方下载驱动组件包,放在安卓源码根目录解压后运行sh脚本即可,其他流程与普通编译一致
https://developers.*.com/android/drivers

# 设置最终ROM包的编译签名为release
https://source.android.com/devices/tech/ota/sign_builds
gedit build/make/core/Makefile
BUILD_KEYS := release-keys

# 刷机
export ANDROID_PRODUCT_OUT=/root/Desktop/aosp/android10.0_r17.QP1A.191005.007.A3/out/target/product/sailfish
fastboot flashall -w

0x06 集成opengapps

# 为了在AOSP上能使用G* Play,可以选择直接集成opengapps,这样编译后刷入就能有套装
# 官方已给详细步骤说明,不再另外单独记录
# 值得一说的是各设备的配置文件路径会稍有差别,例如pixel在安卓10下路径为device/google/marlin/device-sailfish.mk
https://github.com/opengapps/aosp_build

# Product cannot have overlay in vendor tree
# gedit device/lge/bullhead/aosp_bullhead.mk
# PRODUCT_RESTRICT_VENDOR_FILES := false

0x07 跳过Factory image联网验证

重新启动后联网处即可出现跳过选项

fastboot erase frp

0x08 修正aosp不能自动同步时间/WIFI连接受限提示

adb shell settings put global ntp_server ntp1.aliyun.com
adb shell settings put global captive_portal_https_url https://connectivitycheck.platform.hicloud.com/generate_204

参考资料

[Android 编译(一)] Ubuntu 16.04 LTS 成功编译 Android 6.0 源码教程_fuchaosz的博客-CSDN博客_ubuntu 编译android

AOSP | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror

repo 工具使用手册_counsellor的博客-CSDN博客_repo sync 参数

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值