-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp4a_hook.py
More file actions
55 lines (40 loc) · 1.73 KB
/
Copy pathp4a_hook.py
File metadata and controls
55 lines (40 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from pathlib import Path
from pythonforandroid.toolchain import ToolchainCL
# LiteRT-LM GPU acceleration uses OpenCL. On Android, the required
# non-NDK native libraries must be declared in the application manifest.
#
# See:
# https://github.com/google-ai-edge/LiteRT-LM/blob/main/docs/api/kotlin/getting_started.md
#
# The libraries are optional so the APK can still be installed on devices
# that do not provide them; GPU availability is handled at runtime.
LITERT_NATIVE_LIBRARIES = """
<uses-native-library
android:name="libvndksupport.so"
android:required="false" />
<uses-native-library
android:name="libOpenCL.so"
android:required="false" />
"""
def after_apk_build(toolchain: ToolchainCL) -> None:
"""Add native-library declarations required for LiteRT-LM GPU support."""
manifest = Path(toolchain._dist.dist_dir) / "src" / "main" / "AndroidManifest.xml"
print(f"Patching manifest for LiteRT-LM GPU support: {manifest}")
if not manifest.exists():
raise RuntimeError(f"Generated manifest does not exist: {manifest}")
text = manifest.read_text(encoding="utf-8")
# libOpenCL.so is used as the marker because this hook always adds
# both LiteRT-LM declarations together.
if 'android:name="libOpenCL.so"' in text:
print("LiteRT-LM native-library declarations already present")
return
marker = "</application>"
if marker not in text:
raise RuntimeError(f"Cannot find {marker!r} in {manifest}")
updated = text.replace(
marker,
LITERT_NATIVE_LIBRARIES + "\n " + marker,
1,
)
manifest.write_text(updated, encoding="utf-8")
print("Added LiteRT-LM GPU native-library declarations")