Sunday, 4 June 2023

AOSP latest Source code download(Android 13), Build and launching Emulator

  Hi Guys!!!

Hope you are doing well !. Today I am going to discuss latest AOSP source code download, Build and launch Emulator.

I had written about it long back ago almost 6 year back in my blog . But I am revisited same topics due to so many changes done by google over the year.

My Previous Blog post link 

Hardware requirements

As per google below is the recommended hardware 

  • A 64-bit environment (Linux Ubuntu 18.04 or 20.04)
  • At least 250GB of free disk space to check out the code and an extra 150 GB to build it
  • At least 16 GB of available RAM is required, but Google recommends 64 GB

As of June 2021, Google is using 72-core machines with 64 GB of RAM internally, which take about 40 minutes for a full build . By contrast, a 6-core machine with a similar amount of RAM takes 3 hours.

From hardware requirement perspective such configuration machine (laptop/desktop) is costly . 
If you are working in any organisation they provide you such configuration . 
But If you want to do R&D for learning in that case getting such configuration laptop is costly affairs !!!

But I will help you to build AOSP even on low configuration laptop (8GB RAM ). The only tradeoff is it increase build time 6 to 8 hours on first build and incremental build 30 to 1 hours.  If you have 16 GB RAM laptop then it will take half time. 

Establishing Build Environment
If you fresh installation of Ubuntu 18 or 20 then you need to get software setup before Source code download and build . Few example Software 
  • Git
  • Repo
  • Vim
  • Open JDK
  • Build Tool Chain

Downloading the source Code 

To check out the master branch:
repo init -u https://android.googlesource.com/platform/manifest -b master

To checkout Android 13 code that support all most all devices
repo init -u https://android.googlesource.com/platform/manifest -b android-13.0.0_r43


Repo Sync Command 
repo sync -c -j8
-c (current branch) and -jthreadcount flags

Building Android AOSP Source Code 
Once AOSP source code downloaded successfully then you can start Building . 
This is the most crucial part of Low end Device (Low Config Laptop/Desktop having 8GB or 16 GB RAM)

First Establish the Build 
source build/envsetup.sh
lunch 

Now chose the correct Build Target. Best way to choose build target is to check your build machine CPU arc. Just run below command 
     lscpu 
It will give your build machine CPU arch details.If it says x86_64, then choose 
sdk_phone_x86_64_userdebug  or 
sdk_car_x86_64_userdebug  lunch option. 

If you choose the lunch option like this, it would make Android images that run really fast on your machine.
After choosing correct lunch mode you can start build by giving below command 
make -j4 or -j8 (based on CPU thread)
if you want to capture build logs then you can use command like below 
make -j8 2>&1 | tee build.log
Once build started you low end machine will gibe build error. 

Here I am listing down most frequent or common issue will come 

1. Swap Size issue 
If build terminal get close or exit automatically without giving any issue  or wring or error. This means your build machine facing swap memory issue. you need to increase the swap size of your machine. 
Check for the Swap Information
   sudo swapon --show
Deactivate the swap space
   sudo swapoff /swapfile
Use fallocate to instantly creates a file with the specified size.
   sudo fallocate -l 16G /swapfile
Set the swap space
   sudo mkswap /swapfile
Activate the swap space
   sudo swapon /swapfile
Now check for the Swap Information
   sudo swapon --show
Now the output will be something like below,
NAME      TYPE   SIZE   USED   PRIO
/swapfile file   16G     0B    -2


2. build.ninja generate took too long time 
if you get build log message like below

analyzing android.bp files and generating ninja file at out/soong/build.ninja 

With time lapse increasing count That means you build machine have low CPU thread and It is taking more time than usual. Wait with patience 40 to 60 minute it will take to generate snoog files. 
Then it will start actual compilation. 

3. Failed to build AOSP with below error 
 99% 396/397] analyzing Android.bp files and generating ninja file at out/soong/build.ninja
FAILED: out/soong/build.ninja
cd "$(dirname "out/host/linux-x86/bin/soong_build")" && BUILDER="$PWD/$(basename "out/host/linux-x86/bin/soong_build")" && cd / && env -i  "$BUILDER"     --top "$TOP"     --soong_out "out/soong"     --out "out"     -o out/soong/build.ninja --globListDir build --globFile out/soong/globs-build.ninja -t -l out/.module_paths/Android.bp.list --available_env out/soong/soong.environment.available --used_env out/soong/soong.environment.used.build Android.bp
error: prebuilts/module_sdk/conscrypt/current/Android.bp:20:1: dependency "art-bootclasspath-fragment" of "conscrypt-module-sdk_com.android.conscrypt-bootclasspath-fragment@current" missing variant:
  apex:com.android.art
available variants:
  os:android,arch:common
error: prebuilts/module_sdk/conscrypt/current/Android.bp:20:1: dependency "prebuilt_art-bootclasspath-fragment" of "conscrypt-module-sdk_com.android.conscrypt-bootclasspath-fragment@current" missing variant:
  apex:com.android.art
available variants:
  os:android,arch:common
13:57:24 soong bootstrap failed with: exit status 1
#### failed to build some targets (01:12 (mm:ss)) ####
0r
FAILED: out/soong/build.ninja
cd "$(dirname "out/host/linux-x86/bin/soong_build")" && BUILDER="$PWD/$(basename "out/host/linux-x86/bin/soong_build")" && cd / && env -i  "$BUILDER"     --top "$TOP"     --soong_out "out/soong"     --out "out"     -o out/soong/build.ninja --globListDir build --globFile out/soong/globs-build.ninja -t -l out/.module_paths/Android.bp.list --available_env out/soong/soong.environment.available --used_env out/soong/soong.environment.used.build Android.bp
error: prebuilts/module_sdk/conscrypt/current/Android.bp:20:1: dependency "art-bootclasspath-fragment" of "conscrypt-module-sdk_com.android.conscrypt-bootclasspath-fragment@current" missing variant:
  apex:com.android.art
available variants:
  os:android,arch:common
error: prebuilts/module_sdk/conscrypt/current/Android.bp:20:1: dependency "prebuilt_art-bootclasspath-fragment" of "conscrypt-module-sdk_com.android.conscrypt-bootclasspath-fragment@current" missing variant:
  apex:com.android.art
available variants:
  os:android,arch:common
16:48:05 soong bootstrap failed with: exit status 1
#### failed to build some targets (18 seconds) ####

I've found a way to get everything working from a new environment
First of all, make sure you set your environment with the changes that Android recommends
Then include below  ~/.bashrc changes
export USE_CCACHE=1
ccache -M 10G
Close all terminal and start a new terminal. Once terminal start read its heading. If it is giving some warning to install cache then install it . 
sudo apt install ccache
After successful install again close terminal ans start fresh one . this time your see below message on top of your terminal 

Then run your build command

set_stuff_for_environment
source build/envsetup.sh
lunch sdk_car_x86_64_userdebug
make -j8 2>&1 | tee build.log

This time build will finish successfully. Then run emulator command to start the emulator. 

emulator

Below is the screenshot of emulator. 





Thanks
Saurabh
Happy Coding!!!!

No comments:

Post a Comment

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...