preface

Recently, I wanted to compile a module of Android to do tools. At first, I couldn’t get it right according to the rubbish tutorials on the Internet. The articles I found were all copied to and from. In the end, I chose to follow the operation of official documents instead of those outdated articles. Although there are still holes in the middle, they can be solved.

This blog is all about downloading the Android source code on the Mac.

Reference links: source.android.com/setup/build… In addition, I have tried the image of Tsinghua University, but the speed is still not satisfactory. It is recommended to enable VPN in advance


Environment to prepare

I’ll break it down into several steps so you can check each step:

  • Install Git
  • Install python
  • Install the repo
  • Initialize the repo
  • Download the source code

Install python

  • First make sure your computer has a Python environment, andpython -VThe command can be run successfully.
  • If you don’t have python, go to the official website and download it:
    • 3.7.2 example here, into the official download address: www.python.org/downloads/r…
    • Download the PKG file and install it directly

I used python3.7.2 for corporate reasons, but you can choose another version if you want


  • After python3 is installed, you will executepython -VThe command is invalid, andpython3 -VThe system runs normally 👇 :
  • But only the ones that python3 runs properly don’t qualify, because it only recognizes the tools that will be used later to download the source codepythonThis command.
  • To solve this problem, we simply create a new soft linkpythonAnd let it point topython3👇🏻 :
  • To perform firstwhich python3Find the command environment directory
  • Then performsudo ln -s /usr/local/bin/python3 /usr/local/bin/pythonCreate a soft link in the same directory
  • python -VThe successful running

Now that the Python environment is ready, we need to install the official repo tool.


Install the repo

  • Download the repo and drop it directly into the same directory as your Python environment
  • Download command:curl https://storage.googleapis.com/git-repo-downloads/repo > /usr/local/bin/repo
  • Refer to the following steps (you don’t need to download them if you’ve already downloaded them) 👇 :


Initialize the repo

Create a new directory for the source code in the Android Studio project directory and go to:

By default, your Git configuration is complete before you execute the following commands in the source directory created above

Execute commands (make mistakes) : repo init -u https://android.googlesource.com/platform/manifest

After executing the above command, you will find an error:

To fix this, refer to StackOverFlow and add the following code to the repo file you just downloaded:

import ssl
ssl._create_default_https_context = ssl._create_unverified_context
Copy the code



Run it again in the source directory you prepared earlierrepo init -u https://android.googlesource.com/platform/manifest, wait patiently to download:

  • If there is no error, it is OK

Now we can download the source code.



Download the source code

Execute directly in the initialized directoryrepo sync -c -j8Ok, and then a long download:

Wait for the download, no error, you are done, and then use the IDE to open. If something happens during the download and you need to start over, delete the.repo file from the current directory and start over from the initialization step.

If it stays at 0% or n% for a long time, please check if your network is being consumed. My speed is generally maintained at 3-4 MB /s