Skip to content

Commit f7aba8a

Browse files
committed
introduce hook (original intent was to be able to hook before the apk build (but after build.py rendering) in order to change AndroidManifest.xml)
1 parent 7c274c3 commit f7aba8a

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

pythonforandroid/toolchain.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
from __future__ import print_function
1010

11-
1211
def check_python_dependencies():
1312
# Check if the Python requirements are installed. This appears
1413
# before the imports because otherwise they're imported elsewhere.
@@ -76,6 +75,7 @@ def check_python_dependencies():
7675

7776
import argparse
7877
import sh
78+
import imp
7979
from appdirs import user_data_dir
8080
import logging
8181

@@ -293,12 +293,17 @@ def __init__(self):
293293
help=('Dependencies of your app, should be recipe names or '
294294
'Python modules'),
295295
default='')
296-
296+
297297
generic_parser.add_argument(
298298
'--bootstrap',
299299
help='The bootstrap to build with. Leave unset to choose automatically.',
300300
default=None)
301301

302+
generic_parser.add_argument(
303+
'--hook',
304+
help='Filename to a module that contain python-for-android hooks',
305+
default=None)
306+
302307
add_boolean_option(
303308
generic_parser, ["force-build"],
304309
default=False,
@@ -488,6 +493,18 @@ def add_parser(subparsers, *args, **kwargs):
488493
# Each subparser corresponds to a method
489494
getattr(self, args.subparser_name.replace('-', '_'))(args)
490495

496+
def hook(self, name):
497+
if not self.args.hook:
498+
return
499+
if not hasattr(self, "hook_module"):
500+
# first time, try to load the hook module
501+
self.hook_module = imp.load_source("pythonforandroid.hook", self.args.hook)
502+
if hasattr(self.hook_module, name):
503+
info("Hook: execute {}".format(name))
504+
getattr(self.hook_module, name)(self)
505+
else:
506+
info("Hook: ignore {}".format(name))
507+
491508
@property
492509
def default_storage_dir(self):
493510
udd = user_data_dir('python-for-android')
@@ -691,8 +708,12 @@ def apk(self, args):
691708

692709
build = imp.load_source('build', join(dist.dist_dir, 'build.py'))
693710
with current_directory(dist.dist_dir):
711+
self.hook("before_apk_build")
694712
build_args = build.parse_args(args.unknown_args)
713+
self.hook("after_apk_build")
714+
self.hook("before_apk_assemble")
695715
output = shprint(sh.ant, args.build_mode, _tail=20, _critical=True, _env=env)
716+
self.hook("after_apk_assemble")
696717

697718
info_main('# Copying APK to current directory')
698719

@@ -807,7 +828,7 @@ def _adb(self, commands):
807828
for line in output:
808829
sys.stdout.write(line)
809830
sys.stdout.flush()
810-
831+
811832

812833
def build_status(self, args):
813834

0 commit comments

Comments
 (0)