【问题标题】:Create an emulator with a decent size from command line从命令行创建一个大小合适的模拟器
【发布时间】:2015-10-02 19:40:57
【问题描述】:

我想创建一个加载了 Google API 的 ARM API 21 模拟器。

这是我创建 AVD 的方法:

ANDROID_ABI=google_apis/armeabi-v7a
EMULATOR_VERSION=21
avd --force -n nexus4-emulator -t "Google Inc.:Google APIs:"$EMULATOR_VERSION --abi $ANDROID_ABI --device "Nexus 4" -c 128M

现在我启动模拟器:

emulator -avd nexus4-emulator &

但是,这就是我得到的:一个尺寸很小的屏幕(就有效像素而言):

如何更改此配置以使屏幕尺寸合适?

顺便说一句,我正在使用它在 Travis 的机器上构建 APK 和运行测试,但它可以在我的机器 (Mac OS X) 上重现。

【问题讨论】:

  • 有趣的是(阅读:非常令人沮丧)两周前,这个确切的配置确实创建了一个标准的 Nexus 4 屏幕。我刚刚按下“在 Travis 中重建”键:dl.dropboxusercontent.com/u/1581166/stackoverflow/…
  • 另外,android list targets 确实列出了Nexus 4,所以--device 应该不是问题。

标签: android android-emulator travis-ci avd


【解决方案1】:

更新:使用新的 avdmanager 修复分辨率


This 是一个很好的示例,可以使用新的avdmanager 创建一个大小合适的模拟器:

  - $ANDROID_HOME/tools/bin/avdmanager create avd --force --name test --package 'system-images;android-22;default;armeabi-v7a' --abi armeabi-v7a --device 'Nexus 4' --sdcard 128M
  - $ANDROID_HOME/emulator/emulator -avd test -skin 768x1280 -no-audio -no-window &

关于不支持的-s "768x1280" 参数有一个open issue 作为commented here


上一个答案


如果你需要一个来自命令行的 nexus 4 风格的设备,你可以试试这个:

android create avd --force -n nexus4-emulator -t "Google Inc.:Google APIs:"$EMULATOR_VERSION --abi $ANDROID_ABI -s "768x1280" --device "Nexus 4" -c 128M

本地最好使用Android Studio。

下一个配置device 选项用于 CI 构建解决了与屏幕大小相关的点击问题:

  - echo no | android create avd --force -n test -t $ANDROID_TARGET --abi $ANDROID_ABI --device 'Nexus 4' --sdcard 128M
  - emulator -avd test -no-window -skin 768x1280 &

总结:


本地机器:最好的选择是直接使用 Android Studio,因为 Nexus 4 皮肤似乎只放置在那里,而 SDK 工具中包含的设备定义似乎不足以创建它;您需要定义额外的硬件选项。我在下面提供了一个解决方法。

Travis-ci 服务器:最好的选择是加速它禁用音频和窗口并使用通用模拟器。如果您需要 Nexus 4 风格的模拟器,您可以尝试相同的解决方法,否则,请删除 --device 选项并使用以下命令:

android create avd -f \
    -n "${AVD_NAME:-default_emulator}" \
    -t "${AVD_TARGET:-Google Inc.:Google APIs:21}" \
    -b "${AVD_ABI:-armeabi-v7a}" \
    -g "${AVD_TAG:-google_apis}" \
    -c "${AVD_SDCARD:-128M}"

emulator -avd "${AVD_NAME:-default_emulator}" \
    -no-audio \
    -no-window \
    -debug "${AVD_DEBUG:-avd_config}" &

注意:左边是环境变量,你可以在.travis.yml中定义,否则会使用右边的默认值。最后一行打印有用的相关调试信息。

本地配置


Nexus 4 设备定义包含在

android-sdk-linux/tools/lib/devices.xml

<d:device>
    <d:name>Nexus 4</d:name>
    <d:manufacturer>Google</d:manufacturer>
    <d:hardware>
        <d:screen>
            <d:screen-size>normal</d:screen-size>
            <d:diagonal-length>4.7</d:diagonal-length>
            <d:pixel-density>xhdpi</d:pixel-density>
            <d:screen-ratio>notlong</d:screen-ratio>
            <d:dimensions>
                <d:x-dimension>768</d:x-dimension>
                <d:y-dimension>1280</d:y-dimension>
            </d:dimensions>
            <d:xdpi>320</d:xdpi>
            <d:ydpi>320</d:ydpi>
            <d:touch>
                <d:multitouch>jazz-hands</d:multitouch>
                <d:mechanism>finger</d:mechanism>
                <d:screen-type>capacitive</d:screen-type>
            </d:touch>
        </d:screen>
        <d:networking>
            Wifi
            Bluetooth
            NFC
        </d:networking>
        <d:sensors>
            Accelerometer
            Barometer
            Compass
            GPS
            Gyroscope
            LightSensor
            ProximitySensor
        </d:sensors>
        <d:mic>true</d:mic>
        <d:camera>
            <d:location>back</d:location>
            <d:autofocus>true</d:autofocus>
            <d:flash>true</d:flash>
        </d:camera>
        <d:camera>
            <d:location>front</d:location>
            <d:autofocus>false</d:autofocus>
            <d:flash>false</d:flash>
        </d:camera>
        <d:keyboard>nokeys</d:keyboard>
        <d:nav>nonav</d:nav>
        <d:ram unit="KiB">1953125</d:ram>
        <d:buttons>soft</d:buttons>
        <d:internal-storage unit="KiB">7811891</d:internal-storage>
        <d:removable-storage unit="MiB"></d:removable-storage>
        <d:cpu>Qualcomm Snapdragon S4 Pro</d:cpu>
        <d:gpu>Adreno 320</d:gpu>
        <d:abi>
            armeabi-v7a
            armeabi
        </d:abi>
        <d:dock></d:dock>
        <d:power-type>battery</d:power-type>
    </d:hardware>
    <d:software>
        <d:api-level>16</d:api-level>
        <d:live-wallpaper-support>true</d:live-wallpaper-support>
        <d:bluetooth-profiles></d:bluetooth-profiles>
        <d:gl-version>2.0</d:gl-version>
        <d:gl-extensions>GL_EXT_debug_marker GL_AMD_compressed_ATC_texture
            GL_AMD_performance_monitor GL_AMD_program_binary_Z400 GL_EXT_robustness
            GL_EXT_texture_format_BGRA8888 GL_EXT_texture_type_2_10_10_10_REV GL_NV_fence
            GL_OES_compressed_ETC1_RGB8_texture GL_OES_depth_texture GL_OES_depth24
            GL_OES_EGL_image GL_OES_EGL_image_external GL_OES_element_index_uint
            GL_OES_fbo_render_mipmap GL_OES_fragment_precision_high GL_OES_get_program_binary
            GL_OES_packed_depth_stencil GL_OES_rgb8_rgba8 GL_OES_standard_derivatives
            GL_OES_texture_3D GL_OES_texture_float GL_OES_texture_half_float
            GL_OES_texture_half_float_linear GL_OES_texture_npot GL_OES_vertex_half_float
            GL_OES_vertex_type_10_10_10_2 GL_OES_vertex_array_object GL_QCOM_alpha_test
            GL_QCOM_binning_control GL_QCOM_driver_control GL_QCOM_perfmon_global_mode
            GL_QCOM_extended_get GL_QCOM_extended_get2 GL_QCOM_tiled_rendering
            GL_QCOM_writeonly_rendering GL_EXT_sRGB
        </d:gl-extensions>
        <d:status-bar>true</d:status-bar>
    </d:software>
    <d:state name="Portrait" default="true">
        <d:description>The phone in portrait view</d:description>
        <d:screen-orientation>port</d:screen-orientation>
        <d:keyboard-state>keyssoft</d:keyboard-state>
        <d:nav-state>nonav</d:nav-state>
    </d:state>
    <d:state name="Landscape">
        <d:description>The phone in landscape view</d:description>
        <d:screen-orientation>land</d:screen-orientation>
        <d:keyboard-state>keyssoft</d:keyboard-state>
        <d:nav-state>nonav</d:nav-state>
    </d:state>
</d:device>

但是android-studio关联和使用的nexus_4皮肤只在这里:

android-studio/plugins/android/lib/device-art-resources

android-studio/plugins/android/lib/device-art-resources/device-art.xml

  <device id="nexus_4" name="Nexus 4">
    <orientation name="port" size="958,1678" screenPos="94,187" screenSize="768,1280" shadow="port_shadow.png" back="port_back.png" lights="port_fore.png"/>
    <orientation name="land" size="1799,885" screenPos="257,45" screenSize="1280,768" shadow="land_shadow.png" back="land_back.png" lights="land_fore.png"/>
  </device>

您没有指定 --skin,因此使用 SDK 工具中包含的默认皮肤 WVGA800

此皮肤在hardware.ini 文件中具有关联的硬件值:

# skin-specific hardware values
hw.lcd.density=240
vm.heapSize=48
hw.ramSize=512

layout 文件包括下一行:

...
device {
    display {
        width   480
        height  800
        ...

这不是您在使用 Nexus 4 设备定义时所期望的,因此我将直接在本地使用 android-studio 来创建 avd 并使用正确的皮肤。

如果你像以前一样使用命令行并尝试从 android-studio 打开它,你会看到:

本地机解决方案

两者都使用 Android Studio;创建并启动模拟器。

如果您使用该命令创建它,添加至少 200MB 作为内部存储,您将解决屏幕大小问题,因为 android-studio 将使用正确的皮肤更新和修复 avd。

如果您正在寻找没有 android-studio 干预的命令,我将 Nexus 4 皮肤移动到 skins 文件夹,添加了一个带有缺失属性的 hardware.ini,最后执行了以下命令:

./android create avd -f \
    -n "${AVD_NAME:-nexus4_emulator}" \
    -t "${AVD_TARGET:-Google Inc.:Google APIs:21}" \
    -b "${AVD_ABI:-armeabi-v7a}" \
    -g "${AVD_TAG:-google_apis}" \
    -s "${AVD_SKIN:-nexus_4}" \
    -d "${AVD_DEVICE:-Nexus 4}" \
    -c "${AVD_SDCARD:-128M}"

./emulator -avd nexus4_emulator &

hardware.ini内容:

# skin-specific values
AvdId=nexus4_emulator
avd.ini.displayname=nexus4 emulator
disk.dataPartition.size=200M
hw.camera.back=none
hw.camera.front=none
hw.gpu.enabled=yes
hw.lcd.density=320
hw.ramSize=1024
runtime.network.latency=none
runtime.network.speed=full
runtime.scalefactor=auto
skin.dynamic=yes
snapshot.present=no
vm.heapSize=64

这只是一个实验,我不推荐它,但它似乎工作:

注意:我发现另一个关于 avd 名称的问题,在某个时候被替换,我会使用 Nexus4_emulator

CI 配置


这并不能解决 Travis-ci 案例。您需要复制nexus_4 皮肤或create your custom skin 并将其放置在您的项目或可访问的位置。 (更新:我试过了,速度太慢了……)

但您可能不需要完全一致的 Nexus 4 皮肤,最好使用 -no-window-no-audio 等模拟器选项来加快速度。

我通常使用-no-skin 选项,但我不知道hardware.ini 关系,所以我可能会自定义我的模拟器,我需要进一步研究它。我发现在本地使用-no-skin选项时,屏幕大小不正确。请参阅下面的更新。

另一个选项是在启动模拟器时使用--skin 768x1280 -dynamic-skin。 (更新:我在创建 AVD 时使用了这个但被忽略了) 更多信息here

来自here 的替代自定义密度

作为调整模拟器皮肤配置的替代方法,您 可以使用模拟器皮肤的默认密度并添加-dpi-device 启动 AVD 时模拟器命令行的选项。为了 示例:

emulator -avd WVGA800 -scale 96dpi -dpi-device 160

更新: tasomaniac 评论了here

-no-skin 参数移除皮肤。这意味着模拟器将是 320x480 像素宽 x 高和 320 密度,这与 大多数测试。默认皮肤是 480x800,这对大多数人来说都很好 的案例。

--skin WXGA720 可以像下面这样使用,以使测试更可信。这是默认找到的最好的模拟器配置。

- echo no | android create avd --force -n test -t android-19 --abi armeabi-v7a --skin WXGA720

320x480 设备,密度为 320,适用于 Android 意味着该设备是 实际上是 160x240。有了这个,屏幕上没有任何东西。非 UI 相关 模拟器中的测试通过,但与 UI 相关的 Espresso 测试将失败。

Circle CI 也默认只有 -no-audio -no-window

更新:下一个配置解决了点击查看的问题here

android.support.test.espresso.PerformException:执行错误 '单击'视图'动画或过渡在视图上启用 目标设备。

  - echo no | android create avd --force -n test -t $ANDROID_TARGET --abi $ANDROID_ABI --device 'Nexus 4' --sdcard 128M
  - emulator -avd test -no-window -skin 768x1280 &

【讨论】:

  • 感谢您在此处添加更多详细信息。这个答案真的很详细,很好。
  • 同意,很有帮助
【解决方案2】:

所以这在 2017 年末仍未解决(嘘 Google!)。

我发现正在工作的最小更改是将skin.name=1080x1920 或任何其他所需的分辨率添加到avd 的config.ini (~/.android/avd/{avdName}.avd/config.ini)

【讨论】:

  • 这立即为我解决了 - 比上面的解决方案简单得多。
  • 谢谢,一旦我添加了skin.name=1080x1920hw.lcd.density=480,这就会起作用
【解决方案3】:

这是我的 Travis 配置的相关位,以拥有 480x800 hdpi 模拟器:

language: android
jdk:
  - oraclejdk7

env:
  global:
    # API level and build tools version to build the app. Must stay in sync with the Gradle config
    - ANDROID_API_LEVEL=23
    - ANDROID_BUILD_TOOLS_VERSION=22.0.1

    # ARM processor. If it doesn't load in your Mac computer, use google_apis/x86 instead
    - ANDROID_ABI=google_apis/armeabi-v7a

    # From our tests, the most stable emulator at the moment is API 21
    - EMULATOR_VERSION=21

android:
  components:
    # API level and build tools
    - android-$ANDROID_API_LEVEL
    - build-tools-$ANDROID_BUILD_TOOLS_VERSION

    # System images for the emulator. See http://docs.travis-ci.com/user/languages/android/
    - addon-google_apis-google-$EMULATOR_VERSION
    - sys-img-armeabi-v7a-addon-google_apis-google-$EMULATOR_VERSION

before_script:
  # Create an Android Virtual Device
  - android list device
  - echo no | android create avd --force -n wvga800-emulator -t "Google Inc.:Google APIs:"$EMULATOR_VERSION --abi $ANDROID_ABI -s WVGA800 -c 128M

  # Start the emulator (with no GUI: note the "-no-window")
  - emulator -avd wvga800-emulator -no-window &

【讨论】:

    猜你喜欢
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    相关资源
    最近更新 更多