Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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</str


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...