Android代码汇总推送gitlab指引

一、下载Android源码

Android项目原始使用repo工具进行管理,将多个git项目组合成一个完整的大项目。 因此我们使用repo去拉取Android的源码,建议的操作如下:

1.1 下载repo工具

注意:使用linux发行版包管理安装的repo可能存在版本过老的问题,因此建议直接下载google官方最新的repo工具并放置到PATH中。
二进制程序下载链接
<https://storage.googleapis.com/git-repo-downloads/repo>

建议的命令:
wget <https://storage.googleapis.com/git-repo-downloads/repo> -O /usr/bin/repo
chmod +x /usr/bin/repo

1.2 进行git路径替换

Android的源码托管在多个代码平台,存在被墙、国内下载速度不佳等问题,因此建议对于部分的url进行替换。替换的方式有多种,下边是建议的替换方式,已经包含了多个常用的大仓库:

cat >> ~/.gitconfig <<EOF
[url "https://mirrors.tuna.tsinghua.edu.cn/git/git-repo"]
	insteadof = https://gerrit.googlesource.com/git-repo
[url "https://mirrors.tuna.tsinghua.edu.cn/git/lineageOS/"]
	insteadof = https://review.lineageos.org/
[url "https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/"]
	insteadof = https://android.googlesource.com/
[url "https://mirrors.tuna.tsinghua.edu.cn/git/lineageOS/LineageOS/"]
	insteadof = https://github.com/LineageOS/
EOF

1.3 repo初始化及同步

创建一个文件夹,用于初始化repo
mkdir -pv aosp/android-11.0.0_r46
cd aosp/android-11.0.0_r46

repo init -u <https://android.googlesource.com/platform/manifest> --git-lfs --depth=1 -b android-11.0.0_r46

会在当前目录建立.repo目录,该目录中存储了整个项目的依赖信息,如熟悉项目组成,可以对于同步的项目开展裁剪。

确认无误,可以开始同步:
repo sync -c
git lfs pull (可选)

二、开展裁剪及清理

2.1 主要针对repo缓存,.git子目录,.gitattributes等文件

删除repo缓存
rm -rf .repo

删除子目录中的.git
find . -mindepth 2 -type d -name ".git" -exec rm -rf {} \\;

删除子目录中的.gitattributes
find . -mindepth 2 -type f -name ".gitattributes" -exec rm -rf {} \\;

验证删除的结果
find . -type f -name ".gitattributes" 
find . -mindepth 2 -type d -name ".git" 

2.2 进行LFS配置的检查(可选)

常用命令
git lfs install #安装lfs
git lfs ls-files #列出目前正在追踪中的lfs文件
git lfs untrack <file_path> #取消lfs文件的追踪,彻底取消需要在.gitattributes配置

取消文件跟踪:git rm --cached ${file}
git rm --cached android-11.0.0_r46/external/OpenCL-CTS/test_conformance/clcpp/spirv10_2016.04.27.7z
取消目录跟踪:git rm --cached -r ${dir}
git rm --cached -r android-11.0.0_r46

配置 LFS 跟踪规则,样例
在.git同级目录新建.gitattributes文件
样例规则:
*.7z filter=lfs diff=lfs merge=lfs -text
*.so filter=lfs diff=lfs merge=lfs -text
example/**/* filter=lfs diff=lfs merge=lfs -text

2.3 取消LFS文件的追踪(可选)

整个Android项目的大文件不是很多,绝对值也不大(500M左右),为了后期管理的方便,不建议在项目管理的过程中,开启LFS。
在进行初次git add . 后,建议使用git lfs ls-files,查看一下,目前文件追踪的情况。
假如存在文件追踪,建议使用
git lfs untrack <file_path> 
对于文件取消追踪,文件损坏的情况下,可以:
git rm --cached <file_path> 
确定git lfs ls-files的值为空后,再进行git commit操作。

2.4 新增.gitignore

防止推送本地的编译成果,建议将默认的编译产出文件夹进行提交忽略。
在.gitignore文件中添加
android-11.0.0_r46/out/
android-11.0.0_r46/.repo/

三、推送代码

进行推送git

建议使用分支进行不同Android版本的管理
例如,android-11.0.0_r46

git checkout -b android-11.0.0_r46
git add .
git commit -m "init android-11.0.0_r46"
git push origin android-11.0.0_r46

当单个项目过大时,https可能会出现一些错误,此时可以切换至ssh。

四、FAQ

报错案例
remote: GitLab: LFS objects are missing. Ensure LFS is properly set up or try a manual "git lfs push --all".
! [remote rejected] android-11.0.0_r46 -> android-11.0.0_r46 (pre-receive hook declined)
error: failed to push some refs to 'ssh://10.19.154.203:11222/yzd/ysj/AOSP-YZD/aosp-origin.git'
报错案例
LFS upload failed:cts:   0% (0/1), 0 B | 0 B/s                                                
  (missing) android-11.0.0_r46/external/OpenCL-CTS/test_conformance/clcpp/spirv10_2016.04.27.7z (fe4f34d616ed7ef70e870c22078f60655f68b0c5191c8d8b9d045dd0e7390bc2)
Uploading LFS objects:   0% (0/1), 0 B | 0 B/s, done.
hint: Your push was rejected due to missing or corrupt local objects.
hint: You can disable this check with: 'git config lfs.allowincompletepush true'

建议:通过repo同步下来的个别文件,可能存在损坏。这种情况下,建议先将个别文件从git提交中去除,再分析具体文件的情况git rm --cached <file_path> 

报错案例
Enumerating objects : 780637,done.counting objects: 100% (780637/780637), done.Delta compression using up to 4 threadsCompressing objects: 100% (483517/483517), done.Nriting objects: 100% (780636/780636),18.06 GiB7.91 MiB/s,done.otal 780636 (delta 224926), reused 748111 (delta 194884), pack-reused 0error: RPC failed; HTTP 504 curl 22 The requested URL returned error: 504send-pack: unexpected disconnect while reading sideband packetfatal: the remote end hung up unexpectedly
Everything up-to-date

建议:从https切换到ssh

报错案例
root@galt-ubuntu:/tmp/OpenCL-CTS/test_conformance/clcpp# git lfs pull
batch response: Client error: <https://android.googlesource.com/platform/external/OpenCL-CTS.git/info/lfs/objects/batch> from HTTP 405
error: failed to fetch some objects from '<https://android.googlesource.com/platform/external/OpenCL-CTS.git/info/lfs>'

建议:部分仓库的LFS的文件错误,建议同步下来之后,将其排除追踪。