蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行模拟器 ABI 兼容判定、ADB 路径统一、DPI 窗口缩放与外部存储路径规范化离线校验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-tutorial-genymotion-install.py
explore-tutorial-genymotion-install.py150 行
"""Offline deterministic verifier for Android CH07 (tutorial-genymotion-install)."""
from __future__ import annotations

import json


def install_apk_check(emulator_abis: set[str], apk_native_abis: set[str], arm_bridge: bool = False) -> str:
    if not apk_native_abis:
        return "INSTALL_OK_PURE_DEX"
    effective = set(emulator_abis)
    if arm_bridge:
        effective.update({"armeabi", "armeabi-v7a"})
    if effective & apk_native_abis:
        return "INSTALL_OK_NATIVE_MATCH"
    return "INSTALL_FAILED_CPU_ABI_INCOMPATIBLE"


def case_abi_compatibility() -> dict:
    x86_emu = {"x86", "x86_64"}
    arm_only_apk = {"armeabi-v7a"}
    universal_apk = {"armeabi-v7a", "x86"}
    pure_java_apk: set[str] = set()
    actual = {
        "arm_without_translation": install_apk_check(x86_emu, arm_only_apk, arm_bridge=False),
        "arm_with_translation": install_apk_check(x86_emu, arm_only_apk, arm_bridge=True),
        "universal_apk": install_apk_check(x86_emu, universal_apk, arm_bridge=False),
        "pure_dex_apk": install_apk_check(x86_emu, pure_java_apk, arm_bridge=False),
    }
    expected = {
        "arm_without_translation": "INSTALL_FAILED_CPU_ABI_INCOMPATIBLE",
        "arm_with_translation": "INSTALL_OK_NATIVE_MATCH",
        "universal_apk": "INSTALL_OK_NATIVE_MATCH",
        "pure_dex_apk": "INSTALL_OK_PURE_DEX",
    }
    return {
        "name": "emulator_architecture_and_abi_compatibility",
        "summary": "模拟 x86 虚拟设备安装 ARM-only APK 触发 ABI 不兼容及引入 ARM Translation 后的匹配判定",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_adb_sdk_binding() -> dict:
    adb_output = """List of devices attached
192.168.56.101:5555\tdevice
emulator-5554\tdevice
"""
    devices = []
    for line in adb_output.splitlines()[1:]:
        parts = line.split()
        if len(parts) == 2 and parts[1] == "device":
            devices.append(parts[0])
    sdk_cfg = {
        "ide_adb_path": r"C:\Android\Sdk\platform-tools\adb.exe",
        "genymotion_adb_mode": "custom_sdk",
        "genymotion_adb_path": r"C:\Android\Sdk\platform-tools\adb.exe",
    }
    actual = {
        "online_devices": devices,
        "adb_binary_unified": sdk_cfg["ide_adb_path"] == sdk_cfg["genymotion_adb_path"],
        "has_genymotion_tcp_serial": "192.168.56.101:5555" in devices,
    }
    expected = {
        "online_devices": ["192.168.56.101:5555", "emulator-5554"],
        "adb_binary_unified": True,
        "has_genymotion_tcp_serial": True,
    }
    return {
        "name": "adb_sdk_path_and_device_binding",
        "summary": "校验 Genymotion 绑定外部 SDK ADB 路径一致性及 adb devices 设备列表解析",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_display_scaling() -> dict:
    host_height_px = 900
    profile_full_hd = {"w": 1080, "h": 1920, "dpi": 480}
    profile_compact = {"w": 720, "h": 1280, "dpi": 320}
    fit_scale = round((host_height_px * 0.85) / profile_full_hd["h"], 2)
    actual = {
        "full_hd_dp": [int(profile_full_hd["w"] / (profile_full_hd["dpi"] / 160)), int(profile_full_hd["h"] / (profile_full_hd["dpi"] / 160))],
        "compact_dp": [int(profile_compact["w"] / (profile_compact["dpi"] / 160)), int(profile_compact["h"] / (profile_compact["dpi"] / 160))],
        "recommended_window_scale": fit_scale,
        "fits_host_after_scale": int(profile_full_hd["h"] * fit_scale) <= host_height_px,
    }
    expected = {
        "full_hd_dp": [360, 640],
        "compact_dp": [360, 640],
        "recommended_window_scale": 0.4,
        "fits_host_after_scale": True,
    }
    return {
        "name": "virtual_device_display_scaling",
        "summary": "计算不同分辨率与 DPI 虚拟设备的 dp 逻辑尺寸及适配宿主机屏幕的窗口缩放比例",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_storage_path_mapping() -> dict:
    symlinks = {
        "/mnt/sdcard": "/storage/emulated/0",
        "/sdcard": "/storage/emulated/0",
        "/mnt/shell/emulated/0": "/storage/emulated/0",
    }
    def resolve(path: str) -> str:
        for src, dst in sorted(symlinks.items(), key=lambda kv: -len(kv[0])):
            if path == src or path.startswith(src + "/"):
                return dst + path[len(src):]
        return path

    actual = {
        "legacy_eclipse_path": resolve("/mnt/sdcard/Download/demo.txt"),
        "genymotion_shell_path": resolve("/mnt/shell/emulated/0/Download/demo.txt"),
        "both_resolve_identical": resolve("/mnt/sdcard/Download/demo.txt") == resolve("/mnt/shell/emulated/0/Download/demo.txt"),
    }
    expected = {
        "legacy_eclipse_path": "/storage/emulated/0/Download/demo.txt",
        "genymotion_shell_path": "/storage/emulated/0/Download/demo.txt",
        "both_resolve_identical": True,
    }
    return {
        "name": "emulator_storage_path_mapping",
        "summary": "核对经典 /mnt/sdcard、Genymotion /mnt/shell/emulated/0/ 与现代 /storage/emulated/0 路径映射",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main() -> None:
    payload = {
        "version": "CPython 3.12.14 offline Android XML/layout/manifest verifier (Android SDK/emulator absent)",
        "cases": [
            case_abi_compatibility(),
            case_adb_sdk_binding(),
            case_display_scaling(),
            case_storage_path_mapping(),
        ],
    }
    print(json.dumps(payload, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/android-tutorial-genymotion-install-lab/explore-tutorial-genymotion-install.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-tutorial-genymotion-install.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Android

课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip

下载课程关联工程包:Android

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1894.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习