Skip to content

Commit

Permalink
Added --add-resource option (#2684)
Browse files Browse the repository at this point in the history
* add_resources

* add_resource

* Update build.py

* stateless

* multiple kinds

* pep8

* --add_resource
  • Loading branch information
RobertFlatt authored Nov 4, 2022
1 parent 53d77fc commit 5ca3a52
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/buildoptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ options (this list may not be exhaustive):
- ``--add-source``: Add a source directory to the app's Java code.
- ``--no-byte-compile-python``: Skip byte compile for .py files.
- ``--enable-androidx``: Enable AndroidX support library.
- ``--add-resource``: Put this file or directory in the apk res directory.


webview
Expand Down
22 changes: 22 additions & 0 deletions pythonforandroid/bootstraps/common/build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,24 @@ def make_package(args):

# Prepare some variables for templating process
res_dir = "src/main/res"
res_dir_initial = "src/res_initial"
# make res_dir stateless
if exists(res_dir_initial):
shutil.rmtree(res_dir, ignore_errors=True)
shutil.copytree(res_dir_initial, res_dir)
else:
shutil.copytree(res_dir, res_dir_initial)

# Add user resouces
for resource in args.resources:
resource_src, resource_dest = resource.split(":")
if isfile(realpath(resource_src)):
ensure_dir(dirname(join(res_dir, resource_dest)))
shutil.copy(realpath(resource_src), join(res_dir, resource_dest))
else:
shutil.copytree(realpath(resource_src),
join(res_dir, resource_dest), dirs_exist_ok=True)

default_icon = 'templates/kivy-icon.png'
default_presplash = 'templates/kivy-presplash.jpg'
shutil.copy(
Expand Down Expand Up @@ -687,6 +705,10 @@ def parse_args_and_make_package(args=None):
action="append", default=[],
metavar="/path/to/source:dest",
help='Put this in the assets folder at assets/dest')
ap.add_argument('--resource', dest='resources',
action="append", default=[],
metavar="/path/to/source:kind/asset",
help='Put this in the res folder at res/kind')
ap.add_argument('--icon', dest='icon',
help=('A png file to use as the icon for '
'the application.'))
Expand Down
12 changes: 12 additions & 0 deletions pythonforandroid/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,10 @@ def add_parser(subparsers, *args, **kwargs):
'--add-asset', dest='assets',
action="append", default=[],
help='Put this in the assets folder in the apk.')
parser_packaging.add_argument(
'--add-resource', dest='resources',
action="append", default=[],
help='Put this in the res folder in the apk.')
parser_packaging.add_argument(
'--private', dest='private',
help='the directory with the app source code files' +
Expand Down Expand Up @@ -1000,6 +1004,14 @@ def _fix_args(args):
asset_src = asset_dest = asset
# take abspath now, because build.py will be run in bootstrap dir
unknown_args += ["--asset", os.path.abspath(asset_src)+":"+asset_dest]
for resource in args.resources:
if ":" in resource:
resource_src, resource_dest = resource.split(":")
else:
resource_src = resource
resource_dest = ""
# take abspath now, because build.py will be run in bootstrap dir
unknown_args += ["--resource", os.path.abspath(resource_src)+":"+resource_dest]
for i, arg in enumerate(unknown_args):
argx = arg.split('=')
if argx[0] in fix_args:
Expand Down

0 comments on commit 5ca3a52

Please sign in to comment.