打造自己的Android源码学习环境之四:下载Android源代码[通俗易懂]

发布时间:2025-12-09 13:57:49 浏览次数:4

打造自己的Android源码学习环境之四:下载Android源代码

下载android代码的全部指导来自:http://source.android.com/source/index.html。
如果你访问不了这个网址,可以看看这个链接。

注意,需要同时在主机系统(Host OS)和虚拟系统(Guest OS)中配置hosts。

0. 安装所需软件

0.1 安装JDK

参考: http://source.android.com/source/initializing.html

$ sudo apt-get update$ sudo apt-get install openjdk-8-jdk

0.2 安装必要的软件包

参考: http://source.android.com/source/initializing.html

$ 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

0.3 安装Repo

参考: http://source.android.com/source/downloading.html

$ mkdir ~/bin$ vim .bashrc

在.bashrc中添加PATH=~/bin:$PATH
然后使设置生效:

$ . ~/.bashrc

下载repo:

$ curl https://storage.proxy.ustclug.org/git-repo-downloads/repo > ~/bin/repo$ chmod a+x ~/bin/repo

1. 下载Android源代码

1.1 创建目录,存放代码用

$ mkdir android$ cd android/

1.2 配置git

$ git config --global user.name "galian"$ git config --global user.email "xxx@yyy.com"$ git config -luser.name=galianuser.email=xxx@yyy.com

1.3 查询Branch

参考: http://source.android.com/source/build-numbers.html#source-code-tags-and-builds

准备下载android-7.1.1_r13分支对应的代码。

1.4 repo init

repo init -u https://android.googlesource.com/platform/manifest -b android-7.1.1_r13

一些log信息,如下:

$ repo init -u https://android.googlesource.com/platform/manifest -b android-7.1.1_r13Get https://gerrit.googlesource.com/git-repo/clone.bundleGet https://gerrit.googlesource.com/git-repoGet https://android.googlesource.com/platform/manifest% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (22) The requested URL returned error: 404 Not FoundServer does not provide clone.bundle; ignoring.remote: Counting objects: 410, done        remote: Total 4566 (delta 1251), reused 4566 (delta 1251)        接收对象中: 100% (4566/4566), 3.00 MiB | 2.60 MiB/s, 完成.处理 delta 中: 100% (1251/1251), 完成.来自 https://android.googlesource.com/platform/manifest* [新分支]          adt_23.0.3 -> origin/adt_23.0.3* [新分支]          afw-test-harness-1.5 -> origin/afw-test-harness-1.5* [新分支]          afw-test-harness-2.1 -> origin/afw-test-harness-2.1* [新分支]          afw-test-harness-marshmallow-dev -> origin/afw-test-harness-marshmallow-dev* [新分支]          afw-test-harness-nougat-dev -> origin/afw-test-harness-nougat-dev* [新分支]          android-1.6_r1 -> origin/android-1.6_r1* [新分支]          android-1.6_r1.1 -> origin/android-1.6_r1.1* [新分支]          android-1.6_r1.2 -> origin/android-1.6_r1.2(省略)* [新分支]          android-7.0.0_r24 -> origin/android-7.0.0_r24* [新分支]          android-7.0.0_r27 -> origin/android-7.0.0_r27* [新分支]          android-7.0.0_r3 -> origin/android-7.0.0_r3* [新分支]          android-7.0.0_r4 -> origin/android-7.0.0_r4* [新分支]          android-7.0.0_r5 -> origin/android-7.0.0_r5* [新分支]          android-7.0.0_r6 -> origin/android-7.0.0_r6* [新分支]          android-7.0.0_r7 -> origin/android-7.0.0_r7* [新分支]          android-7.1.0_r1 -> origin/android-7.1.0_r1* [新分支]          android-7.1.0_r2 -> origin/android-7.1.0_r2* [新分支]          android-7.1.0_r3 -> origin/android-7.1.0_r3* [新分支]          android-7.1.0_r4 -> origin/android-7.1.0_r4* [新分支]          android-7.1.0_r5 -> origin/android-7.1.0_r5* [新分支]          android-7.1.0_r6 -> origin/android-7.1.0_r6* [新分支]          android-7.1.0_r7 -> origin/android-7.1.0_r7* [新分支]          android-7.1.1_r1 -> origin/android-7.1.1_r1* [新分支]          android-7.1.1_r10 -> origin/android-7.1.1_r10* [新分支]          android-7.1.1_r11 -> origin/android-7.1.1_r11* [新分支]          android-7.1.1_r12 -> origin/android-7.1.1_r12* [新分支]          android-7.1.1_r13 -> origin/android-7.1.1_r13(省略)Your identity is: galian <xxx@yyy.com>If you want to change this, please re-run 'repo init' with --config-nameTesting colorized output (for 'repo diff', 'repo status'):black    red      green    yellow   blue     magenta   cyan     white bold     dim      ul       reverse Enable color display in this user account (y/N)? Yrepo has been initialized in /home/galian/android

1.5 开始下载代码

$ repo sync

大概需要5~6个小时可以下载完成。

注意:为了确保全下载下来,再多执行一次repo sync

android 7.0.1r13的代码大概54GB,其中.repo目录34GB。除去.repo目录,代码大概20GB左右。

2. 可能出现的问题

2.1 Network is unreachable

$ repo init -u https://android.googlesource.com/platform/manifest -b android-7.1.1_r13 gpg: keyring `/home/galian/.repoconfig/gnupg/secring.gpg' created gpg: keyring `/home/galian/.repoconfig/gnupg/pubring.gpg' created gpg: /home/galian/.repoconfig/gnupg/trustdb.gpg: trustdb createdgpg: key 920F5C65: public key "Repo Maintainer <repo@android.kernel.org>" importedgpg: key 692B382C: public key "Conley Owens <cco3@android.com>" importedgpg: Total number processed: 2gpg:               imported: 2  (RSA: 1)fatal: Cannot get https://gerrit.googlesource.com/git-repo/clone.bundle fatal: error [Errno 101] Network is unreachable

解决方法:
参考这个链接

$ sudo vim /etc/hosts

修改hosts文件后,执行

sudo apt-get install nscdsudo /etc/init.d/nscd restart

2.2 unable to auto-detect email address

*** Please tell me who you are.Rungit config --global user.email "you@example.com"git config --global user.name "Your Name"to set your account's default identity.Omit --global to set the identity only in this repository.fatal: unable to auto-detect email address (got 'galian@galian-VirtualBox.(none)')

解决方法:

$ git config --global user.name "your_name"$ git config --global user.email "xxx@yyy.com"

替换your_namexxx@yyy.com为你自己的信息。

3. 参考

(1) http://source.android.com/source/index.html
(2) http://source.android.com/source/initializing.html
(3) http://source.android.com/source/downloading.html
(4) http://source.android.com/source/build-numbers.html#source-code-tags-and-builds

———————– 分 割 线 ———————–
本系列文章如下:

  • (1)《打造自己的Android源码学习环境之一:序》
  • (2)《打造自己的Android源码学习环境之二:在虚拟机中安装Ubuntu(上)》
  • (3)《打造自己的Android源码学习环境之三:在虚拟机中安装Ubuntu(下)》
  • (4)《打造自己的Android源码学习环境之四:下载Android源码》
  • (5)《打造自己的Android源码学习环境之五:编译Android源码》
  • (6)《打造自己的Android源码学习环境之六:运行Android模拟器》
需要做网站?需要网络推广?欢迎咨询客户经理 13272073477