Repository navigation
Expand file tree
/
Copy pathp4a_hook.py
More file actions
64 lines (53 loc) · 2.6 KB
/
Copy pathp4a_hook.py
File metadata and controls
64 lines (53 loc) · 2.6 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
56
57
58
59
60
61
62
63
64
# SPDX-License-Identifier: MIT
"""python-for-android hooks for the runner (wired in via ``p4a.hook``).
``before_apk_build`` runs inside the dist directory that gradle is about to
compile, so the boot.py entry-point patch lands in the Java that ships. Patching
p4a's bootstrap source from build_android.sh missed on a first build, when
buildozer had not cloned p4a yet, and never reached an existing dist: the APK
then started ``main.pyc`` directly, skipping boot.py and its stdio sidecar
(android-runner#9).
``before_apk_build`` also refuses a dist whose Python packages differ from the
recipe pins (``stale_dist.py``): p4a reuses an existing dist and keeps older
installed versions beside newer ones, so a moved pin could ship the old build.
``after_apk_build`` runs once p4a has rendered the manifest and before gradle
reads it. It declares ``org.pydevices.runner.Relauncher`` (``p4a_java/``) in a
process of its own; buildozer's ``android.add_activities`` can't set a process.
"""
import os
import pathlib
import sys
_HERE = pathlib.Path(__file__).resolve().parent
if str(_HERE) not in sys.path:
sys.path.insert(0, str(_HERE))
from patch_p4a_boot_entrypoint import patch_file # noqa: E402
import stale_dist # noqa: E402
_ACTIVITY = "src/main/java/org/kivy/android/PythonActivity.java"
_MANIFEST = "src/main/AndroidManifest.xml"
_RELAUNCHER = (
'<activity android:name="org.pydevices.runner.Relauncher"'
' android:process=":relaunch"'
' android:theme="@android:style/Theme.Translucent.NoTitleBar"'
' android:excludeFromRecents="true"'
' android:noHistory="true"'
' android:exported="false" />'
)
def before_apk_build(toolchain):
sites = stale_dist.check(os.getcwd())
print("p4a_hook: dist packages match the recipe pins (%d bundle(s))" % sites)
path = pathlib.Path(os.getcwd()) / _ACTIVITY
if not path.is_file():
raise RuntimeError("p4a_hook: no %s in dist %s" % (_ACTIVITY, os.getcwd()))
status = patch_file(path)
print("p4a_hook: boot.py entry point %s: %s" % (status, path))
if status == "no-match":
raise RuntimeError("p4a_hook: getEntryPoint not found in %s" % path)
def after_apk_build(toolchain):
path = pathlib.Path(os.getcwd()) / _MANIFEST
text = path.read_text()
if "org.pydevices.runner.Relauncher" in text:
print("p4a_hook: Relauncher already declared: %s" % path)
return
if "</application>" not in text:
raise RuntimeError("p4a_hook: no </application> in %s" % path)
path.write_text(text.replace("</application>", " " + _RELAUNCHER + "\n </application>", 1))
print("p4a_hook: Relauncher declared: %s" % path)