Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an emulator with a decent size from command line

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):

Emulator with tiny screen

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).

like image 941
espinchi Avatar asked Oct 02 '15 19:10

espinchi


People also ask

How do I run an emulator from command prompt?

Starting the emulator Use the emulator command to start the emulator, as an alternative to running your project or starting it through the AVD Manager. Here's the basic command-line syntax for starting a virtual device from a terminal prompt: emulator -avd avd_name [ {- option [ value ]} … ]

How do I increase my emulator memory?

Go to Tools->Android->AVD Manager , there's something like pencil to edit your AVD click on that, then in the pop-up window click Show Advanced Settings and there you can change the RAM size.


3 Answers

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

enter image description here

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:

enter image description here

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:

enter image description here

enter image description here

enter image description here

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:

enter image description here

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

enter image description here

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:

enter image description 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 &
like image 60
albodelu Avatar answered Sep 30 '22 21:09

albodelu


So this is still not solved in late 2017 (Booo Google!).

The minimal change I found to be working is adding skin.name=1080x1920 or any other desired resolution to the avd's config.ini (~/.android/avd/{avdName}.avd/config.ini)

like image 29
Rotemmiz Avatar answered Sep 30 '22 23:09

Rotemmiz


Here's the relevant bits of my Travis config to have a 480x800 hdpi emulator:

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 &
like image 21
espinchi Avatar answered Sep 30 '22 21:09

espinchi