in tools/android/emulator/emulated_device.py [0:0]
def _MakeAvd(self):
"""Crafts a set of ini files to correspond to an avd for this device.
AVD is the only way to pass certain properties on to the emulated device,
most notably dpi-device and vm heapsize (both of which are ignored from the
command line). Unfortunately there are options which are only controllable
from commandline (instead of avd) so we get to configure things thru both
interfaces. One day I hope the configuration style will all be unified into
one rational method which is effective both thru ADT/eclipse and
programatically. (As you're about to see, programmatically creating AVDs is
a bit of a trip!).
Returns:
an appropriate avd_name to pass to the emulator.
"""
# When using AVDs, the emulator expects to find AVDs beneath
#
# $ANDROID_SDK_HOME/.android/avd/[avd_name].
# if unset, this defaults to $HOME or /tmp
# both of these are undesired in our case.
#
# Also when using AVDs the emulator wants to find $ANDROID_SDK_ROOT
# and expects skin info to be stored beneath that location. We will
# in the future need to support skins.
avd_files = self._TempDir('avd_files')
android_tmp_dir = os.path.join(avd_files, 'tmp')
home_dir = os.path.join(avd_files, 'home')
os.makedirs(android_tmp_dir)
os.makedirs(home_dir)
# New version of emulator check for these directories.
os.makedirs(os.path.join(self._images_dir, 'platforms'))
os.makedirs(os.path.join(self._images_dir, 'platform-tools'))
self._emulator_env['ANDROID_HOME'] = self._images_dir
self._emulator_env['ANDROID_SDK_ROOT'] = self._images_dir
self._emulator_env['ANDROID_SDK_HOME'] = home_dir
self._emulator_env['HOME'] = home_dir
self._emulator_env['ANDROID_TMP'] = android_tmp_dir
self._console_auth_token_file = os.path.join(home_dir,
'.emulator_console_auth_token')
if not self._enable_console_auth:
# Write an empty file to disable console auth.
with open(self._console_auth_token_file, 'w+') as f:
f.write('')
dot_android_dir = os.path.join(home_dir, '.android')
os.makedirs(dot_android_dir)
ddms_cfg_file = os.path.join(dot_android_dir, 'ddms.cfg')
with open(ddms_cfg_file, 'w+') as ddms_cfg:
# suppress the 'welcome to android' dialog
ddms_cfg.write('pingOptIn=false\n')
ddms_cfg.write('pingTime.emulator=1348614108574\n')
ddms_cfg.write('pingId=592273184351987827\n')
dot_config_dir = os.path.join(home_dir, '.config',
'Android Open Source Project')
os.makedirs(dot_config_dir)
emulator_cfg_file = os.path.join(dot_config_dir, 'Emulator.conf')
with open(emulator_cfg_file, 'w+') as emulator_cfg:
# suppress some dialogs
emulator_cfg.write('[General]\n')
emulator_cfg.write('showAdbWarning=false\n')
emulator_cfg.write('showAvdArchWarning=false\n')
avd_dir = os.path.join(home_dir, '.android', 'avd')
# Allowed chars are:
# ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-
avd_name = 'mobile_ninjas.adb.%s' % self.emulator_adb_port
content_dir = os.path.join(avd_dir, avd_name)
os.makedirs(content_dir)
root_config_file = os.path.join(avd_dir, '%s.ini' % avd_name)
with open(root_config_file, 'w+') as root_config:
root_config.write('path=%s\n' % self._SessionImagesDir())
root_config.write('target=android-%s\n' % self._metadata_pb.api_name)
user_cfg_file = os.path.join(self._SessionImagesDir(), 'emulator-user.ini')
with open(user_cfg_file, 'w+') as user_cfg:
# Always put emulator window in fixed position.
user_cfg.write('window.x = 0\n')
user_cfg.write('window.y = 0\n')
config_ini_file = os.path.join(self._SessionImagesDir(), 'config.ini')
with open(config_ini_file, 'w+') as config_ini:
wrote_cores = False
for prop in self._metadata_pb.avd_config_property:
config_ini.write('%s=%s\n' % (prop.name, prop.value))
wrote_cores |= prop.name == _CORES_PROP
# the default size is ~256 megs, which fills up fast on iterative
# development.
if 'ext4' in subprocess.check_output(['file', self._UserdataQemuFile()]):
# getting this size right is pretty crucial - if it doesnt match
# the underlying file the guest os will get confused.
config_ini.write('disk.dataPartition.size=%s\n' %
os.path.getsize(self._UserdataQemuFile()))
else:
config_ini.write('disk.dataPartition.size=2047m\n')
# system partition must be less than 2GB (there's a constraint check in
# qemu). Also we must set the commandline flag too - which sets both
# userdata and system sizes, so everything is set to 2047 for sanity.
if 'ext4' in subprocess.check_output(['file', self._SystemFile()]):
# getting this size right is pretty crucial - if it doesnt match
# the underlying file the guest os will get confused.
config_ini.write('disk.systemPartition.size=%s\n' %
os.path.getsize(self._SystemFile()))
else:
config_ini.write('disk.systemPartition.size=2047m\n')
# a link back to our original name, not sure if this is needed, but lets
# be consistant.
config_ini.write('avd.name=%s\n' % avd_name)
config_ini.write('image.sysdir.1=%s\n' % 'session')
config_ini.write('image.sysdir.2=%s\n' % 'init')
# if we do not set this - android just creates cache by itself.
config_ini.write('disk.cachePartition=1\n')
config_ini.write('disk.cachePartition.path=cache.img\n')
cache_size = '66m'
if 'ext4' in subprocess.check_output(['file', self._CacheFile()]):
cache_size = os.path.getsize(self._CacheFile())
# getting this size right is pretty crucial - if it doesnt match
# the underlying file the guest os will get confused.
config_ini.write('disk.cachePartition.size=%s\n' % cache_size)
if self._metadata_pb.with_adbd_pipe:
config_ini.write('adbd.over.pipe=1\n')
# this really could be determined by the emulator-arm vs emulator-x86
# binary itself, and when we run without any AVD at all, it does. However
# once we start specifying avds, we need to re-specify this info in the
# avd, otherwise the emulator will balk.
config_ini.write(
'abi.type=%s\n' % self._metadata_pb.emulator_architecture)
avd_cpu_arch = self._metadata_pb.emulator_architecture
if avd_cpu_arch.startswith('arm'):
avd_cpu_arch = 'arm'
# else hopefully source properties matches. sigh!
config_ini.write('hw.cpu.arch=%s\n' % avd_cpu_arch)
# allow the user to override from the launch command any core values
# the system image may set by default.
if FLAGS['cores'].present or not wrote_cores:
config_ini.write('%s=%d\n' % (_CORES_PROP, FLAGS.cores))
config_ini.write('hw.gpu.enabled=yes\n')
if not FLAGS.hardware_keyboard:
config_ini.write('hw.keyboard=no\n')
# and there are race conditions if skin and the hw ini values do not agree
# with each other.
skin = self._metadata_pb.skin
height = skin[skin.index('x') + 1:]
width = skin[:skin.index('x')]
config_ini.write('hw.lcd.width=%s\n' % width)
config_ini.write('hw.lcd.height=%s\n' % height)
# there are other avd pieces we omit, because they're overridden by the
# flags we pass in from the commandline.
return avd_name