Merge "Find goldfish images in subdirectory in SDK repo"
diff --git a/create/goldfish_local_image_local_instance.py b/create/goldfish_local_image_local_instance.py
index c066b3c..4141bd8 100644
--- a/create/goldfish_local_image_local_instance.py
+++ b/create/goldfish_local_image_local_instance.py
@@ -238,13 +238,7 @@
         emulator_path = self._FindEmulatorBinary(avd_spec.local_tool_dirs)
         emulator_path = os.path.abspath(emulator_path)
 
-        image_dir = os.path.abspath(avd_spec.local_image_dir)
-
-        if not (os.path.isfile(os.path.join(image_dir, _SYSTEM_IMAGE_NAME)) or
-                os.path.isfile(os.path.join(image_dir,
-                                            _SYSTEM_QEMU_IMAGE_NAME))):
-            raise errors.GetLocalImageError("No system image in %s." %
-                                            image_dir)
+        image_dir = self._FindImageDir(avd_spec.local_image_dir)
 
         # TODO(b/141898893): In Android build environment, emulator gets build
         # information from $ANDROID_PRODUCT_OUT/system/build.prop.
@@ -343,6 +337,35 @@
         raise errors.GetSdkRepoPackageError(_MISSING_EMULATOR_MSG)
 
     @staticmethod
+    def _FindImageDir(image_dir):
+        """Find emulator images in the directory.
+
+        In build environment, the images are in $ANDROID_PRODUCT_OUT.
+        In an extracted SDK repository, the images are in the subdirectory
+        named after the CPU architecture.
+
+        Args:
+            image_dir: The path given by the environment variable or the user.
+
+        Returns:
+            The directory containing the emulator images.
+
+        Raises:
+            errors.GetLocalImageError if the images are not found.
+        """
+        entries = os.listdir(image_dir)
+        if len(entries) == 1:
+            first_entry = os.path.join(image_dir, entries[0])
+            if os.path.isdir(first_entry):
+                image_dir = first_entry
+
+        if (os.path.isfile(os.path.join(image_dir, _SYSTEM_QEMU_IMAGE_NAME)) or
+                os.path.isfile(os.path.join(image_dir, _SYSTEM_IMAGE_NAME))):
+            return image_dir
+
+        raise errors.GetLocalImageError("No system image in %s." % image_dir)
+
+    @staticmethod
     def _IsEmulatorRunning(adb):
         """Check existence of an emulator by sending an empty command.
 
diff --git a/create/goldfish_local_image_local_instance_test.py b/create/goldfish_local_image_local_instance_test.py
index 54b5944..98690a1 100644
--- a/create/goldfish_local_image_local_instance_test.py
+++ b/create/goldfish_local_image_local_instance_test.py
@@ -180,8 +180,10 @@
         """Test _CreateAVD with SDK repository files."""
         self._SetUpMocks(mock_popen, mock_utils, mock_instance)
 
-        self._CreateEmptyFile(os.path.join(self._image_dir, "system.img"))
-        self._CreateEmptyFile(os.path.join(self._image_dir, "build.prop"))
+        self._CreateEmptyFile(os.path.join(self._image_dir, "x86",
+                                           "system.img"))
+        self._CreateEmptyFile(os.path.join(self._image_dir, "x86",
+                                           "build.prop"))
 
         mock_avd_spec = mock.Mock(flavor="phone",
                                   boot_timeout_secs=None,
@@ -214,7 +216,7 @@
         self._mock_proc.poll.assert_called()
 
         self.assertTrue(os.path.isfile(
-            os.path.join(self._image_dir, "system", "build.prop")))
+            os.path.join(self._image_dir, "x86", "system", "build.prop")))
 
     # pylint: disable=protected-access
     @mock.patch("acloud.create.goldfish_local_image_local_instance.instance."