Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the emulator pixel density from the command line?

(Using android sdk 25.1.6)

I am creating and opening Android emulator from the command line:

$> android create avd -n my_device -t 9 --skin 1440x2560 --abi default/x86

How can I set its pixel density?

I have tried the following approaches:

  1. After running the above command I answer "yes" to creating a hardware profile. When I'm asked what density to use I type 570 but it does not accept it (it asks me the same question again) and only accepts empty input (default).

  2. After creating the emulator, open ~/.android/avd/my_avd.ini and append:

    hw.lcd.density=570
    

    but this setting is ignored.

  3. add this flag when opening the emulator:

    $> emulator avd my_avd -dpi-device 570
    

    but I get:

    WARNING: The -dpi-device flag is obsolete and will be ignored
    

Any idea?

like image 564
Yaron Naveh Avatar asked May 14 '16 23:05

Yaron Naveh


1 Answers

Here's a full solution that sets the pixel density as well as width and height. This matches the emulated device created through android studio/avd:

# Install AVD files
$ANDROID_HOME/tools/bin/sdkmanager --install 'system-images;android-29;default;x86'
yes | $ANDROID_HOME/tools/bin/sdkmanager --licenses

# Create emulator
echo "no" | $ANDROID_HOME/tools/bin/avdmanager create avd -n Pixel_API_29_AOSP -d pixel --package 'system-images;android-29;default;x86' --force

$ANDROID_HOME/emulator/emulator -list-avds

# Set screen dimensions
echo "hw.lcd.density=420" >> ~/.android/avd/Pixel_API_29_AOSP.avd/config.ini
echo "hw.lcd.height=1920" >> ~/.android/avd/Pixel_API_29_AOSP.avd/config.ini
echo "hw.lcd.width=1080" >> ~/.android/avd/Pixel_API_29_AOSP.avd/config.ini

echo "Starting emulator and waiting for boot to complete..."
nohup $ANDROID_HOME/emulator/emulator -avd Pixel_API_29_AOSP -no-snapshot -no-window -no-audio -no-boot-anim -camera-back none -camera-front none -qemu -m 2048 > /dev/null 2>&1 &
$ANDROID_HOME/platform-tools/adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed | tr -d '\r') ]]; do sleep 1; done; input keyevent 82'

echo "Emulator has finished booting"
$ANDROID_HOME/platform-tools/adb devices
like image 154
badsyntax Avatar answered Sep 20 '22 18:09

badsyntax