Get the Android source code under Ubuntu and compile it

Posted by comtek on Wed, 02 Feb 2022 04:11:44 +0100

catalogue

background

Get source code

Install repo

Download source zip

synchronization

compile

Install lunch

Setting environment

compile

Use AS to view the source code

Compile idegen section

Optimize read options

View source code

epilogue

background

It is recommended to obtain the Android source code under Linux or Mac, and the hard disk space should be at least 250G, because the source code compression package has 119G, and the network should be smooth

Get source code

Install repo

root@rtlab-computer:/home/rtlab/szc# apt install repo

Check the repo version

root@rtlab-computer:/home/rtlab/szc# repo
warning: Python 3 support is currently experimental. YMMV.
Please use Python 2.6 - 2.7 instead.


... A new version of repo (2.15) is available.
... You should upgrade soon:
    cp /home/rtlab/szc/AndroidSource/aosp/.repo/repo/repo /usr/bin/repo


usage: repo COMMAND [ARGS]
The most commonly used repo commands are:
  abandon        Permanently abandon a development branch
  branch         View current topic branches
  branches       View current topic branches
  checkout       Checkout a branch for development
  cherry-pick    Cherry-pick a change.
  diff           Show changes between commit and working tree
  diffmanifests  Manifest diff utility
  download       Download and checkout a change
  gitc-delete    Delete a GITC Client.
  gitc-init      Initialize a GITC Client.
  grep           Print lines matching a pattern
  info           Get info on the manifest branch, current branch or unmerged branches
  init           Initialize a repo client checkout in the current directory
  list           List projects and their associated directories
  overview       Display overview of unmerged project branches
  prune          Prune (delete) already merged topics
  rebase         Rebase local branches on upstream branch
  smartsync      Update working tree to the latest known good revision
  stage          Stage file(s) for commit
  start          Start a new branch for development
  status         Show the working tree status
  sync           Update working tree to the latest revision
  upload         Upload changes for code review
See 'repo help <command>' for more information on a specific command.
See 'repo help --all' for a complete list of recognized commands.
Bug reports: https://bugs.chromium.org/p/gerrit/issues/entry?template=Repo+tool+issue
root@rtlab-computer:/home/rtlab/szc#

At present, the python version I use is 3. The above prompt says that it is best to use Python 2 6-2.7, because repo's support for Python 3 is only experimental at present, but Python 2.0 is used 7. Synchronizing the latest aosp source code will report an error, but Python 3 will not. Therefore, python 3 is recommended.

Create a new working directory under which our future operations on AOSP will be carried out

root@rtlab-computer:/home/rtlab/szc# mkdir AndroidSource && cd AndroidSource

Download source zip

Here we recommend the image of China University of science and technology. The download speed of Tsinghua is very unstable these two days

root@rtlab-computer:/home/rtlab/szc/AndroidSource# wget http://mirrors.ustc.edu.cn/aosp-monthly/aosp-latest.tar

The total size of the compressed package is 119G. The download speed here is 10M, and it has been down for four hours.

After the download is completed, decompress it, and the decompression time is also relatively long

root@rtlab-computer:/home/rtlab/szc/AndroidSource# tar -xf aosp-latest.tar
root@rtlab-computer:/home/rtlab/szc/AndroidSource# cd aosp/

synchronization

First, we need to change the synchronization address to the address of China University of science and technology

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# vim .repo/manifests.git/config

Just change the url of the [remote "origin"] tag to that of China University of science and technology

url =  git://mirrors.ustc.edu.cn/aosp/platform/manifest

Then set the file mode of git

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# git config core.filemode false

Last synchronization

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# repo sync

If some code synchronization fails halfway, don't rush to ctrl-c. after the repo sync command ends, run the following command to re synchronize the failed part until it is completed

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# repo sync -j1 --fail-fast

After that, we can get the following directory information

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# repo sync -j1 --fail-fast
warning: Python 3 support is currently experimental. YMMV.
Please use Python 2.6 - 2.7 instead.


... A new version of repo (2.15) is available.
... You should upgrade soon:
    cp /home/rtlab/szc/AndroidSource/aosp/.repo/repo/repo /usr/bin/repo


Fetching: 100% (1022/1022), done in 21m15.123s
Garbage collecting: 100% (1022/1022), done in 11.650s
Checking out: 100% (1022/1022), done in 57.294s
repo sync has finished successfully.
root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# ls
Android.bp  Makefile   art     bootable        build          cts     developers   device    frameworks  kernel   libnativehelper  pdk               prebuilts  system  toolchain
BUILD       WORKSPACE  bionic  bootstrap.bash  compatibility  dalvik  development  external  hardware    libcore  packages         platform_testing  sdk        test    tools
root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp#

compile

Install lunch

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# apt install python-lunch

Setting environment

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# source build/envsetup.sh

compile

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# lunch aosp_arm-eng && make -j4

Failed to create parent directories of empty Ninja glob file 'xxx / AOSP / bootstrap/build-globs. ninja': mkdir XXX/aosp/. Bootstrap permission denied, manually create this directory, set AOSP directory permission to 777, and then recompile

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# mkdir -p /home/rtlab/szc/AndroidSource/aosp/.bootstrap && chmod -R 777 .
root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# make -j4

Post a screenshot of the successful compilation (in fact, after compiling all morning, the first compilation failed halfway. Set the permission of the whole aosp directory to 777 and then compile it successfully)

Use AS to view the source code

Compile idegen section

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# make idegen -j4
root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# development/tools/idegen.sh

The screenshot after completion is as follows

Optimize read options

Open android.com under the root directory of aosp iml

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# vim android.iml

First delete all orderEntry tags to the following three lines

    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="inheritedJdk" />
    <orderEntryProperties />

Then add a filter directory on < / content > to open only the framework source code

.......
<excludeFolder url="file://$MODULE_DIR$/.repo" />
<excludeFolder url="file://$MODULE_DIR$/art" />
<excludeFolder url="file://$MODULE_DIR$/bionic" />
<excludeFolder url="file://$MODULE_DIR$/bootable" />
<excludeFolder url="file://$MODULE_DIR$/build" />
<excludeFolder url="file://$MODULE_DIR$/compatibility" />
<excludeFolder url="file://$MODULE_DIR$/dalvik" />
<excludeFolder url="file://$MODULE_DIR$/developers" />
<excludeFolder url="file://$MODULE_DIR$/developers/samples" />
<excludeFolder url="file://$MODULE_DIR$/development" />
<excludeFolder url="file://$MODULE_DIR$/device/google" />
<excludeFolder url="file://$MODULE_DIR$/device/sample" />
<excludeFolder url="file://$MODULE_DIR$/docs" />
<excludeFolder url="file://$MODULE_DIR$/external" />
<excludeFolder url="file://$MODULE_DIR$/flashing-files" />
<excludeFolder url="file://$MODULE_DIR$/frameworks/base/docs" />
<excludeFolder url="file://$MODULE_DIR$/kernel" />
<excludeFolder url="file://$MODULE_DIR$/libcore" />
<excludeFolder url="file://$MODULE_DIR$/libnativehelper" />
<excludeFolder url="file://$MODULE_DIR$/out" />
<excludeFolder url="file://$MODULE_DIR$/pdk" />
<excludeFolder url="file://$MODULE_DIR$/platform_testing" />
<excludeFolder url="file://$MODULE_DIR$/prebuilt" />
<excludeFolder url="file://$MODULE_DIR$/prebuilts" />
<excludeFolder url="file://$MODULE_DIR$/shortcut-fe" />
<excludeFolder url="file://$MODULE_DIR$/test" />
<excludeFolder url="file://$MODULE_DIR$/toolchain" />
<excludeFolder url="file://$MODULE_DIR$/tools" />

    </content>
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="inheritedJdk" />
    <orderEntryProperties />
  </component>
</module>

View source code

Open Android Studio and select Android IPR on

The final results are shown in the figure below:

Although there is a red flag, the code of java layer can be directly viewed by pressing ctrl + left mouse button, but the native layer can't be viewed. Therefore, to view the native layer, you still need to go to the jni directory (sublime text can be used here, or in the command line):

epilogue

Finally, I recommend a website to view the Android source code (java layer and C layer) online: http://androidxref.com/

Topics: Android