Android 源码编译

  • 一,虚拟机安装

​ 1.1 进入https://cn.ubuntu.com/download中文官网下载iso镜像

1.2  这里我们下载Ubuntu 18.04 LTS 

1.3虚拟VM机安装ubuntu系统,注意编译源码需要至少16G运行内存和400G磁盘空间,尽量设大点

二 配置编译环境

2.1 下载android源码,安装python

apt-get install python

如果是 Ubuntu 18.04 只能使用python2.x

如果是Ubuntu 20.04 则使用python3.x

由于环境变量默认python,所以用python3 -v才能获取指令。

不过我们可以合并,使用以下命令

sudo ln -s /usr/bin/python3  /usr/bin/python

这样python -v就可以获取指令了

遇到错误,无root权限

apt-get install python
E: 无法打开锁文件 /var/lib/dpkg/lock-frontend - open (13: 权限不够)
E: 无法获取 dpkg 前端锁 (/var/lib/dpkg/lock-frontend),请查看您是否正以 root 用户运行?

解决方法:

输入su root 获得权限

如果提示认证失败,则改下密码

dong@dong:~/桌面$ su root
密码:
su: 认证失败

解决方法:

修改 root 用户密码

sudo passwd root

再次输入命令apt-get install python,遇到错误,资源占用

E: 无法获得锁 /var/lib/dpkg/lock-frontend - open (11: 资源暂时不可用)
E: 无法获取 dpkg 前端锁 (/var/lib/dpkg/lock-frontend),是否有其他进程正占用它?

解决方法:

查找资源占用

ps -e|grep apt

杀死进程

sudo kill [进程id号]

然后进行强制解锁

sudo rm /var/cache/apt/archives/lock

sudo rm /var/lib/dpkg/lock

再次安装python

apt-get install python

卸载更新python版本

检查版本

python --version

删除版本

sudo apt-get remove python2

删除依赖
sudo apt-get remove --auto-remove python2
sudo apt-get purge --auto-remove python2

桌面可能依赖python,不要立马重启,可以先安装桌面
sudo apt-get install ubuntu-desktop

切换python版本,比如从2切换到3则按下面的操作

安装3

sudo apt install python3

修改权重

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150

查看当前配置

sudo update-alternatives --config python

2.2 安装Git & 配置Git信息

sudo apt-get install git
git config --global user.name dong
git config --global user.email 809459535@qq.com

2.3 安装curl

sudo apt-get install curl

如果直接下载源码会报错, 提示需要curl

dong@dong:~/桌面$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/.bin/repo

Command 'curl' not found, but can be installed with:

sudo snap install curl  # version 8.1.2, or
sudo apt  install curl  # version 7.68.0-1ubuntu2.20

See 'snap info curl' for additional versions.

我们根据提示输入

sudo snap install curl

 2.4 下载repo

apt install repo

//创建目录

mkdir ~/bin

PATH=~/bin:$PATH

curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo

chmod a+x ~/bin/repo

//打开环境变量文件

sudo vim /etc/profile

添加下面两行

export PATH=~/.bin:$PATH
export REPO_URL=https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/

//刷新缓存

source /etc/profile

下载报错

curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (35) OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to storage.googleapis.com:443

解决办法:需要fanqiang,我们切换为国内镜像:

清华镜像

curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo

或者中科大镜像

curl -sSL  'https://gerrit-googlesource.proxy.ustclug.org/git-repo/+/master/repo?format=TEXT' |base64 -d > ~/bin/repo

repo文件可能会报错,出现以下错误,

我们检测到您所在的子网和/或所使用的客户端存在大量下载某些较大二进制文件的行为,为保证用户的正常使用,我们阻断了此类请求。

We have detected enormous traffic from your network or client and have blocked your requests to ensure the quality of service for normal users.

Nous avons détecté un grand nombre de téléchargements de certains fichiers binaires volumineux à partir de votre sous-réseau et/ou du client que vous utilisez et avons bloqué ces requêtes pour garantir un accès normal aux utilisateurs.

お客様のサブネットおよび/またはクライアントで、特定の大きなバイナリのダウンロードを多数検出しました。お客様の通常の使用のためこれらのリクエストをブロックさせていただきます。

您可以尝试更改网络环境或更换客户端;您也可以联系 support@tuna.tsinghua.edu.cn 并附上下方的标识符。

解决:

这个可能是连接的公共不安全的网络,禁止下载大型二进制文件,我们主机换个wifi,再次执行repo应该就可以了

 执行sudo vim /etc/profile,报错找不到vim命令

dong@dong:~/桌面$ sudo vim /etc/profile
sudo: vim:找不到命令

解决方法:需要安装vim

sudo apt-get install vim

2.5 创建源码存放目录

 mkdir aosp  (根据实际情况来新建目录)
 cd aosp

报错,提示无法创建目录

mkdir: 无法创建目录"/home/ubuntu/aosp": 没有那个文件或目录

解决:

先进入root模式:

sudo -s

再进入home目录

cd /home

再创建 ubuntu目录

mkdir /home/ubuntu/

再创建 aosp目录

mkdir /home/ubuntu/aosp

2.6 查看要下载的分支

git clone https://aosp.tuna.tsinghua.edu.cn/platform/manifest
cd manifest/
git branch -a

用q退出预览

或者Google官网查看版本代号标记

https://source.android.com/docs/setup/about/build-numbers?hl=zh-cn#source-code-tags-and-builds

比如Android7.1.0的 对应的标记

NDE63Xandroid-7.1.0_r7NougatPixel XL、Pixel2016-11-05
NDE63Vandroid-7.1.0_r6NougatPixel XL、Pixel2016-11-05
NDE63Uandroid-7.1.0_r5NougatPixel XL、Pixel2016-11-05
NDE63Pandroid-7.1.0_r4NougatPixel XL、Pixel2016-10-05
NDE63Landroid-7.1.0_r2NougatPixel XL、Pixel2016-10-05
NDE63Handroid-7.1.0_r1NougatPixel XL、Pixel2016-10-05

2.7 初始化仓库

repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest

2.8 repo指定Android版本

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

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

repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-13.0.0_r35

报错:没有python文件夹

dong@dong:~/桌面/aosp$ repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-13.0.0_r35
/usr/bin/env: “python”: 没有那个文件或目录

解决:

查看python安装位置

whereis python3

显示如下:

python3: /usr/bin/python3.8 /usr/bin/python3 /usr/lib/python3.8 /usr/lib/python3.9 /usr/lib/python3 /etc/python3.8 /etc/python3 /usr/local/lib/python3.8 /usr/include/python3.8 /usr/share/python3 /usr/share/man/man1/python3.1.gz

sudo ln -s /usr/bin/python3 /usr/bin/python

报错,不能下载google的仓库,需要切换清华镜像

root@dong:/home/dong/桌面/aosp# repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-7.1.0_r1
Downloading Repo source from https://gerrit.googlesource.com/git-repo
fatal: Cannot get https://gerrit.googlesource.com/git-repo/clone.bundle
fatal: error [Errno 111] Connection refused
fatal: double check your --repo-rev setting.
fatal: cloning the git-repo repository failed, will remove '.repo/repo'

解决:

打开全局变量配置文件

sudo gedit ~/.bashrc

添加全局变量我,们在末尾添加这两行并保存:
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'

使配置文件生效

source ~/.bashrc

报错,找不到repo命令

root@dong:/home/dong/桌面/aosp# repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-7.1.0_r1

找不到命令“repo”,您的意思是:

  command 'reno' from deb python3-reno (2.11.2-2build1)
  command 'repl' from deb mailutils-mh (1:3.7-2.1)
  command 'repl' from deb mmh (0.4-2)
  command 'repl' from deb nmh (1.7.1-6)
  command 'rep' from deb rep (0.92.5-3build5)
  command 'repc' from deb qtchooser (66-2build1)

尝试 apt install <deb name>

解决:

卸载重装repo

sudo apt-get remove repo

 如果提示版本不对,就按命令安装repo

root@dong:/home/dong/aosp# repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-7.1.0_r1

Command 'repo' not found, but can be installed with:

snap install git-repo  # version 1.12.37-3, or
apt  install repo    

See 'snap info git-repo' for additional versions.

解决:

apt  install repo 

报错,repo设置失败

dong@dong:~/桌面/aosp$ repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-13.0.0_r35
Downloading Repo source from https://gerrit.googlesource.com/git-repo
fatal: Cannot get https://gerrit.googlesource.com/git-repo/clone.bundle
fatal: error [Errno 111] Connection refused
fatal: double check your --repo-rev setting.
fatal: cloning the git-repo repository failed, will remove '.repo/repo'

解决:

重新初始化repo,设置repo环境变量

export PATH=~/.bin:$PATH
export REPO_URL=https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/

报错,这是python版本链接不对

root@dong:/home/dong/aosp# repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-13.0.0_r35
  File "/root/bin/repo", line 51
    def print(self, *args, **kwargs):
            ^
SyntaxError: invalid syntax

解决:

现在是python2,需要链接到python3上面

sudo rm /usr/bin/python     

sudo ln -s /usr/bin/python3 /usr/bin/python

再次执行下载源码指令

repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-13.0.0_r35

成功实例:

dong@dong:~/桌面/aosp$ repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-13.0.0_r35
Downloading Repo source from https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/
remote: Enumerating objects: 4882, done.
remote: Counting objects: 100% (4882/4882), done.
remote: Compressing objects: 100% (2422/2422), done.
remote: Total 8618 (delta 4305), reused 2460 (delta 2460), pack-reused 3736
接收对象中: 100% (8618/8618), 4.18 MiB | 3.54 MiB/s, 完成.
处理 delta 中: 100% (5569/5569), 完成.

2.9 同步源码

repo sync  j8(j8是开启的线程数量,等待源代码下载完成,长时间等待,若失败请检查网络等情况)

开始下载源码,大概需要5到6个小时,占了100多G的磁盘空间

开始同步

dong@dong:~/桌面/aosp$ repo sync
remote: Enumerating objects: 5288, done.        
remote: Counting objects:   0% (1/5288)        
remote: Counting objects:   1% (53/5288)        
remote: Counting objects:   2% (106/5288)        
remote: Counting objects:   3% (159/5288)        
remote: Counting objects:   4% (212/5288)        
remote: Counting objects:   5% (265/5288)        
remote: Counting objects:   6% (318/5288)        
remote: Counting objects:   7% (371/5288)      
 

下载成功,4到5个钟头

处理 delta 中:  95% (32415/34113)
处理 delta 中:  96% (32750/34113)
处理 delta 中:  97% (33208/34113)
处理 delta 中:  98% (33455/34113)
处理 delta 中:  99% (34110/34113)
处理 delta 中: 100% (34113/34113)
处理 delta 中: 100% (34113/34113), 完成 413 个本地对象.
Fetching: 22% (252/1135) 1:10:04 | 4 jobs | 5:19 platform/external/icu @ exter..
Fetching: 100% (1135/1135), done in 4h53m27.407s

更新,又需要1个小时

Checking out: 95% (1083/1135) platform/prebuilts/vndk/v29正在更新文件:  73% (531Checking out: 97% (1103/1135) platform/system/keymaster正在更新文件:  73% (533/7正在更新文件: 100% (724/724), 完成.
正在更新文件: 100% (119/119), 完成.
正在更新文件: 100% (111/111), 完成.
Checking out: 97% (1108/1135) platform/prebuilts/gcc/linux-x86/host/x86_64-linuxChecking out: 97% (1109/1135) platform/prebuilts/gcc/linux-x86/host/x86_64-w64-mChecking out: 99% (1129/1135) platform/prebuilts/module_sdk/OnDevicePersonalizatChecking out: 100% (1135/1135), done in 1h23m34.934s
repo sync has finished successfully.

 

2.10 如果出现失败可以把repo脚本里面替换REPO_URL镜像源

REPO_URL = "https://gerrit.googlesource.com/git-repo"

换为

REPO_URL = 'https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'

三 linux vim 编辑 保存 退出

vim /etc/profile

3.2 编辑文件

按 i 进行编辑

3.3  保存与退出

首先按esc键返回命令编辑模式,刚才的Insert会消失

按英文状态的 :

:q! 不保存文件,强制退出

:w 保存文件,不退出vi命令

:wq 保存文件,退出vi命令

四 驱动下载

4.1 官网下载对应版本的驱动

https://developers.google.cn/android/drivers#redfintp1a.221105.002

4.2 找到piexl 5对应的驱动文件

4.3 解压到aosp根目录

tar -xvzf qcom-redfin-tp1a.221005.002-e7e20f49.tgz -C ~/GitProjects/aosp/
tar -xvzf google_devices-redfin-tp1a.221005.002-3daf7ea0.tgz  -C ~/GitProjects/aosp/

4.4 运行解压出来的脚本

./extract-qcom-redfin.sh
./extract-google_devices-redfin.sh

五 AOSP项目源码结构

  • abi Application Binary Interface 应用程序二进制接口,abi相信同学们在SO库调用上遇到过,如果不支持该平台的话就说不ABI不支持。
  • art Android Runtime 安卓运行时。这个会提前把字节码编译成二进制机器码保存起来,执行的时候加载速度比较快。Dalvik虚拟机则是在加载以后再去编译的,所以速度上ART会比Dalvik快一点。牺牲空间来赢取时间。
  • bionic 基础库,Android系统与Linux内核的桥梁。Bionic 音标为 bīˈänik,翻译为"仿生"。
  • bootable 系统启动引导相关程序
  • build 用于构建Android系统的工具,也就是用于编译Android系统的
  • cts Compatibility Test Suite 兼容性测试
  • dalvik dalvik虚拟机,用于解析执行dex文件的虚拟机
  • developers 开发者目录
  • developerment 开发目录,比如说应用,application就在里面了,apps
  • devices 设备相关的配置信息,什么索尼、HTC、自己的产品,就可以定义在这个目录下了
  • docs 文档
  • external 开源模组相关文件
  • frameworks 系统架构,Android的核心了
  • hardware hal层代码,硬件抽象层
  • libcore 核心库
  • libnativehelper native帮助库,实现JNI的相关文件
  • ndk native development kit
  • out 输出目录,编译以后生成的目录,相关的产出就在这里了
  • packages 应用程序包。一些系统的应用就在这里了,比如说蓝牙,Launcher,相机,拨号之类的。
  • pdk Plug-in Development Kit (PDK) is designed to help you build your own pattern projects
  • platform_testing 平台测试
  • prebuilts x86/arm架构下预编译的文件
  • sdk software development kit
  • system 底层系统文件
  • toolchain 工具链
  • tools 工具文件
  • Makefile mk文件,用于控制编译

六 系统编译

6.1 Android10以上需要安装以下工具包

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 

6.2 安装openjdk8

sudo apt-get install openjdk-8-jdk 

(安装不成功检查ubuntu是否换源、是否有更新软件包列表)

sudo apt-get update

jdk对应android版本

Android 7.0 (Nougat) - Android 10.0 (Q):Ubuntu - OpenJDK 8;
Android 5.x (Lollipop) - Android 6.0 (Marshmallow):Ubuntu - OpenJDK 7;
Android 2.3.x (Gingerbread) - Android 4.4.x (KitKat):Ubuntu - Java JDK 6;
Android 1.5 (Cupcake) - Android 2.2.x (Froyo):Ubuntu - Java JDK 5

如果安装 sudo apt-get install libesd0-dev 报 Unable to locate package libesd0-dev 这个错,解决办法 

sudo gedit /etc/apt/sources.list  //在行尾添加如下两行的内容
deb http://us.archive.ubuntu.com/ubuntu/ xenial main universe
deb-src http://us.archive.ubuntu.com/ubuntu/ xenial main universe

6.3 设置启用cache (加快重新编译源码时的速度。可选)

export USE_CCACHE=1  (在你home主目录的.bashrc中加入)
export CCACHE_DIR=/home/ubuntu/.ccache  (指定一个缓存目录,也可以不指定,默认目录为你当前用户目录下的.ccache)
aosp/prebuilts/misc/linux-x86/ccache/ccache -M 50G (这个命令在Android源码中,缓存大小按照自己的硬盘来适当调整)
source ~/.bashrc  (source命令使修改立即生效)

6.4 编译

cd aosp (进入源码目录)

source build/envsetup.sh (脚本初始化环境)

make clobber (用于清除缓存)

6.5 整体编译,选择编译目标,选定的版本按照实际情况来

lunch

会输入以下信息供选择

You're building on Linux

Lunch menu... pick a combo:
     1. aosp_arm-eng
     2. aosp_arm64-eng
     3. aosp_mips-eng
     4. aosp_mips64-eng
     5. aosp_x86-eng
     6. aosp_x86_64-eng
     7. aosp_car_arm-userdebug
     8. aosp_car_arm64-userdebug
     9. aosp_car_x86-userdebug
     10. aosp_car_x86_64-userdebug
     11. mini_emulator_arm64-userdebug
     12. m_e_arm-userdebug
     13. m_e_mips64-eng
     14. m_e_mips-userdebug
     15. mini_emulator_x86_64-userdebug
     16. mini_emulator_x86-userdebug
     17. uml-userdebug
     18. aosp_cf_x86_auto-userdebug
     19. aosp_cf_x86_phone-userdebug
     20. aosp_cf_x86_tablet-userdebug
     21. aosp_cf_x86_tablet_3g-userdebug
     22. aosp_cf_x86_tv-userdebug
     23. aosp_cf_x86_wear-userdebug
     24. aosp_cf_x86_64_auto-userdebug
     25. aosp_cf_x86_64_phone-userdebug
     26. aosp_cf_x86_64_tablet-userdebug
     27. aosp_cf_x86_64_tablet_3g-userdebug
     28. aosp_cf_x86_64_tv-userdebug
     29. aosp_cf_x86_64_wear-userdebug
     30. cf_x86_auto-userdebug
     31. cf_x86_phone-userdebug
     32. cf_x86_tablet-userdebug
     33. cf_x86_tablet_3g-userdebug
     34. cf_x86_tv-userdebug
     35. cf_x86_wear-userdebug
     36. cf_x86_64_auto-userdebug
     37. cf_x86_64_phone-userdebug
     38. cf_x86_64_tablet-userdebug
     39. cf_x86_64_tablet_3g-userdebug
     40. cf_x86_64_tv-userdebug
     41. cf_x86_64_wear-userdebug
     42. aosp_marlin-userdebug
     43. aosp_marlin_svelte-userdebug
     44. aosp_sailfish-userdebug
     45. aosp_walleye-userdebug
     46. aosp_walleye_test-userdebug
     47. aosp_taimen-userdebug
     48. hikey-userdebug
     49. hikey64_only-userdebug
     50. hikey960-userdebug

Which would you like? [aosp_arm-eng]
  • 输出有编号和版本,就是要你选择编译目标的格式,编译目标的格式组成为BUILD-BUILDTYPE,比如aosp_arm-eng的BUILD为aosp_arm,BUILDTYPE为eng。
  • 其中BUILD表示编译出的镜像可以运行在什么环境,aosp代表Android开源项目,arm表示系统是运行在arm架构的处理器上。
  • BUILDTYPE 指的是编译类型,有以下三种:
  • 1)user:用来正式发布到市场的版本,权限受限,如没有 root 权限,不能 dedug,adb默认处于停用状态。
    2)userdebug:在user版本的基础上开放了 root 权限和 debug 权限,adb默认处于启用状态。一般用于调试真机。
    3)eng:开发工程师的版本,拥有最大的权限(root等),具有额外调试工具的开发配置。一般用于模拟器。
  • 如果你没有Nexus /ˈneksəs/ 设备,只想编译完后运行在模拟器查看,那么BUILD可以选择aosp_x86,BUILDTYPE选择eng,Which would you like? [aosp_arm-eng]后面直接输入对应序号5就可以。

6.6 也可以直接指定编译的目标:

lunch aosp_x86-eng 

lunch 5

6.7 开始编译,16是线程数,和cpu有关,适当调整数字

 make -j16

6.8 如果提示make找不到,则需要安装make工具

oot@dong:/home/dong/桌面#  make clean

Command 'make' not found, but can be installed with:

apt install make        # version 4.2.1-1.2, or
apt install make-guile  # version 4.2.1-1.2

解决方法:按提示输入以下命令

apt install make  # version 4.2.1-1.2

6.9 如果编译报以下错误,则标识内存不足,保证16GB以上内存

98% 392/397] analyzing Android.bp files and generating ninja file at out/soong
FAILED: out/soong/build.ninja
cd “KaTeX parse error: Expected 'EOF', got '&' at position 49: …soong_build")" &̲& BUILDER="PWD/KaTeX parse error: Expected 'EOF', got '&' at position 50: …soong_build")" &̲& cd / && env -…BUILDER” --top “$TOP” --soong_out “out/soong” --out “out” -o out/soong/build.ninja --globListDir build --globFile out/soong/globs-build.ninja -t -l out/.module_paths/Android.bp.list --available_env out/soong/soong.environment.available --used_env out/soong/soong.environment.used.build Android.bp
Killed
00:31:16 soong bootstrap failed with: exit status 1

解决:

1. 回到根目录
cd
2. 查看交换区大小
free -m
3. 关闭原来的swap文件
sudo swapoff /swapfile
4. 删除原来的swpa文件
sudo rm /swapfile
5. 重新创建spap文件
sudo dd if=/dev/zero of=/swapfile bs=1G count=16
6. 赋予权限
sudo chmod 0600 /swapfile
7.创建文件系统
sudo mkswap -f /swapfile
8. 开启swapfile
sudo swapon /swapfile

6.10 可能报python语法错误

SyntaxError: invalid syntax
[  1% 643/35707] target Java: core-all...RARIES/core-all_intermediates/classes)
  File "build/tools/normalize_path.py", line 25
    print os.path.normpath(p)
           ^
SyntaxError: invalid syntax
[  1% 643/35707] host Java: jarjar (ou...IBRARIES/jarjar_intermediates/classes)
  File "build/tools/normalize_path.py", line 25
    print os.path.normpath(p)
           ^
SyntaxError: invalid syntax
[  1% 643/35707] host C++: ijar <= build/tools/ijar/classfile.cc
ninja: build stopped: subcommand failed.
build/core/ninja.mk:148: recipe for target 'ninja_wrapper' failed
make: *** [ninja_wrapper] Error 1

#### make failed to build some targets (04:52 (mm:ss)) ####

解决:优先使用python2

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150

6.11 可能报找不到java类

[  0% 3/83736] JarJar: out/target/common/obj/JAVA_LIBRARIES/libphonenumber-platform_intermediates/classes-jarjar.jar
FAILED: out/target/common/obj/JAVA_LIBRARIES/libphonenumber-platform_intermediates/classes-jarjar.jar
/bin/bash -c "java -jar out/host/linux-x86/framework/jarjar.jar process external/libphonenumber/jarjar-rules.txt out/target/common/obj/JAVA_LIBRARIES/libphonenumber-platform_intermediates/classes-full-debug.jar out/target/common/obj/JAVA_LIBRARIES/libphonenumber-platform_intermediates/classes-jarjar.jar"
错误: 找不到或无法加载主类 com.tonicsystems.jarjar.Main
ninja: build stopped: subcommand failed.
10:46:07 ninja failed with: exit status 1
build/core/main.mk:21: recipe for target 'run_soong_ui' failed
make: *** [run_soong_ui] Error 1

#### make failed to build some targets (41 seconds) ####

解决:

编译之前执行 以下指令

export LC_ALL=C

6.13 可能报SSL错误

Jack server failed to (re)start, try 'jack-diagnose' or see Jack server log
SSL error when connecting to the Jack server. Try 'jack-diagnose'
SSL error when connecting to the Jack server. Try 'jack-diagnose'
[  0% 292/79741] target  C++: libv8src <= external/v8/src/wasm/wasm-module.cc

解决:

原因就是编译时用的是open-jdk 8u292,默认禁用了TLSv1, TLSv1.1,进入java.security中删除TLSv1, TLSv1.1
cd /etc/java-8-openjdk/security/java.security
修改后执行

cd /prebuilts/sdk/tools/ 

./jack-admin kill-server

./jack-admin start-server

6.11 编译成功会显示下面内容:

Creating filesystem with parameters:
    Size: 2147483648
    Block size: 4096
    Blocks per group: 32768
    Inodes per group: 8192
    Inode size: 256
    Journal blocks: 8192
    Label: system
    Blocks: 524288
    Block groups: 16
    Reserved block group size: 127
Created filesystem with 2216/131072 inodes and 199826/524288 blocks
[100% 7669/7669] Install system fs ima.../target/product/generic_x86/system.img
out/target/product/generic_x86/system.img+ maxsize=2192446080 blocksize=2112 total=2147483648 reserve=22146432

#### make completed successfully (01:24:41 (hh:mm:ss)) ####

会在源码跟目录out/target/product/angler目录下生成镜像文件:

  • system.img:系统镜像
  • ramdisk.img:根文件系统镜像
  • userdata.img:用户数据镜像
  • recovery.img:recovery镜像
  • boot.img:启动镜像
  • vendor.img:驱动镜像

 6.11 其它指令说明 

- 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.
- clean - m clean 会删除此配置的所有输出和中间文件。此内容与 rm -rf out/ 相同
其中mmm指令就是用来编译指定目录.通常来说,每个目录只包含一个模块.比如这里我们要编译Setting 模块,执行指令:
mmm packages/apps/Settings/

6.12 常见make指令

//编译整个工程,整体编译时间较长

make

//对单个模块进行编译,对其所依赖的模块也进行编译,整体编译时间较长
make module

//先进入子目录,对其目录对应的模块进行编译,编译时间短
mm

//编译制定目录下的模块,不编译其所依赖的其他模块,第一次一般都会报错,编译时间短  
mmm

6.13 make生成镜像文件

//生成boot.img文件

make bootimage    

//重新打包生成system.img文件

make snod          

//生成userdata.img文件
make userdataimage

//编译出对应版本的adb命令文件
make adb          

 //编译出对应版本的fastboot刷机工具 
make fastboot    

七 Ubuntu上删除AOSP源码

7.1 打开终端,进入AOSP源码的目录。如果你不知道源码的具体位置,可以使用以下命令来查找:这将会在整个系统中搜索build/envsetup.sh文件,该文件存在于AOSP源码目录中。

find / -name "build/envsetup.sh"

7.2  执行以下命令来进入AOSP源码环境:

source build/envsetup.sh

7.3 进入AOSP源码目录:

cd  aosp

7.4 执行以下命令来清除源码:会清除所有生成的文件和目录。

 make clean

7..5 删除AOSP源码目录: 会永久删除源码

rm -rf <AOSP源码目录>

八 需要安装的工具集合介绍

sudo apt-get install openjdk-8-jdk //jdk 这个不用说了吧
sudo apt-get install python //Repo 是基于 Python 2.x 中的特定功能构建的,与 Python 3 不兼容
sudo apt-get install phablet-tools //git工具包
sudo apt-get install curl //上传和下载数据的工具
sudo apt-get install build-essential //提供编译程序必须软件包的列表信息
sudo apt-get install make //源码编译工具
sudo apt-get install gcc //GNU编译器套件
sudo apt-get install g++
sudo apt-get install libc6-dev //共享库
sudo apt-get install patch //补丁工具
sudo apt-get install texinfo //文档系统
sudo apt-get install libncurses-dev //系统的必备库
sudo apt-get install git-core gnupg //git的工具包,虽然有git了,但是有备无患
sudo apt-get install ncurses-dev //编译内核的时候需要
sudo apt-get install valgrind //内存检查器

九 真机安装源码镜像

9.1 前提需要有真机,其它厂商都加有锁,并且没有开放的驱动,是刷不了源码镜像的。所以只能用google真机,如下机型:

nexus Pixel5 

nexus Pixel6 

nexus Pixel6p 

nexus Pixel7 

nexus Pixel8

在Google手机上刷机时需要下载Google硬件驱动,找到对应的机型,点击Link即可下载
下载网址:https://developers.google.cn/android/blobs-preview

9.2 提取驱动,将下载的驱动压缩包拷贝的Android源码根目录,并进行解压,解压后得到一个.sh的脚本

9.3 解压驱动

tar -zxvf google_devices-oriole-sp2a.220305.013.a3-04c512f4.tgz

9.4 执行extract-google_devices-oriole.sh脚本提取驱动,运行提示"Press Enter to view the license"时输入回车后即可,后面输入d来进行翻页

./extract-google_devices-oriole.sh 

9.5 直到提示输入"I ACCEPT"时,按提示输入后回车即可完成驱动的提取,完成后源码根目录多了一个vendor的目录 

9.6 设置代码编译环境,每次重启shell时都需要配置编译环境,在根目录执行如下命令:  

source build/envsetup.sh 

9.8 使用lunch启动

lunch aosp_oriole-userdebug 

不同的设备选择的目标不同,根据Android官网给出的设备目录选项来选择
网址:https://source.android.google.cn/setup/build/running#selecting-device-build 

9.9 开始编译,输入m或make即可编译整个Android源码,也可使用参数-j指定线程数

make -j8

如果只编译某一部分,则先切换的对应的目录下,使用mm命令进行编译

mm

9.10 window系统下载adb和fastboot工具,解压后将目录添加到环境变量中 

下载链接:https://developer.android.google.cn/studio/releases/platform-tools#downloads

9.11 手机上启动USB调试 

如要在通过 USB 连接的设备上使用 adb,则必须在设备的系统设置中启用 USB 调试

9.12 安装驱动,在设备管理器中找到对应的设备安装驱动,安装好后则会多出一个"Android Device"设备 

下载链接:https://developer.android.google.cn/studio/run/win-usb

9.13 解锁加载引导程序

进入fastboot模式,查看是否处于锁定模式

adb reboot bootloader

如果处于锁定模式,先进行解锁,同时需要在手机上进行确认

fastboot flashing unlock

处于解锁模式后,设备每次启动都会先进入fastboot模式,如需处于锁定模式,则输入

fastboot flashing lock

9.14 刷写设备

进入fastboot模式

adb reboot bootloade

刷机

完整刷机,google_img.zip为完整刷机包

fastboot update -w google_img.zip

或进入到out目录,直接执行

fastboot flashall -w

9.15 也可指定刷机部分

fastboot flash aboot emmc_appsboot.mbn      //烧写bootloader分区
fastboot flash boot boot.img                            //烧写kernel分区
fastboot flash system system.img                   //烧写system分区
fastboot flash userdata userdata.img              //烧写userdata分区
fastboot flash persist persist.img                    //烧写persist分区
fastboot flash cache cache.img                      //烧写cache分区
fastboot reboot                                               //重启 

9.16 刷机成功后会输出以下信息

fastboot flashall
--------------------------------------------
Checking 'product'                                 OKAY [  0.069s]
Setting current slot to 'b'                        OKAY [  0.087s]
Sending 'boot_b' (98304 KB)                        OKAY [  2.720s]
Writing 'boot_b'                                   OKAY [  0.308s]
Sending 'dtbo_b' (16384 KB)                        OKAY [  0.540s]
Writing 'dtbo_b'                                   OKAY [  0.112s]
Sending 'vbmeta_b' (8 KB)                          OKAY [  0.140s]
Writing 'vbmeta_b'                                 OKAY [  0.079s]
Sending 'vbmeta_system_b' (4 KB)                   OKAY [  0.140s]
Writing 'vbmeta_system_b'                          OKAY [  0.079s]
Sending 'vendor_boot_b' (98304 KB)                 OKAY [  2.680s]
Writing 'vendor_boot_b'                            OKAY [  0.308s]
Rebooting into fastboot                            OKAY [  0.070s]
< waiting for any device >
Sending 'super' (4 KB)                             OKAY [  0.001s]
Updating super partition                           OKAY [  0.031s]
Resizing 'product_b'                               OKAY [  0.006s]
Resizing 'system_b'                                OKAY [  0.006s]
Resizing 'system_ext_b'                            OKAY [  0.006s]
Resizing 'system_a'                                OKAY [  0.006s]
Resizing 'vendor_b'                                OKAY [  0.006s]
Resizing 'vendor_a'                                OKAY [  0.006s]
Invalid sparse file format at header magic
Resizing 'product_b'                               OKAY [  0.006s]
Sending sparse 'product_b' 1/2 (262140 KB)         OKAY [  6.954s]
Writing 'product_b'                                OKAY [  0.710s]
Sending sparse 'product_b' 2/2 (20168 KB)          OKAY [  0.547s]
Writing 'product_b'                                OKAY [  0.090s]
Invalid sparse file format at header magic
Resizing 'system_b'                                OKAY [  0.006s]
Sending sparse 'system_b' 1/4 (262112 KB)          OKAY [  6.951s]
Writing 'system_b'                                 OKAY [  0.697s]
Sending sparse 'system_b' 2/4 (262116 KB)          OKAY [  6.912s]
Writing 'system_b'                                 OKAY [  0.701s]
Sending sparse 'system_b' 3/4 (262140 KB)          OKAY [  6.935s]
Writing 'system_b'                                 OKAY [  0.679s]
Sending sparse 'system_b' 4/4 (64236 KB)           OKAY [  1.697s]
Writing 'system_b'                                 OKAY [  0.206s]
Resizing 'system_ext_b'                            OKAY [  0.006s]
Sending 'system_ext_b' (191812 KB)                 OKAY [  4.977s]
Writing 'system_ext_b'                             OKAY [  0.526s]
Resizing 'system_a'                                OKAY [  0.005s]
Sending 'system_a' (26672 KB)                      OKAY [  0.699s]
Writing 'system_a'                                 OKAY [  0.091s]
Invalid sparse file format at header magic
Resizing 'vendor_b'                                OKAY [  0.010s]
Sending sparse 'vendor_b' 1/3 (262092 KB)          OKAY [  6.941s]
Writing 'vendor_b'                                 OKAY [  0.731s]
Sending sparse 'vendor_b' 2/3 (262116 KB)          OKAY [  6.881s]
Writing 'vendor_b'                                 OKAY [  0.692s]
Sending sparse 'vendor_b' 3/3 (239980 KB)          OKAY [  6.379s]
Writing 'vendor_b'                                 OKAY [  0.627s]
Rebooting                                          OKAY [  0.000s]
Finished. Total time: 88.360s

十 模拟器安装源码镜像,和单独编译模块

10.1 模拟器安装对比真机会比较简单,编译完运行模拟器就可以了

source build/envsetup.sh

lunch aosp_x86-eng

emulator

10.2  由于启动模拟器也需要内存,可能报内存不足错误,里面总共3个错误

emulator: WARNING: system partition size adjusted to match image file (1536 MB > 200 MB)

emulator: WARNING: data partition size adjusted to match image file (550 MB > 200 MB)

emulator: WARNING: Increasing RAM size to 1GB
emulator: ERROR: x86 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).

解决:

第一个

emulator: WARNING: Increasing RAM size to 1GB

先解决内存大小问题,输入

emulator -partition-size 1024

第二个

emulator: ERROR: x86 emulation currently requires hardware acceleration!

需要开启硬件加速

启动android sdk中的 SDK Manager.exe

第三个

Please ensure KVM is properly installed and usable.

提示你去安装KVM

sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils
sudo adduser `id -un` libvirtd
sudo adduser `id -un` kvm

Check success or not
sudo kvm-ok

10.3 启动虚拟机可能出现的错误

VM虚拟机启动可能需要cpu虚拟化支持,我们需要编辑虚拟机,开启cpu虚拟化

开启后可能会打不开虚拟机,说不支持的平台,这个时候如果在确定cpu开启了虚拟化,同时关闭了内核隔离的情况下还出现,则可以尝试关闭系统程序的所有虚拟化选项。

 

10.4 运行成功则会启动模拟器,启动编译后的系统,如下

10.5 如果需要单独编译某个模块,可以先进去模块下面,比如我们要编译系统的Settings应用模块:

source build/envsetup.sh
lunch aosp_x86-eng

cd packages/apps/Settings

 这时不再是make指令,而是mm指令来编译当前目录下的模块,不编译依赖模块。

mm 

除了Settings.odex文件,还会在out/target/product/generic_x86/system/priv-app/Settings目录下生成Settings.apk。

10.6 还有以下命令可以进行单编:

  • mmm:编译指定目录下的模块,不编译它所依赖的其它模块。
  • mma:编译当前目录下的模块及其依赖项。
  • mmma:编译指定路径下所有模块,并且包含依赖。

10.7 如果你修改了源码,想查看生成的APK文件,有两种方式:

  1. 通过adb push或者adb install 来安装APK。
  2. 使用make snod命令,重新生成 system.img,运行模拟器查看。

十一 同步编译源码完成过程

安装pthon

apt-get install python
sudo passwd root

查找资源占用
ps -e|grep apt
杀死进程
sudo kill [进程id号]
然后进行强制解锁
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock
再次安装python
apt-get install python
python --version

安装python3
sudo apt-get remove python3
sudo apt-get remove --auto-remove python3
sudo apt-get purge --auto-remove python3
sudo apt-get install ubuntu-desktop

配置python优先级
sudo update-alternatives --config python
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150

安装配置git
sudo apt-get install git
git config --global user.name dong
git config --global user.email 809459535@qq.com

安装curl下载源码工具
sudo apt-get install curl

初始化repo
dong@dong:~$ mkdir ~/bin
dong@dong:~$ PATH=~/bin:$PATH
dong@dong:~$ curl -sSL  'https://gerrit-googlesource.proxy.ustclug.org/git-repo/+/master/repo?format=TEXT' |base64 -d > ~/bin/repo
dong@dong:~$ chmod a+x ~/bin/repo
dong@dong:~$ mkdir aosp
dong@dong:~$ cd aosp
dong@dong:~/aosp8$ export REPO_URL='https://gerrit-googlesource.proxy.ustclug.org/git-repo'
dong@dong:~/aosp8$ repo init -u git://mirrors.ustc.edu.cn/aosp/platform/manifest -b android-7.1.0_r7

同步源码
repo syncs
repo sync -c --no-tags
repo sync -j4 -q -c --no-tags

交换内存大小
回到根目录
cd ..
查看交换区大小
free -m
关闭原来的swap文件
sudo swapoff /swapfile
删除原来的swpa文件
sudo rm /swapfile
重新创建spap文件
sudo dd if=/dev/zero of=/swapfile bs=1G count=16
赋予权限
sudo chmod 0600 /swapfile
创建文件系统
sudo mkswap -f /swapfile
开启swapfile
sudo swapon /swapfile

安装编译工具
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 
sudo apt-get install openjdk-8-jdk 

启动编译
export LC_ALL=C
source build/envsetup.sh (脚本初始化环境)
make clobber (用于清除缓存)
lunch
lunch aosp_x86-eng
make -j16

启动模拟器
emulator

可能用到的压缩指令
tar --preserve -cvf archive.tar file_or_directory
tar -xvf archive.tar

sudo apt-get update
sudo apt-get install zip

zip -r -y aosp7.0.zip aosp
unzip example.zip -d /home/user/
zip -r archive.zip /home/user/documents /home/user/pictures

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值