android-test icon indicating copy to clipboard operation
android-test copied to clipboard

Allow to provide own emulator images

Open menny opened this issue 4 years ago • 3 comments

We store our images in a different location than androidsdk. They are downloaded and make available using a http_archive

Overview

images are hard-coded in the repository at //tools/android/emulated_devices:macro/image_info.bzl as API_TO_IMAGE. This is limiting users of the repo when wanting to use a newer API (currently API_TO_IMAGE goes up to API 28). An additional limitation is that API_TO_IMAGE is pointing directly to @androidsdk, while this could be okay, we do not want our developers to maintain their SDK installation and we serve the SDK and the images as http_archive repository-rules.

Proposed Changes

Allow accepting api_to_images as an argument in make_device. Defaulting to API_TO_IMAGE.

menny avatar Mar 25 '21 18:03 menny

This allows us to do something like this macro:


def get_android_device(
        api_level = 26,
        arch = "x86_64",
        type = "default",
        boot_properties = {
            "ro.product.brand": "google",
            "ro.product.device": "crespo",
            "ro.product.manufacturer": "samsung",
            "ro.product.model": "Nexus S",
            "ro.product.name": "soju",
        },
        avd_properties = {
            "hw.mainKeys": "no",
        },
        horizontal_resolution = 480,
        ram = 1500,
        screen_density = 233,
        vertical_resolution = 800,
        vm_heap = 256,
        cache = 32):
    device_target_name = "{}_{}_{}".format(type, api_level, arch)
    if native.existing_rule(device_target_name) == None:
        image = new_image(
            api_level = api_level,
            flavor = type,
            files = "@" + _emulator_image_name(api_level, arch, type) + "//:emulator_image",
            arch = arch,
        )
        api_to_images = {api_level: [image]}
        make_device(
            name = device_target_name,  #note: this is not really used, the target name is calculated internally in make_device stack.
            horizontal_resolution = horizontal_resolution,
            vertical_resolution = vertical_resolution,
            screen_density = screen_density,
            ram = ram,
            vm_heap = vm_heap,
            cache = cache,
            min_api = api_level,
            max_api = api_level,
            system_image_flavors = [type],
            boot_properties = dict(boot_properties),  # unfreeze
            avd_properties = avd_properties,
            emulator_types = ["qemu2"],
            default_emulator_type = "qemu2",
            archs_override = ["armeabi-v7a", "arm64-v8a", "x86", "x86_64"],
            api_to_images = api_to_images,
        )
    return ":" + device_target_name

"@" + _emulator_image_name(api_level, arch, type) + "//:emulator_image" returns the label to an external repository target that contains the image files.

And then returns the calculated target device_target_name = "{}_{}_{}".format(type, api_level, arch)

menny avatar Mar 25 '21 21:03 menny

and then in the test rule:

android_instrumentation_test(
    name = "example_app_test",
    target_device = get_android_device(),
    test_app = ":example_app_test_binary",
)

menny avatar Mar 25 '21 22:03 menny

cc @vsethia - what's the guidance now for changes to the unified launcher now that it's no longer used internally? Will/can changes be accepted here?

jin avatar Apr 07 '21 03:04 jin