Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files
examples/tools/android/README.md
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
## Android Environment Setup Guide
|
2 |
+
|
3 |
+
This guide will help you set up a local Android environment for AgentWorld.
|
4 |
+
|
5 |
+
### Installation Steps
|
6 |
+
|
7 |
+
1. **Download and Install Android Studio**
|
8 |
+
- Visit [https://developer.android.com/studio](https://developer.android.com/studio)
|
9 |
+
- Download and install the latest version for your operating system
|
10 |
+
|
11 |
+
2. **Install ADB and Android Emulator**
|
12 |
+
- Open Android Studio
|
13 |
+
- Click on the top menu: Tools → SDK Manager
|
14 |
+
<img src="../../../readme_assets/android_step1.png" width="70%" alt="SDK Manager">
|
15 |
+
<!-- {:style="width:200px; height:auto;"} -->
|
16 |
+
- Check the following components:
|
17 |
+
- Android SDK Build-Tools
|
18 |
+
- Android SDK Command-line Tools
|
19 |
+
- Android Emulator
|
20 |
+
- Android SDK Platform-Tools
|
21 |
+
- Click "Apply" to install these components
|
22 |
+
<img src="../../../readme_assets/android_step2.png" width="70%" alt="Check components">
|
23 |
+
- **Important**: Copy the installation directory path (you'll need it later for configuration)
|
24 |
+
|
25 |
+
3. **Create a Virtual Device**
|
26 |
+
- From the main menu, select: View → Tool Windows → Device Manager
|
27 |
+
<img src="../../../readme_assets/android_step3.png" width="70%" alt="Device Manager">
|
28 |
+
- Click the "+" button, then "Create Virtual Device"
|
29 |
+
<img src="../../../readme_assets/android_step4.png" width="70%" alt="button">
|
30 |
+
- Select a device (e.g., Medium Phone), then click "Next"
|
31 |
+
<img src="../../../readme_assets/android_step5.png" width="70%" alt="next">
|
32 |
+
- Select a image (e.g., VanillalceCream), then click "Next"
|
33 |
+
<img src="../../../readme_assets/android_step6.png" width="70%" alt="next">
|
34 |
+
- Configure device settings as needed, then click "Finish"
|
35 |
+
- **Important**: Note down the AVD ID (device name) for later use
|
36 |
+
<img src="../../../readme_assets/android_step7.png" width="70%" alt="avd id">
|
37 |
+
|
38 |
+
4. **Configure in Your Code**
|
39 |
+
- Method 1: Default Acquisition of Emulator and ADB Installation Paths
|
40 |
+
- Only set the AVD_ID copied during the earlier installation process.
|
41 |
+
- Method 2: Manually Specify Emulator and ADB Installation Paths.Provide the following:
|
42 |
+
- AVD_ID: The name of the virtual device you created
|
43 |
+
- ADB path: Your SDK directory + "/platform-tools/adb"
|
44 |
+
- Emulator path: Your SDK directory + "/emulator/emulator"
|
45 |
+
### Example Code
|
46 |
+
#### Method 1
|
47 |
+
|
48 |
+
```python
|
49 |
+
from examples.tools.android.action.adb_controller import ADBController
|
50 |
+
|
51 |
+
# Initialize the Android controller
|
52 |
+
android_controller = ADBController(avd_name="Medium_Phone_API_35")
|
53 |
+
```
|
54 |
+
#### Method 2
|
55 |
+
|
56 |
+
```python
|
57 |
+
from examples.tools.android.action.adb_controller import ADBController
|
58 |
+
|
59 |
+
# Initialize the Android controller
|
60 |
+
android_controller = ADBController(
|
61 |
+
avd_name="Medium_Phone_API_35",
|
62 |
+
adb_path="/Users/username/Library/Android/sdk/platform-tools/adb",
|
63 |
+
emulator_path="/Users/username/Library/Android/sdk/emulator/emulator"
|
64 |
+
)
|
65 |
+
|
66 |
+
# Now you can use this controller with your agent
|
67 |
+
```
|
68 |
+
|
69 |
+
### Troubleshooting
|
70 |
+
|
71 |
+
- If the emulator fails to start, try increasing the memory allocation in the AVD settings
|
72 |
+
- Make sure your paths are correct for your operating system:
|
73 |
+
- Windows: Use backslashes or raw strings (r"C:\path\to\sdk")
|
74 |
+
- macOS/Linux: Use forward slashes as shown in the example
|
75 |
+
|
76 |
+
### Additional Resources
|
77 |
+
|
78 |
+
- [Android SDK Official Documentation](https://developer.android.com/studio/intro)
|
79 |
+
- [Android Emulator Documentation](https://developer.android.com/studio/run/emulator)
|
examples/tools/android/__init__.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
# coding: utf-8
|
2 |
+
# Copyright (c) 2025 inclusionAI.
|
examples/tools/android/android.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding: utf-8
|
2 |
+
# Copyright (c) 2025 inclusionAI.
|
3 |
+
|
4 |
+
import traceback
|
5 |
+
from typing import Any, Tuple, List, Dict
|
6 |
+
|
7 |
+
from examples.tools.tool_action import AndroidAction
|
8 |
+
from aworld.core.common import ActionModel, Observation, ActionResult
|
9 |
+
from aworld.logs.util import logger
|
10 |
+
from examples.tools.android.action.adb_controller import ADBController
|
11 |
+
from examples.tools.android.action.executor import AndroidToolActionExecutor
|
12 |
+
from examples.tools.conf import AndroidToolConfig
|
13 |
+
from aworld.core.tool.base import ToolFactory, Tool
|
14 |
+
from aworld.tools.utils import build_observation
|
15 |
+
|
16 |
+
ALL_UNICODE_CHARS = frozenset(chr(i) for i in range(0x10FFFF + 1))
|
17 |
+
|
18 |
+
|
19 |
+
@ToolFactory.register(name="android",
|
20 |
+
desc="android",
|
21 |
+
supported_action=AndroidAction,
|
22 |
+
conf_file_name=f'android_tool.yaml')
|
23 |
+
class AndroidTool(Tool):
|
24 |
+
|
25 |
+
def __init__(self, conf: AndroidToolConfig, **kwargs):
|
26 |
+
super(AndroidTool, self).__init__(conf, **kwargs)
|
27 |
+
self.controller = ADBController(avd_name=self.conf.get('avd_name'),
|
28 |
+
adb_path=self.conf.get('adb_path'),
|
29 |
+
emulator_path=self.conf.get('emulator_path'))
|
30 |
+
|
31 |
+
if self.conf.get("custom_executor"):
|
32 |
+
self.action_executor = AndroidToolActionExecutor(self.controller)
|
33 |
+
|
34 |
+
def reset(self, *, seed: int | None = None, options: Dict[str, str] | None = None) -> Tuple[
|
35 |
+
Observation, Dict[str, Any]]:
|
36 |
+
# self.controller.stop_emulator()
|
37 |
+
# self.controller.start_emulator()
|
38 |
+
self.controller.setup_connection()
|
39 |
+
logger.info("start emulator successfully...")
|
40 |
+
# snapshot screen and annotate
|
41 |
+
xml, pic_base64 = self.get_observation()
|
42 |
+
action_result_list = [ActionResult(content='start', keep=True)]
|
43 |
+
return build_observation(observer=self.name(),
|
44 |
+
ability='',
|
45 |
+
dom_tree=xml,
|
46 |
+
image=pic_base64,
|
47 |
+
action_result=action_result_list), {}
|
48 |
+
|
49 |
+
def do_step(self, action_list: List[ActionModel], **kwargs) -> Tuple[
|
50 |
+
Observation, float, bool, bool, Dict[str, Any]]:
|
51 |
+
|
52 |
+
exec_state = 0
|
53 |
+
fail_error = ""
|
54 |
+
action_result_list = None
|
55 |
+
try:
|
56 |
+
action_result_list = self.action_executor.execute_action(action_list, **kwargs)
|
57 |
+
exec_state = 1
|
58 |
+
except Exception as e:
|
59 |
+
traceback.print_exc()
|
60 |
+
fail_error = str(e)
|
61 |
+
|
62 |
+
terminated = kwargs.get("terminated", False)
|
63 |
+
if action_result_list:
|
64 |
+
for action_result in action_result_list:
|
65 |
+
if action_result.is_done:
|
66 |
+
terminated = action_result.is_done
|
67 |
+
self._finish = True
|
68 |
+
|
69 |
+
info = {"exception": fail_error}
|
70 |
+
info.update(kwargs)
|
71 |
+
xml, pic_base64 = self.get_observation()
|
72 |
+
|
73 |
+
return (build_observation(observer=self.name(),
|
74 |
+
ability=action_list[-1].action_name,
|
75 |
+
dom_tree=xml,
|
76 |
+
image=pic_base64,
|
77 |
+
action_result=action_result_list),
|
78 |
+
exec_state,
|
79 |
+
terminated,
|
80 |
+
kwargs.get("truncated", False),
|
81 |
+
info)
|
82 |
+
|
83 |
+
def close(self):
|
84 |
+
self.controller.stop_emulator()
|
85 |
+
|
86 |
+
def get_controller(self):
|
87 |
+
return self.controller
|
88 |
+
|
89 |
+
def get_observation(self) -> Observation:
|
90 |
+
return self.controller.screenshot_and_annotate()
|
examples/tools/android/requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
opencv-python~=4.11.0.86
|
2 |
+
pyshine~=0.0.9
|