Android Virtual Device Manager

Android Virtual Device Manager (AVD Manager) is a tool provided by Android Studio for managing virtual devices that can be used to test and debug Android applications. In this article, we will explore the features and capabilities of AVD Manager, and provide code examples to demonstrate its usage.

Introduction to AVD Manager

AVD Manager allows developers to create and manage virtual devices that emulate different Android device configurations. These virtual devices can be customized with various hardware profiles, system images, and emulator options.

Creating a Virtual Device

To create a new virtual device, open AVD Manager by clicking on the AVD Manager icon in the Android Studio toolbar or by navigating to Tools > AVD Manager. In the AVD Manager window, click on the Create Virtual Device button and follow the wizard to select the desired hardware profile and system image.

Once the virtual device is created, it will appear in the AVD Manager list. Selecting a virtual device and clicking on the Start button will launch the Android Emulator with the selected virtual device configuration.

Managing Virtual Devices

AVD Manager provides various options to manage virtual devices. You can edit the properties of a virtual device by selecting it from the list and clicking on the Edit button. This allows you to modify the device name, hardware profile, system image, and other emulator options.

The Delete button can be used to remove a virtual device from AVD Manager. However, be cautious as this action will permanently delete all data associated with the virtual device.

Command-Line Interface

AVD Manager can also be accessed from the command-line interface using the avdmanager command. This allows developers to automate the creation and management of virtual devices.

For example, the following command creates a new virtual device named "my_device" with a Nexus 5X hardware profile and Android 11 system image:

avdmanager create avd -n my_device -k "system-images;android-30;google_apis_playstore;x86"

Similarly, the following command starts the emulator with the specified virtual device:

emulator -avd my_device

Code Examples

Here are some code examples that demonstrate the usage of AVD Manager.

1. Creating a Virtual Device Programmatically

import com.android.sdklib.AndroidVersion;
import com.android.sdklib.ISystemImage;
import com.android.sdklib.devices.Device;
import com.android.sdklib.devices.DeviceManager;
import com.android.sdklib.internal.avd.AvdManager;

public class AVDManagerExample {
    public static void main(String[] args) {
        AvdManager avdManager = AvdManager.getInstance(AndroidSdkHandler.getInstance(null), System.out);
        DeviceManager deviceManager = DeviceManager.createInstance(null);

        Device device = deviceManager.getDevice("Nexus 5X");
        ISystemImage systemImage = avdManager.getLatestSystemImage(device);
        AndroidVersion androidVersion = systemImage.getAndroidVersion();

        avdManager.createAvd(
            "my_device",
            device,
            systemImage,
            "default",
            null,
            null,
            null,
            false,
            false,
            false,
            false,
            false,
            false,
            false,
            false,
            false,
            false,
            false,
            0,
            false,
            null,
            null,
            false,
            false
        );
    }
}

2. Starting the Emulator Programmatically

import com.android.sdklib.internal.avd.AvdInfo;
import com.android.tools.idea.avdmanager.AvdManagerConnection;
import com.android.tools.idea.run.AvdChooserDialog;
import com.android.tools.idea.sdk.AndroidSdks;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.android.sdk.AndroidSdkUtils;

public class EmulatorExample {
    public static void main(String[] args) {
        Project project = null; // Provide the current project

        ApplicationManager.getApplication().invokeLater(() -> {
            AvdChooserDialog dialog = new AvdChooserDialog(null, project);
            dialog.show();

            AvdInfo avdInfo = dialog.getChosenAvd();
            if (avdInfo != null) {
                if (AndroidSdkUtils.activateDdmsIfNecessary()) {
                    AvdManagerConnection.getDefaultAvdManagerConnection().startAvd(avdInfo);
                } else {
                    Messages.showErrorDialog(project, "Failed to start the emulator.", "Error");
                }
            }
        });
    }
}

Conclusion

AVD Manager is a powerful tool for creating and managing virtual devices for Android application development. It provides a convenient user interface as well as a command-line interface for automation. By utilizing AVD Manager, developers can easily test and debug their applications on different Android device configurations.