shuiyu1123 39b1da37c9 feat: 全面完善API — 补全核心类方法、存根转真实实现、Exception体系扩展
核心类方法补全:
- Activity: +20方法 (requestWindowFeature, setTitle/getTitle, onKeyDown/onKeyUp, enterPictureInPictureMode, getReferrer, isTaskRoot, navigateUpTo等)
- Fragment: +15方法 (getChildFragmentManager, getParentFragment, requireContext/requireActivity, setMenuVisibility等)
- Intent: +20方法 (getParcelableExtra, setComponent/getComponent, getDataString, getScheme, putExtras等)
- View: +20方法 (setLabelFor, setRotationX/Y, setForeground, setBackgroundTintList等)
- KeyEvent: +7方法 (getCharacters, isSystem, isPrintingKey, isModifierKey, startTracking等)
- Paint: +12方法 (setStrokeCap/Join, getTextBounds, setBlendMode, setLetterSpacing等)
- Notification.Builder: +10方法 (setProgress, setShowWhen, setOnlyAlertOnce, setSound等)

存根转真实实现:
- TrafficStats: 读取 /proc/net/dev 返回真实网络流量
- RecyclerView.Adapter: 观察者模式触发通知
- BaseAdapter: 观察者模式触发通知
- ExifInterface: 使用PIL实现图片翻转/旋转
- Process: 使用os.kill实现进程信号
- FileUtils: 真实文件I/O
- SharedPreferences: 监听器模式
- TelephonyManager.listen: 维护监听器列表
- ScrollView.smoothScrollTo: 更新滚动位置

Exception体系扩展:
- 新增15个Exception/Error类 (ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchElementException, AssertionError, OutOfMemoryError, StackOverflowError, NoClassDefFoundError, UnsatisfiedLinkError, LinkageError, VerifyError, AbstractMethodError, IncompatibleClassChangeError, ExceptionInInitializerError)

Math/System/Thread/StringBuilder/Integer增强:
- Math: +20方法 (subtractExact, multiplyExact, toIntExact, floorDiv/floorMod, fma等)
- System: getProperties/setProperties/mapLibraryName/getLogger
- Thread: +10方法 (getUncaughtExceptionHandler, getAllStackTraces, getStackTrace, holdLock等)
- StringBuilder: +7方法 (ensureCapacity, trimToSize, setLength, subSequence等)
- Integer: +8静态方法 (compare, compareUnsigned, divideUnsigned, sum, max, min等)

修复:
- app_fragment.py: 使用java.lang.IllegalStateException替代本地定义
- content_intent.py: Intent.setData/getScheme正确从Uri提取scheme
- view_ext.py: KeyEvent继承view.KeyEvent获取所有KEYCODE常量
- __init__.py: 补全Fragment/KeyEvent/MotionEvent/TrafficStats/RecyclerView等导出

统计: 221模块, 3660类, 30826方法, 180217行
2026-06-24 23:46:36 +00:00
2026-06-24 11:16:26 +00:00

Android-pyApi

Android API for Python — 在桌面/Linux 上原生运行 Android API 的 Python 库

License: MIT Python 3.8+ Platform

这是什么

Android-pyApi 将完整的 Android SDK API 翻译为纯 Python 实现,让你在桌面环境直接调用 Android API——不需要模拟器、不需要真机、不需要 Android Studio。

213 个模块,178,000+ 行代码,覆盖 android/java/javax/org/dalvik 全包。

在 Linux 上,系统级 API(网络、传感器、蓝牙、音频、位置等)使用真实系统调用,返回真实数据。

核心特性

API 覆盖

包 模块数 内容
android.* 167 app, content, os, view, widget, graphics, media, hardware, net, telephony, provider 等
java.* 35 lang, io, util, net, nio, security, crypto, sql, math, time 等
javax.* 4 net, crypto, security, sql
org.* 5 json, w3c_dom, xml_sax, xml_pull, apache_http
dalvik.* 1 system

Linux 真实实现

在 Linux 环境下,以下 API 使用真实系统调用而非存根:

API 实现方式 真实行为
ConnectivityManager ip 命令 + /proc/net 真实 IP、接口、DNS、网关
LocationManager IP 地理定位 真实经纬度、城市、ISP
SensorManager /sys/class/hwmon 真实温度、CPU 频率、风扇
AudioManager amixer (ALSA) 真实音量控制
BluetoothAdapter bluetoothctl (BlueZ) 真实扫描、配对、连接
ClipboardManager xclip 真实剪贴板读写
PowerManager / BatteryManager psutil + /sys/class/power_supply 真实 CPU、内存、电池
TelephonyManager MAC 地址 + 内核版本 真实设备 ID
NotificationManager notify-send 真实桌面通知
Activity / Display xdotool + X11 真实屏幕尺寸、截屏
ActivityManager psutil + /proc 真实进程列表
Shell subprocess 真实命令执行
Camera OpenCV / V4L2 真实拍照
UI 自动化 xdotool 真实点击、输入、按键

跨平台

  • Linux — 系统级 API 使用真实实现
  • macOS / Windows — 系统级 API 使用存根(后续支持)
  • API 形状 — 所有平台一致,方法签名完全对齐 Android SDK

安装

# 从源码安装
git clone https://gitee.com/shuiyu1123/Android-pyApi.git
cd Android-pyApi
pip install -e .

# 或直接使用
export PYTHONPATH=/path/to/Android-pyApi:$PYTHONPATH

可选依赖

# Linux 系统工具
sudo apt install xclip xdotool libnotify-bin alsa-utils bluez lm-sensors

# Python 库
pip install psutil requests Pillow opencv-python numpy

快速开始

from android_pyapi import Intent, Uri, Bundle, File, JSONObject, ArrayList, HashMap

# Intent — Android 最核心的 API
intent = Intent("android.intent.action.VIEW")
intent.setData(Uri.parse("https://example.com"))
intent.putExtra("key", "value")
print(intent.getAction())        # android.intent.action.VIEW
print(intent.getData())          # https://example.com
print(intent.getStringExtra("key"))  # value

# java.util 集合
lst = ArrayList()
lst.add("hello")
lst.add("world")
print(lst.size())                # 2
print(lst.get(0))                # hello

# JSONObject
json = JSONObject()
json.put("name", "test")
json.put("version", 42)
print(json.toString())           # {"name":"test","version":42}

# Linux 真实实现
from android_pyapi import Build, LocationManager, ConnectivityManager
print(Build.MODEL)               # 真实主机名
print(Build.MANUFACTURER)        # Linux

loc = LocationManager().getLastKnownLocation("network")
print(loc)                       # 真实经纬度

项目结构

android_pyapi/
├── _core.py              # Stub 基类、全局上下文
├── _linux_impl.py        # Linux 真实实现层 (14个实现类)
├── __init__.py           # 统一导出
├── android/              # 167 个 Android 模块
│   ├── app.py            # Activity, Service, Fragment, Notification
│   ├── content.py        # Context, Intent, ContentResolver, ClipboardManager
│   ├── os.py             # Handler, Looper, Bundle, Build, PowerManager
│   ├── view.py           # View, ViewGroup, MotionEvent, KeyEvent
│   ├── widget.py         # TextView, Button, ListView, RecyclerView, Toast
│   ├── graphics.py       # Bitmap, Canvas, Paint, Color, Path, Matrix
│   ├── hardware.py       # SensorManager, Camera, BiometricPrompt
│   ├── net.py            # ConnectivityManager, NetworkInfo
│   ├── telephony.py      # TelephonyManager, SmsManager
│   ├── location.py       # LocationManager, Location, Geocoder
│   ├── media.py          # AudioManager, MediaPlayer, MediaRecorder
│   └── ...
├── java/                 # 35 个 Java 标准库模块
│   ├── lang.py           # String, Math, System, Thread, Exception 体系
│   ├── io.py             # File, InputStream, OutputStream, Reader, Writer
│   ├── util.py           # ArrayList, HashMap, Calendar, Collections, UUID
│   ├── net.py            # URL, HttpURLConnection, Socket
│   └── ...
├── javax/                # 4 个 javax 模块
├── org/                  # 5 个 org 模块
└── dalvik/               # 1 个 dalvik 模块

设计理念

不是模拟器

Android-pyApi 不是 Android 模拟器。它不包含 ART 虚拟机、SurfaceFlinger、binder 等底层基础设施。

它是 API 兼容层:

  • API 形状 — 类名、方法签名、常量值完全对齐 Android SDK
  • 系统级行为 — 在 Linux 上用真实系统调用实现
  • 框架级行为 — Activity 生命周期、View 渲染等保持存根(桌面无对应框架)

渐进式真实化

存根 (所有平台) → Linux 真实实现 → macOS 真实实现 → Windows 真实实现

每个 API 从"返回硬编码值"逐步演进为"调用真实系统功能"。

使用场景

  • Android 脚本开发 — 在桌面编写和调试依赖 Android API 的 Python 脚本
  • 跨平台工具 — 用 Android API 风格编写跨平台系统工具
  • 学习 Android API — 不需要 Android Studio 就能探索 API
  • 自动化测试 — 在 CI/CD 中测试 Android API 调用逻辑
  • IoT / 嵌入式 — 在 Linux 设备上用 Android API 风格操作硬件

贡献

欢迎贡献!特别需要:

  • macOS 真实实现层 (_macos_impl.py)
  • Windows 真实实现层 (_windows_impl.py)
  • 更多 API 的真实行为实现
  • 测试用例

License

MIT

S
Description
No description provided
Readme
1.9 MiB
0 Stars 1 Watchers 0 Forks