Skip to content

Commit 5ca3a52

Browse files
authored
Added --add-resource option (#2684)
* add_resources * add_resource * Update build.py * stateless * multiple kinds * pep8 * --add_resource
1 parent 53d77fc commit 5ca3a52

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

doc/source/buildoptions.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ options (this list may not be exhaustive):
9191
- ``--add-source``: Add a source directory to the app's Java code.
9292
- ``--no-byte-compile-python``: Skip byte compile for .py files.
9393
- ``--enable-androidx``: Enable AndroidX support library.
94+
- ``--add-resource``: Put this file or directory in the apk res directory.
9495

9596

9697
webview

pythonforandroid/bootstraps/common/build/build.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,24 @@ def make_package(args):
329329

330330
# Prepare some variables for templating process
331331
res_dir = "src/main/res"
332+
res_dir_initial = "src/res_initial"
333+
# make res_dir stateless
334+
if exists(res_dir_initial):
335+
shutil.rmtree(res_dir, ignore_errors=True)
336+
shutil.copytree(res_dir_initial, res_dir)
337+
else:
338+
shutil.copytree(res_dir, res_dir_initial)
339+
340+
# Add user resouces
341+
for resource in args.resources:
342+
resource_src, resource_dest = resource.split(":")
343+
if isfile(realpath(resource_src)):
344+
ensure_dir(dirname(join(res_dir, resource_dest)))
345+
shutil.copy(realpath(resource_src), join(res_dir, resource_dest))
346+
else:
347+
shutil.copytree(realpath(resource_src),
348+
join(res_dir, resource_dest), dirs_exist_ok=True)
349+
332350
default_icon = 'templates/kivy-icon.png'
333351
default_presplash = 'templates/kivy-presplash.jpg'
334352
shutil.copy(
@@ -687,6 +705,10 @@ def parse_args_and_make_package(args=None):
687705
action="append", default=[],
688706
metavar="/path/to/source:dest",
689707
help='Put this in the assets folder at assets/dest')
708+
ap.add_argument('--resource', dest='resources',
709+
action="append", default=[],
710+
metavar="/path/to/source:kind/asset",
711+
help='Put this in the res folder at res/kind')
690712
ap.add_argument('--icon', dest='icon',
691713
help=('A png file to use as the icon for '
692714
'the application.'))

pythonforandroid/toolchain.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,10 @@ def add_parser(subparsers, *args, **kwargs):
512512
'--add-asset', dest='assets',
513513
action="append", default=[],
514514
help='Put this in the assets folder in the apk.')
515+
parser_packaging.add_argument(
516+
'--add-resource', dest='resources',
517+
action="append", default=[],
518+
help='Put this in the res folder in the apk.')
515519
parser_packaging.add_argument(
516520
'--private', dest='private',
517521
help='the directory with the app source code files' +
@@ -1000,6 +1004,14 @@ def _fix_args(args):
10001004
asset_src = asset_dest = asset
10011005
# take abspath now, because build.py will be run in bootstrap dir
10021006
unknown_args += ["--asset", os.path.abspath(asset_src)+":"+asset_dest]
1007+
for resource in args.resources:
1008+
if ":" in resource:
1009+
resource_src, resource_dest = resource.split(":")
1010+
else:
1011+
resource_src = resource
1012+
resource_dest = ""
1013+
# take abspath now, because build.py will be run in bootstrap dir
1014+
unknown_args += ["--resource", os.path.abspath(resource_src)+":"+resource_dest]
10031015
for i, arg in enumerate(unknown_args):
10041016
argx = arg.split('=')
10051017
if argx[0] in fix_args:

0 commit comments

Comments
 (0)