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

如何为Gradle管理的Android设备指定内存与内部存储分配

Gradle管理的模拟器能否通过命令行调整内存和内部存储参数?

我们在并行运行Android设备测试时会用到这类Gradle命令:

./gradlew pixel4api30DebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.aaa.bbb.ccc.Suites.TestSuite -Pandroid.experimental.androidTest.numManagedDeviceShards=3

平时在Android Studio中,我们可以通过AVD管理器调整模拟器的内存和内部存储参数(类似配置页面中直接修改对应数值的操作)。

可以通过Gradle配置实现这类参数调整,具体有两种方式:

1. 固定配置在build.gradle文件中

在模块的build.gradle(或build.gradle.kts)里,通过managedDevices块指定模拟器的硬件参数:

Groovy版本

android {
    testOptions {
        managedDevices {
            devices {
                pixel4api30(com.android.build.api.dsl.ManagedVirtualDevice) {
                    device = "Pixel 4"
                    apiLevel = 30
                    systemImageSource = "google"
                    // 配置内存和内部存储
                    hardwareProperties {
                        ramSize = "2048M" // 设置内存为2GB
                        internalStorageSize = "4096M" // 设置内部存储为4GB
                    }
                }
            }
        }
    }
}

Kotlin版本

android {
    testOptions {
        managedDevices {
            devices {
                create<ManagedVirtualDevice>("pixel4api30") {
                    device = "Pixel 4"
                    apiLevel = 30
                    systemImageSource = "google"
                    hardwareProperties {
                        ramSize = "2048M"
                        internalStorageSize = "4096M"
                    }
                }
            }
        }
    }
}

配置完成后,运行原Gradle测试命令即可生效。

2. 命令行动态传递参数

如果需要临时调整参数,可以在build.gradle中预留属性读取逻辑,再通过命令行传递参数:

修改build.gradle(Groovy示例)

android {
    testOptions {
        managedDevices {
            devices {
                pixel4api30(com.android.build.api.dsl.ManagedVirtualDevice) {
                    device = "Pixel 4"
                    apiLevel = 30
                    systemImageSource = "google"
                    hardwareProperties {
                        // 优先读取命令行参数,无参数时用默认值
                        ramSize = project.findProperty("testDeviceRam") ?: "2048M"
                        internalStorageSize = project.findProperty("testDeviceStorage") ?: "4096M"
                    }
                }
            }
        }
    }
}

命令行运行示例

./gradlew pixel4api30DebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.aaa.bbb.ccc.Suites.TestSuite -Pandroid.experimental.androidTest.numManagedDeviceShards=3 -PtestDeviceRam=3072M -PtestDeviceStorage=8192M

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 07:45:37