AAOS - Build Automotive Android
Tags: automotiveandroid
Android Automotive extends Android, by adding support for automotive-specific requirements, features, and technologies. It shares the same codebase with Android Open Source Project for phone, tablets, etc.
For General guide for Android Devices: https://source.android.com/docs/setup.
For Automotive Android Devices: https://source.android.com/docs/automotive.
Host Requirements
Refer: https://source.android.com/docs/setup/start/requirements.
Minimal Hardware
- 64-bit Linux System with
glibc >= 2.17
(check withldd --version
) - 512 GB SSD (need more when using repo mirroring)
- 16 GB RAM (need more when using more parallel threads
-jN
)
Build tools
Required packages:
May need these:
Recommend to use ccache
for GCC/G++ compilation:
Add below line into ~/.bashrc
to use ccache for AOSP build:
Repo tool
The Repo launcher provides scripts that to checkout and download a full repo:
Android Source Code
The Android source is located in a collection of Git repositories hosted by Google.
Download Repo
For the latest source code, default main branch is used (i.e. -b main
).
For faster cloning, refer to Git partial clone and shallow clone.
For a list of branches and tag names, see Source code tags and builds:
Codename | Version | API Level | Tag | Build ID | Security patch |
---|---|---|---|---|---|
Android15 | 15 | 35 | android-15.0.0_r1 | AP3A.240905.015.A2 | 2024-09-05 |
Android14 | 14 | 34 | android-14.0.0_r74 | AD1A.240905.004 | 2024-09-05 |
Android13 | 13 | 33 | android-13.0.0_r83 | TQ3A.230805.001.S1 | 2023-08-05 |
Android12L | 12 | 32 | android-12.1.0_r27 | SP2A.220505.008 | 2022-05-05 |
Android12 | 12 | 31 | android-12.1.0_r26 | SD2A.220601.004.B2 | 2022-06-01 |
-
Define macros:
-
Initialize the repo using shallow clone:
-
Sync (download) source code of the current branch:
Folders Structure
There are many folders in the root folder of AOSP, here are their short descriptions:
Directoryart/ Implementation of Android Runtime layer,
including Ahead-Of-Time (AOT) compilation, Just-In-Time (JIT) compilation.- …
Directorybionic/ Android is not using glibc like most Linux distributions, instead,
the c-library is called bionic based mostly on BSD-derived sources.- …
Directorybootable/ Bootloader, and some tools for recovery, flashing such as fastboot.
- …
Directorybuild/ Scripts, Configurations for Build process, coordinating the compilation, linking, and packaging of various components.
- …
Directorycts/ The Compatibility Test suite to ensure that a build complies with the Android specification.
- …
Directorydalvik/ Dalvik VM has been replaced by the Android Runtime (ART) since Android 5.0
- …
Directorydevelopers/ Assist developers in building, testing, and optimizing Android applications.
- …
Directorydevelopment/ Development tools such as the SDK and NDK.
- …
Directorydevice/ Build configuration, hardware modules and specific code for different devices,
containing BSP with overlay, board configs, device, kernel, and SEpolicy.- …
Directoryexternal/ Contains source code for all external open source projects, such as SQLite, FreeType and WebKit.
- …
Directoryframeworks/ Implementation of key services such as System Server, Activity Managers, having the combination of Java and C/C++ native libraries where interaction between sources will happen, such as mapping between the java application APIs and the native C++ libraries.
- …
Directoryhardware/ The Android Hardware Abstraction Layer specification and implementation.
- …
Directorykernel/ Prebuilt kernel files. Just binary files, source code does not come here.
- …
Directorypackages/ Source code of default application such as Contacts, Calendar, etc.
- …
Directoryprebuilt/ Files that are distributed in binary form for convenience, including the cross compilations toolchain for different development machines.
- …
Directorysystem/ That is the minimal Linux system that is started before the Dalvik VM and any java based services are enabled.
This includes the source code for the init process and the default init.rc script that provide the dynamic configuration of the platform.- …
Directoryvendor/ It is vendor-specific and contains custom apps, SE policies, hardware configurations, and so on.
This folder is not part of the AOSP source.- …
Source code Tools
- AOSP Build Environment is provided in the script
build/envsetup.sh
to initialize the workspace, and it must be always activated before any build. - Android Code Search is an online source code searching engine, and cross referencing.
- Android Studio for Platform is the version of the Android Studio IDE for Android Open Source Project platform.
Set up Environment
Activate AOSP Build Environment:
This script provides methods to make different targets, commands for searching:
Make targets
List of make targets:
make all
- make everything, whether it is included in the product definition or not.make clean
- remove all built files (prepare for a new build).make sdk
- build the tools that are part of an SDK (adb, fastboot, etc.)make snod
- build the system image from the current software binaries.make modules
- shows a list of submodules that can be built.make <local_module>
- make a specific module defined in LOCAL_MODULE in the Android.mk, not the same as directory namemake clean-<local_module>
- clean a specific module
Build Macros
croot
- change directory to the top of the treem
- execute make from the top of the tree (in any directory)mm
- builds all the modules in the current directorymmm <dir1> ...
- Builds all the modules in the supplied directories, but not their dependencies.mma
- Builds all the modules in the current directory, and their dependencies.mmma <dir1> ...
- Builds all the modules in the supplied directories, and their dependencies.allmod
- List all of Modules.gomod <module>
- Jump to the folder of calling module.outmod <module>
- Returns the output file for the module (.so/.jar/.apk/…)
Search Macros
cgrep <PATTERN>
- Greps on all local C/C++ files.ggrep <PATTERN>
- Greps on all local Gradle files.jgrep <PATTERN>
- Greps on all local Java files.resgrep <PATTERN>
- Greps on all local res/*.xml files.mangrep <PATTERN>
- Greps on all local AndroidManifest.xml files.mgrep <PATTERN>
- Greps on all local Makefiles files.sepgrep <PATTERN>
- Greps on all local sepolicy files.sgrep <PATTERN>
- Greps on all local source files.godir <filename>
- Go to the directory containing a file
Select a Target
There are some kinds of target device:
Physical Device
- This is the real hardware, but it is not always available
- The best environment to test and develop because it is how thing actually run with real devices and environment
- The build will need some specific packages for the target, such as the drivers, configurations (usually known as Board Support Packages)
Android Virtual Device
Android Emulator can run the entire Android Architecture with different Android Virtual Device (AVD) images:
- An AVD contains the full Android software stack, with many functional hooks to appeal to the use cases of the Android app developer
- This may present challenges when building an AVD with customized Android framework due to virtual drivers / devices
- The target is usually named
sdk_car
Cuttlefish Virtual Device
Cuttlefish is a configurable virtual Android device that can run both remotely (using third-party cloud offerings such as Google Cloud Engine) and locally (on Linux x86 and ARM64 machines).
- Cuttlefish Virtual Device contains a configurable virtual Android device, including all of the framework code and any custom platform code
- Replicate the framework-based behavior of a real device with a focus on high fidelity by maintaining close alignment with the core framework
- The target is usually named
aosp_cf_*_auto
Product Definition
The target selection is an option listed in COMMON_LUNCH_CHOICES
which is defined across multiple AndroidProducts.mk
files.
To see all lunch target, run the command:
If there is an error that prevents lunch to show all target. Here is a quick fix:
- run
export TARGET_RELEASE=trunk_staging
, - then run
build_build_var_cache
, - then finally run
lunch
again.
Each target name contains:
TARGET_PRODUCT
, for example, a combination ofaosp
for google AOSP, then optionallycf
for cuttlefish device, then Architecturex86_64
and Hardware (codename) such asauto
, to make the final nameaosp_cf_x86_64_auto
TARGET_RELEASE
is set totrunk_staging
.TARGET_BUILD_VARIANT
is one ofuser
(for production),userdebug
(with debugging enabled), andeng
(fast build, for development).
Build Cuttlefish Emulator
Cuttlefish is recommended for platform development. Working on Automotive Android is likely to do something in the Platform side, so this is the first choice when selecting an emulator.
Build Cuttlefish Image
Find a cuttlefish product:
-
Enable development environment:
-
Select the target:
-
Now, compile the target:
The image is located at
out/target/product/vsoc_x86_64_only
.
Some problems may happen:
-
ccache is readonly → mount ccache to a different point:
-
test is not found → exclude them from test list:
-
some random failures → lower the threads:
such as from-j20
to-j10
Build Cuttlefish Device
Refer: https://source.android.com/docs/devices/cuttlefish/get-started.
Verify KVM availability, this command should return a nonzero value:
-
Download support packages:
-
Download Cuttlefish source code:
-
Compile Cuttlefish packages:
-
Install Cuttlefish packages:
-
Finally, reboot system to apply changes, trigger installing additional kernel modules and applies udev rules.
Launch Cuttlefish
-
Cuttlefish virtual device needs to know the built target’s artifacts to find required configs and images, so it always needs to enter the build environment:
-
Now run the emulator:
If want to a refresh run:
Cuttlefish launches with --start_webrtc
, which enables a webview through port 8443 on the host machine.
Useful debugging information:
- Point browser to https://localhost:8443 to interact with the device.
- Serial console is disabled; use
-console=true
to enable it. - Logcat output:
~/cuttlefish/instances/cvd-1/logs/logcat
- Kernel log:
~/cuttlefish/instances/cvd-1/kernel.log
- Launcher log:
~/cuttlefish/instances/cvd-1/logs/launcher.log
- Instance configuration:
~/cuttlefish/instances/cvd-1/cuttlefish_config.json
- Launcher Build ID:
eng.trongv.20241007.012401
Android Platform Tools
Android platform tools are provide in this package:
Emulator can be accessed via adb as a normal Android device. For example, check the kernel version:
The kernel version is correct for android14.
For stop, restart or reset the emulator:
- If device is responsive, perform a clean restart using
adb reboot
. - If device is unresponsive, perform an unclean restart using
restart_cvd
. - Reset the device state using
powerwash_cvd
. - Stop the device and exit
stop_cvd
- Stop the device and launch again with specifi flag
launch_cvd --resume=false
Build Android Virtual Device
Build AVD Target
Find an AVD SDK product:
-
Enter development environment
-
Select the target:
-
Now, compile the target:
The image is located at
out/target/product/emulator_car64_x86_64
.
Run AVD Emulator
Android Emulator is provided as a prebuilt tool in AOSP, located in prebuilts/android-emulator/linux-x86_64/emulator. Building Emulator requires to checkout then compile a different repo.
Run the emulator:
Emulator Command Line provides options to run an emulator. Basically, for development:
Most used options:
verbose
: displays which files and settings are actually selected when startingshell
: open the root shellshow-kernel
: option to show the kernel console, it’s good for debuggingselinux {disabled|permissive}
: SELinux will not be enforcedgpu {host|guest}
: usewritable-system
: system is writable so it can save user’s created filesno-boot-anim
: disable the boot animationno-snapshot
: do not save system statewipe-data
: remove all user data
Connect to he device shell, and check the kernel version:
There is an error that the service bt*
is created and killed repeatly.
It might happen because of wrong kernel version! It should be android14 for sdk_car_x86_64.
P/s: even using kernel in branch common-android14-6.1
, the above issue still happens.
Build Trout Device
Refer to: https://source.android.com/docs/automotive/virtualization/reference_platform
Through a new product named trout
, Android Automotive now provides support for deployment as a guest virtual machine (VM) in environments compatible with the VirtIO standard.
trout
is based on the Cuttlefish virtual reference platform and is available as the trout
device configuration, such as aosp_trout_x86_64-trunk_staging-userdebug
.
However, at this time, trout
target can not be built!