Android12 source code compilation error ninja: build stopped: subcommand failed. Solution

1. Background description

Virtual machine: wsl 2.0 Ubuntu18.04
Virtual machine configuration: 8G memory, 1T storage
Software version: The current highest version of the Android system is Android S, which is the Android 12 beta version source code without any modifications.
Error content: When compiling to 54%, ninja compilation was interrupted, ninja: build stopped: subcommand failed.

[ 54% 26841/49115] //prebuilts/rust:libtest rustc linux-x86/1.51.0/src/stdlibs/library/test/src/lib.rs
[54% 26842/49115] //frameworks/av/services/mediametrics:mediametrics clang + + main_mediametrics.cpp
[54% 26843/49115] Copy: out/target/product/G10/symbols/system/lib/libtest.dylib.so
[54% 26844/49115] //frameworks/av/media/libmediatranscoding:mediatranscoding_aidl_interface-ndk_platform clang + + android/media/TranscodingResultParcel.cpp [com.android.media]
[54% 26845/49115] //frameworks/compile/libbcc/bcinfo:libbcinfo header-abi-linker libbcinfo.so.lsdump
FAILED: out/soong/.intermediates/frameworks/compile/libbcc/bcinfo/libbcinfo/android_vendor.31_arm_armv7-a-neon_cortex-a7_shared/libbcinfo.so.lsdump
prebuilts/clang-tools/linux-x86/bin/header-abi-linker -o out/soong/.intermediates/frameworks/compile/libbcc/bcinfo/libbcinfo/android_vendor.31_arm_armv7-a-neon_cortex-a7_shared/libbcinfo.so. lsdump -so out/soong/.intermediates/frameworks/compile/libbcc/bcinfo/libbcinfo/android_vendor.31_arm_armv7-a-neon_cortex-a7_shared/libbcinfo.so -arch arm -Iframeworks/compile/libbcc/bcinfo/include @out/soong /.intermediates/frameworks/compile/libbcc/bcinfo/libbcinfo/android_vendor.31_arm_armv7-a-neon_cortex-a7_shared/libbcinfo.so.lsdump.rsp
Failed to parse JSON: * Line 1, Column 1
  Syntax error: value, object or array expected.

ReadDump failed
[54% 26846/49115] //prebuilts/gradle-plugin:metalava-gradle-plugin-deps for prebuilts [linux_glibc common]
[54% 26847/49115] //external/rust/crates/android_log-sys:libandroid_log_sys rustc src/lib.rs
[54% 26848/49115] //frameworks/base/media:media_permission-aidl-cpp clang-tidy android/media/permission/Identity.cpp
[54% 26849/49115] //frameworks/av/media/libmediametrics:mediametricsservice-aidl-cpp clang-tidy android/media/IMediaMetricsService.cpp
[54% 26850/49115] build out/target/product/G10/gen/META/lic_intermediates/webrtc_agc__agc.meta_lic
[54% 26851/49115] //frameworks/av/media/codec2/components/aac:libcodec2_soft_aacdec link libcodec2_soft_aacdec.so
[54% 26852/49115] //external/rust/crates/anyhow:libanyhow rustc src/lib.rs
[54% 26853/49115] //external/rust/crates/anyhow:libanyhow rustc src/lib.rs
[54% 26854/49115] build out/target/product/G10/gen/META/lic_intermediates/libbt-stack.meta_lic
[54% 26855/49115] build out/target/product/G10/gen/META/lic_intermediates/audio.a2dp.default.meta_lic
[54% 26856/49115] build out/target/product/G10/gen/META/lic_intermediates/libbt-audio-hal-interface.meta_lic
[54% 26857/49115] //frameworks/av/media/libmediatranscoding:mediatranscoding_aidl_interface-ndk_platform clang-tidy android/media/TranscodingSessionStats.cpp [com.android.media]
[54% 26858/49115] build out/target/product/G10/gen/META/lic_intermediates/avrcp-target-service.meta_lic
[54% 26859/49115] //frameworks/av/media/libmediatranscoding:mediatranscoding_aidl_interface-ndk_platform clang-tidy android/media/ITranscodingClientCallback.cpp [com.android.media]
[54% 26860/49115] //frameworks/av/media/libmediatranscoding:mediatranscoding_aidl_interface-ndk_platform clang-tidy android/media/TranscodingVideoTrackFormat.cpp [com.android.media]
[54% 26861/49115] //frameworks/av/media/libmediatranscoding:mediatranscoding_aidl_interface-ndk_platform clang-tidy android/media/IMediaTranscodingService.cpp [com.android.media]
[54% 26862/49115] //frameworks/av/media/libmediatranscoding:mediatranscoding_aidl_interface-ndk_platform clang-tidy android/media/ITranscodingClient.cpp [com.android.media]
[54% 26863/49115] //frameworks/av/media/libmediatranscoding:mediatranscoding_aidl_interface-ndk_platform clang-tidy android/media/TranscodingSessionParcel.cpp [com.android.media]
[54% 26864/49115] //frameworks/av/media/libmediatranscoding:mediatranscoding_aidl_interface-ndk_platform clang-tidy android/media/TranscodingRequestParcel.cpp [com.android.media]
[54% 26865/49115] //art/dexlayout:libart-dexlayout link libart-dexlayout.so [apex31]
[54% 26866/49115] //frameworks/av/media/codec2/components/hevc:libcodec2_soft_hevcenc link libcodec2_soft_hevcenc.so
[54% 26867/49115] //external/libvpx:libvpx link libvpx.so
[54% 26868/49115] //frameworks/av/media/codec2/components/gav1:libcodec2_soft_av1dec_gav1 link libcodec2_soft_av1dec_gav1.so
[54% 26869/49115] //external/libvpx:libvpx link libvpx.so
[54% 26870/49115] //system/logging/logd:logd link logd
ninja: build stopped: subcommand failed.
19:21:50 ninja failed with: exit status 1

[0;31m#### failed to build some targets (09:08 (mm:ss)) ####[00m


#### failed to exec [make droid dist BUILD_RTK_STYLE_IMAGE=true DIST_DIR=/home/daqiang_ni/samba/airplay75p/vendor/realtek/common/kernel/system/tmp/dist/android] (09:08 (mm:ss) ) ####
tcl ret_tclbuild = 1

#### failed to exec [trom_build] (22:50 (mm:ss)) ####

2. Try the plan

After the error was reported, I tried multiple incremental compilations, and always reported an error at 54%, with the same error content.
I suspect that the sync is not complete, so I repo sync and it prompts success, but the error is still reported when compiling again.
Other colleagues said that the compilation passed. Considering the different compilation environments and using the VMWare virtual machine environment to compile, 97% still reported the same error.
Try make clean,rm -rf out/ to clear the compilation environment and then compile, but an error is still reported.
According to the information, the compilation was interrupted due to insufficient Linux memory during compilation. Wsl2 extended the VHD to 10G (the method is as follows), but the same error was still reported.
Add wsl2 configuration file
Add the .wslconfig file to the C:\Users\userName path
.wslconfig

[wsl2]
memory=10GB
processors=8

The content of .wslconfig is as above, the memory configuration is 10GB, and the processor has 8 cores or 16 threads.
Restart Ubuntu18.04 and check the memory status. It has been modified to 10GB.

$ free memory -h
              total used free shared buff/cache available
Mem: 9.7G 2.6G 1.8G 36K 5.3G 6.9G
Swap: 3.0G 33M 3.0G

3. Problem analysis and processing

Use the following command to continuously print the Linux memory usage at intervals of 1 second during compilation. When an error occurs, the memory is only about 110MB. It can be basically judged that the compilation is interrupted due to tight system memory during compilation.

$watch -d -n 1 free memory -h
Every 1.0s: free memory -h gd08575: Tue Aug 2 09:41:24 2022
              total used free shared buff/cache available
Mem: 9.7G 2.9G 912M 36K 5.9G 6.6G
Swap: 3.0G 37M 3.0G

#Or use the command

$ while true;do free memory -h;sleep 1;done
              total used free shared buff/cache available
Mem: 9.7G 3.5G 4.1G 36K 2.1G 6.0G
Swap: 3.0G 41M 3.0G
              total used free shared buff/cache available
Mem: 9.7G 3.3G 4.2G 36K 2.2G 6.1G
Swap: 3.0G 41M 3.0G
...

When the error is reported and the memory is checked, the available memory is about 4GB or less. Use the htop command to check the Linux process and find that there are still many compilation processes running. Therefore, try restarting wsl2 to release the memory and compile again.

#After closing Ubuntu18.04LTS, open WindowsPowerShell with administrator rights and restart wsl
$wsl –shutdown

Enter Ubuntu18.05LTS to check the memory status, 9GB is available, try Android incremental compilation again, and the compilation is successful!

Reference study