Ubuntu编译Andorid源码并运行模拟器

本机环境

  • OS Name: Ubuntu 24.04.2 LTS
  • OS Type: 64-bit
  • Kernel Version: Linux 6.11.0-26-generic
  • Processor: Intel® Core™ Ultra 7 155H × 22
  • Memory: 32.0 GiB
  • Disk Capacity: 1.0 TB

安装repo

创建一个bin目录,并将这个目录添加到系统的环境变量中

mkdir ~/bin
PATH=~/bin:$PATH

下载 repo

curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo

使用 repo同步源码

建立工作目录

mkdir aosp
cd aosp

使用repo init 指令初始化源码仓库

export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'
repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-12.0.0_r34
repo sync

配置编译环境

sudo apt update
sudo apt install flex bison build-essential zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 libncurses5 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z1-dev libgl1-mesa-dev libxml2-utils xsltproc fontconfig -y
sudo apt install make git-core gnupg zip unzip curl python3 openjdk-11-jdk -y
sudo apt clean && sudo apt autoremove -y

编译 Android 镜像

cd aosp
source build/envsetup.sh
lunch aosp_arm64-eng
make 

启动模拟器

emulator

emulator -avd Pixel_5_API_31 -writable-system -no-snapshot
-writable-system:允许修改系统分区(推送系统应用必需)
//显示模拟器列表
emulator -list-avds

常见问题

安装 libncurses5失败,报Unable to locate package libncurses5

方法一:从阿里云镜像仓库手动下载安装旧版本包
URL地址:https://mirrors.aliyun.com

wget https://mirrors.aliyun.com/ubuntu/pool/universe/n/ncurses/libncurses5_6.3-2ubuntu0.1_amd64.deb
sudo dpkg -i libncurses5_6.3-2ubuntu0.1_amd64.deb

启动模拟器失败,报找不到userdata-qemu.img

修改build/make/target/product/AndroidProducts.mk文件,添加sdk_开头的几个选项

COMMON_LUNCH_CHOICES := \
    aosp_arm64-eng \
    aosp_arm-eng \
    aosp_x86_64-eng \
    aosp_x86-eng \
    sdk_x86_64-eng \
    sdk_phone_x86_64-eng \
    sdk_x86_64-userdebug \
    sdk_phone_x86_64-userdebug

然后重新编译,执行以下语句:

lunch sdk_x86_64-eng

参考资源

posted @ 2025-06-08 21:16  南极冰川雪  阅读(86)  评论(0)    收藏  举报