You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker中使用Android Gradle Managed Devices遇快照创建失败求助

Android Gradle Managed Devices在Docker中创建快照失败的解决建议

我在项目中使用Android Gradle Managed Devices,配置如下:

devices {
        pixel2(com.android.build.api.dsl.ManagedVirtualDevice) {
            device = "Pixel 2"
            apiLevel = 30
            systemImageSource = "google"
        }
}

本地运行一切正常,但在Docker镜像中运行时,始终出现以下错误:

* What went wrong:
Execution failed for task ':mobile:pixel2Setup'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.ManagedDeviceSetupTask$ManagedDeviceSetupRunnable
   > Snapshot setup ran successfully, but the snapshot failed to be created. This is
     likely to a lack of disk space for the snapshot. Try the cleanManagedDevices
     task with the --unused-only flag to remove any unused devices for this project.

我已尝试使用-Pandroid.testoptions.manageddevices.emulator.gpu="swiftshader_indirect"参数(适用于无硬件渲染场景,如GitHub Actions),也尝试过ATD镜像。

Docker镜像已安装最新命令行工具(commandlinetools-linux-8512546_latest.zip),安装脚本如下:

RUN curl -sS https://dl.google.com/android/repository/${SDK_VERSION} -o /tmp/sdk.zip \
    && mkdir -p ${ANDROID_HOME}/cmdline-tools \
    && unzip -q -d ${ANDROID_HOME}/cmdline-tools /tmp/sdk.zip \
    && mv ${ANDROID_HOME}/cmdline-tools/cmdline-tools ${ANDROID_HOME}/cmdline-tools/latest \
    && rm /tmp/sdk.zip \
    && yes | sdkmanager --licenses \
    && yes | sdkmanager "platform-tools" \
        "emulator" \
        "platforms;android-$ANDROID_BUILD_VERSION" \
        "build-tools;$ANDROID_TOOLS_VERSION" \
        "system-images;android-30;google_apis;x86" \
    && rm -rf ${ANDROID_HOME}/.android \
    && chmod 777 -R /opt/android \

搜索后找到相关Issue(Google IssueTracker编号235119877),以下是可行的解决建议:

  • 检查并调整Docker容器磁盘配额:错误提示指向磁盘空间不足,Docker默认容器磁盘配额可能无法满足模拟器快照创建需求。运行容器时可通过--storage-opt size=20G参数指定更大的磁盘空间,或在Docker Compose配置中添加storage_opt: size: 20G。
  • 提前清理旧快照:在执行测试任务前,先运行./gradlew cleanManagedDevices --unused-only,清理项目中未使用的设备快照,释放磁盘空间。
  • 调整快照存储路径:通过设置环境变量ANDROID_EMULATOR_HOME,将模拟器快照存储到宿主机挂载的大磁盘目录中,比如在启动容器时挂载目录并设置变量:-v /host/large/dir:/emulator-home -e ANDROID_EMULATOR_HOME=/emulator-home。
  • 匹配系统镜像源:Gradle配置中systemImageSource = "google",但Docker中安装的是google_apis版本的系统镜像,两者不匹配。可修改Gradle配置为systemImageSource = "google_apis",或在Docker脚本中安装纯Google镜像:system-images;android-30;google;x86。
  • 增加容器内存分配:模拟器运行和快照创建需要足够内存,Docker默认内存可能不足。启动容器时添加-m 8G参数,为容器分配足够内存。
  • 尝试降低API级别:如果业务允许,可尝试将API级别降至29或更低,部分低版本系统镜像的快照创建资源需求更低,可能避免该问题。

内容的提问来源于stack exchange,提问作者moskis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 13:41:42