从命令行创建一个大小合适的模拟器

Create an emulator with a decent size from command line(从命令行创建一个大小合适的模拟器)

本文介绍了从命令行创建一个大小合适的模拟器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我创建 AVD 的方法:

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

现在我启动模拟器:

emulator -avd nexus4-emulator &

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

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

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

解决方案

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

<小时>

<块引用>

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

 <device id="nexus_4" name="Nexus 4"><方向名称="port" size="958,1678" screenPos="94,187" screenSize="768,1280" shadow="port_shadow.png" back="port_back.png" lights="port_fore.png"/><方位名称="土地"尺寸="1799,885" screenPos="257,45" screenSize="1280,768" shadow="land_shadow.png" back="land_back.png" lights="land_fore.png"/></设备>

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

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

# 皮肤特定的硬件值hw.lcd.密度=240vm.heapSize=48hw.ramSize=512

layout 文件包括下一行:

<代码>...设备 {展示 {宽度 480身高 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 内容:

# 皮肤特定值AvdId=nexus4_emulatoravd.ini.displayname=nexus4 模拟器disk.dataPartition.size=200Mhw.camera.back=无hw.camera.front=无hw.gpu.enabled=是hw.lcd.密度=320hw.ramSize=1024runtime.network.latency=noneruntime.network.speed=fullruntime.scalefactor=autoskin.dynamic=是快照.present=否vm.heapSize=64

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

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

CI 配置

<小时>

这并不能解决 Travis-ci 案例.您需要复制 nexus_4 皮肤或

来自这里的替代自定义密度

><块引用>

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

模拟器 -avd WVGA800 -scale 96dpi -dpi-device 160

更新: tasomaniac 评论 这里

<块引用>

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

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

- 回显不 |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

更新:下一个配置解决了点击查看这里

的问题<块引用>

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

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

I want to create an ARM API 21 emulator loaded with Google APIs.

Here's how I create the 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

And now I start up the emulator with:

emulator -avd nexus4-emulator &

However, this is what I get: a screen with tiny dimensions (in terms of effective pixels):

How can I change this config so that I have a properly sized screen?

I'm using this to build APKs and run tests on a machine in Travis, by the way, but it's reproducible in my machine (Mac OS X).

解决方案

Update: fix resolution using the new avdmanager


This is a good sample to create an emulator with a decent size using the new 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 &

There is an open issue as commented here about the unsupported -s "768x1280" parameter.


Previous answer


If you need a nexus 4 style device from commandline, you can try this:

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

Locally is better to use Android Studio.

The next configuration with device option for CI builds solves click issues related to screen size:

  - 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 &

Summary:


Local machine: the best option is to use Android Studio directly because the Nexus 4 skin seems placed only there and the device definition included in the SDK Tools seems not to be enough to create it; you need to define extra hardware options. I offer a work around below.

Travis-ci server: the best option is to speed up it disabling the audio and the window and to use a generic emulator. If you need a Nexus 4 style emulator you can try the same workaround, otherwise, remove the --device option and use commands like these:

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}" &

Note: on the left side are environment variables you can define in the .travis.yml, otherwise, the right side default values will be used. The last line prints useful and related debug information.

Local configuration


The Nexus 4 device definition is included in

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>

But the nexus_4 skin associated and used by android-studio is only here:

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>

You don't specify the --skin so the default skin WVGA800 included in the SDK tools is used:

This skin has associated hardware values in the hardware.ini file:

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

And the layout file includes the next lines:

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

This is not what you expect when you use the Nexus 4 device definition so I would use directly android-studio locally to create the avd and to use the correct skin.

If you use the command line like you did and try to open it from android-studio, you'll see:

Local machine solution

Use Android Studio for both; create and start the emulator.

If you create it using that command, adding a minimum of 200MB as internal storage, you'll solve the screen size issue because android-studio will update and fix the avd using the correct skin.

If you are looking for a command without android-studio intervention, I moved the Nexus 4 skin to the skins folder, added a hardware.ini with the missing properties to it and finally executed the next commands:

./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 content:

# 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

This is only an experiment, I don't recommend it, but it seems to work:

Note: I found another issue about the avd name, replaced at some point, I would use Nexus4_emulator.

CI configuration


This doesn't solve the Travis-ci case. You would need to copy the nexus_4 skin or create your custom skin and place it in your project or an accessible location. (Updated: I tried this, and it's so slow...)

But you probably don't need an exact Nexus 4 skin, and it's better to use the emulator options like -no-window, -no-audio etc. to speed up it.

I normally used the -no-skin option, but I didn't know the hardware.ini relation, so I'll probably customise my emulator, I need investigate it more. I found that when using the -no-skin option locally, the screen size is not correct. See update below.

Another option is to use --skin 768x1280 -dynamic-skin when starting the emulator. (updated: I used this when was creating the AVD but is ignored) Further information here:

Alternative custom density from here

As an alternative to adjusting the emulator skin configuration, you can use the emulator skin's default density and add the -dpi-device option to the emulator command line when starting the AVD. For example:

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

Update: tasomaniac commented here

-no-skin parameter removes the skin. This means that the emulator will be 320x480 pixel widthxheight and 320 density which messes up with most of the tests. The default skin is 480x800 which is fine for most of the cases.

--skin WXGA720 can be used like below to make tests more trusted. This is the best emulator config that is found by default.

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

320x480 device with 320 density for Android means that the device is actually 160x240. With this nothing fits in the screen. Non UI related tests in the emulator passes but UI related Espresso tests will fail.

Circle CI also defaults to only -no-audio -no-window

Update: the next configuration solved an issue clicking views here

android.support.test.espresso.PerformException: Error performing 'single click' on view 'Animations or transitions are enabled on the target device.

  - 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 &

这篇关于从命令行创建一个大小合适的模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从命令行创建一个大小合适的模拟器

基础教程推荐