Automation foundation model for tiny devices: 2-bit, 8-29 MB, tool calls, ASR, structured extraction and embeddings on phones, wearables, smart homes, robots, cars and microcontrollers.
feat(android): add detection for Android platform and handle wheel loading errors
HenryNdubuaku6dae40037308
diff --git a/needle/agent/fetch.py b/needle/agent/fetch.pyindex aa605b8..33d3491 100644--- a/needle/agent/fetch.py+++ b/needle/agent/fetch.py@@ -105,11 +105,29 @@ def _is_musl():_ENGINE_ARCHES = {"x86_64": "x86_64", "amd64": "x86_64", "aarch64": "aarch64", "arm64": "aarch64"}+def _is_android():+ """Android reports itself as Linux but links Bionic, where no published engine loads.++ Termux is the usual way this is reached. ``platform.libc_ver()`` finds no glibc+ there, so the Linux path would pick the musl aarch64 wheel and fail to load it.+ """+ if hasattr(sys, "getandroidapilevel"):+ return True+ return bool(os.environ.get("ANDROID_ROOT") and os.environ.get("ANDROID_DATA"))++def _platform_tag():machine = platform.machine().lower()if sys.platform == "darwin":arch = "arm64" if machine in ("arm64", "aarch64") else "x86_64"return "macosx_11_0_" + arch+ if _is_android():+ raise RuntimeError(+ "no Needle engine wheel is published for Android (Bionic): the Linux wheels link "+ "glibc or musl, so libneedle fails to load with a missing libm.so.6. The Android "+ "builds ship a static library and the needle CLI (needle download android-arm64), "+ "not a shared library for ctypes; to use the Python API here, build a shared "+ "libneedle for Bionic and point NEEDLE3_LIB_PATH (NEEDLE2_LIB_PATH for Needle 2) at it.")arch = _ENGINE_ARCHES.get(machine)if arch is None:# Any other machine (32-bit ARM or x86, riscv64, a 32-bit Python ondiff --git a/tests/test_fetch.py b/tests/test_fetch.pyindex 25b38db..1496ea0 100644--- a/tests/test_fetch.py+++ b/tests/test_fetch.py@@ -241,6 +241,7 @@ def test_platform_tag_names_only_the_architectures_the_engine_is_built_for(monkefrom needle.agent import fetchmonkeypatch.setattr(fetch, "_is_musl", lambda: False)+ monkeypatch.setattr(fetch, "_is_android", lambda: False)for system, machine, tag in (("linux", "x86_64", "manylinux2014_x86_64"),("linux", "aarch64", "manylinux2014_aarch64"),("win32", "AMD64", "win_amd64"),@@ -294,3 +295,39 @@ def test_engine_load_falls_back_to_the_other_libc(monkeypatch, tmp_path):with pytest.warns(UserWarning, match="using the musllinux"):assert needle._load_cdll(3) == "handle"assert loaded["path"].endswith("musl/libneedle.so")+++def test_android_is_refused_instead_of_being_handed_a_glibc_wheel(monkeypatch):+ """Termux reports Linux/aarch64, so it used to download a wheel Bionic cannot load."""+ import platform+ from needle.agent import fetch++ monkeypatch.setattr(sys, "platform", "linux")+ monkeypatch.setattr(platform, "machine", lambda: "aarch64")+ monkeypatch.setattr(fetch, "_is_musl", lambda: False)++ monkeypatch.setattr(fetch, "_is_android", lambda: False)+ assert fetch._platform_tag() == "manylinux2014_aarch64"++ monkeypatch.setattr(fetch, "_is_android", lambda: True)+ with pytest.raises(RuntimeError, match="Android .Bionic.*libm.so.6.*NEEDLE3_LIB_PATH"):+ fetch._platform_tag()+++def test_android_is_detected_from_the_api_level_or_the_runtime_env(monkeypatch):+ from needle.agent import fetch++ monkeypatch.delenv("ANDROID_ROOT", raising=False)+ monkeypatch.delenv("ANDROID_DATA", raising=False)+ monkeypatch.delattr(sys, "getandroidapilevel", raising=False)+ assert fetch._is_android() is False++ monkeypatch.setenv("ANDROID_ROOT", "/system")+ assert fetch._is_android() is False, "one marker alone is not Android"+ monkeypatch.setenv("ANDROID_DATA", "/data")+ assert fetch._is_android() is True++ monkeypatch.delenv("ANDROID_ROOT")+ monkeypatch.delenv("ANDROID_DATA")+ monkeypatch.setattr(sys, "getandroidapilevel", lambda: 34, raising=False)+ assert fetch._is_android() is True