Skip to content

Instantly share code, notes, and snippets.

@Jiayi-Pan
Last active December 29, 2023 05:10
Show Gist options
  • Save Jiayi-Pan/9cdf7ef7fc3f88a05d43e3a866bb250e to your computer and use it in GitHub Desktop.
Save Jiayi-Pan/9cdf7ef7fc3f88a05d43e3a866bb250e to your computer and use it in GitHub Desktop.
Clone/duplicate an existing Android AVD
def clone_avd(src_avd_name, tar_avd_name, android_avd_home):
"""
Clone the source AVD to the target AVD.
Parameters:
- src_avd_name: The name of the source AVD folder.
- tar_avd_name: The name of the target AVD folder.
- android_avd_home: The path to the .android/avd directory.
This function copies the source AVD folder and its .ini file to a new target AVD
and updates the paths inside the .ini files accordingly.
"""
# Paths for source and target AVD directories and .ini files
src_avd_dir = os.path.join(android_avd_home, src_avd_name + '.avd')
tar_avd_dir = os.path.join(android_avd_home, tar_avd_name + '.avd')
src_ini_file = os.path.join(android_avd_home, src_avd_name + '.ini')
tar_ini_file = os.path.join(android_avd_home, tar_avd_name + '.ini')
# Copy the AVD folder
shutil.copytree(src_avd_dir, tar_avd_dir)
# Copy the .ini file and modify it for the new AVD
with open(src_ini_file, 'r') as src_ini, open(tar_ini_file, 'w') as tar_ini:
for line in src_ini:
tar_ini.write(line.replace(src_avd_name, tar_avd_name))
# Update paths inside the target AVD's .ini files
for ini_name in ['config.ini', 'hardware-qemu.ini']:
ini_path = os.path.join(tar_avd_dir, ini_name)
if os.path.exists(ini_path):
with open(ini_path, 'r') as file:
lines = file.readlines()
with open(ini_path, 'w') as file:
for line in lines:
# Update paths and AVD name/ID
new_line = line.replace(src_avd_name, tar_avd_name)
file.write(new_line)
# Update the snapshots' hardware.ini file if it exists
snapshots_hw_ini = os.path.join(tar_avd_dir, 'snapshots', 'default_boot', 'hardware.ini')
if os.path.exists(snapshots_hw_ini):
with open(snapshots_hw_ini, 'r') as file:
lines = file.readlines()
with open(snapshots_hw_ini, 'w') as file:
for line in lines:
# Update AVD name/ID
new_line = line.replace(src_avd_name, tar_avd_name)
file.write(new_line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment