Compile and build AOSP code

The power of CLI is always admirable.

#Overview

  • In the last chapter Aosp setup on Ubuntu machine, We have covered how to set up Ubuntu machine and download AOSP source code. Now after downloading we need to explore, compile and build source code so that it will open in Emulator or physical device. Here I have listed few commands which are mostly used in compiling and building process.
  • We will definitely cover exploring, editing, and customising our own Android OS in the coming chapters.

# Compile full source code after downloading
$: source build/envsetup.sh
$: lunch
$: make -j8

  • After preparing the compilation environment, the first step in compiling the Android source code is “source build/envsetup.sh” where the source command is used to run shell script commands. The function is equivalent to “.”, So this command is also equivalent to “. build/envsetup.sh”. Every time you open a new terminal all the above 3 commands are needed to apply.
  • Lunch command is for board configuration selection where you get a list of choices to select.
  • make command will start build process from the root directory. Here we have added -j8 which indicates threads to be run in parallel. We can use j2, j4, j8, j12, j16…depending upon our computer cores.

# Commands to compile individual module

  • There are IDE’s to explore the code even you can edit and debug in the editor but compiling individual module is always prefered using terminal commands so here are few commands which will be useful while development.
    1. m : Compile at the root of the source tree
    2. mm : Compile all modules in the current path, but do not include dependencies
    3. mmm : Compile all modules in the specified path, but do not include dependencies
    4. mma : Compile all modules in the current path and include dependencies
    5. mmma : Compile all modules in the specified path, and include dependencies
    6. make : Without parameters, it means that the entire Android code is compiled

# Compilation instructions of some modules using make

  • Most of the time we need to compile following modules so here are few commands we can use to compile it.
    1. init : make init
    2. zygot: make app_process
    3. system_server : make services
    4. java framework : make framework
    5. framework resources : make framework-res
    6. jni framework : make libandroid_runtime
    7. binder : make libbinder

# Compilation using mmm or other commands

  • All the above make commands can be applied using mmm or similar following way.
    1. init : mmm system/core/init
    2. zygot : mmm frameworks/base/cmds/app_process
    3. system_server : mmm frameworks/base/services
    4. java framework : mmm frameworks/base
    5. framework resources : mmm frameworks/base/core/res
    6. jni framework : mmm frameworks/base/core/jni
    7. binder : mmm frameworks/native/libs/binder

# Code Search

  • Generally, we use IDE’s to explore and edit code so following commands will not be frequently required but yes we should know about those commands. Those who have hands-on experience of the terminal and those commands can use those happily.
    1. cgrep : Search operations on all C / C ++ files
    2. jgrep : Perform a search operation on all Java files
    3. ggrep : Perform a search operation on all Gradle files
    4. mangrep : Perform a search operation on all AndroidManifest.xml files
    5. mgrep : Perform a search operation on all Android.mk files
    6. sepgrep : Search for all sepolicy files
    7. resgrep : Perform search operations on all local res / *. Xml files
    8. sgrep : Perform a search operation on all resource files

# Other commonly used commands are

  1. make clean : perform a clean operation, equivalent to rm -rf out/
    2. make update-api : Update the API. This instruction needs to be executed after the framework API is changed. API is recorded in the directory frameworks/base/api.

Feel free to comment for your queries regarding the topic.

Software Engineer. (Mobile and Web)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store