diff --git a/doc/source/buildoptions.rst b/doc/source/buildoptions.rst index d9f97175a2..cfe8bc1a04 100644 --- a/doc/source/buildoptions.rst +++ b/doc/source/buildoptions.rst @@ -91,6 +91,7 @@ options (this list may not be exhaustive): - ``--add-source``: Add a source directory to the app's Java code. - ``--no-compile-pyo``: Do not optimise .py files to .pyo. - ``--enable-androidx``: Enable AndroidX support library. +- ``--add-resource``: Put this file or directory in the apk res directory. webview diff --git a/pythonforandroid/bootstraps/common/build/build.py b/pythonforandroid/bootstraps/common/build/build.py index 6885a333df..a9f4dde42e 100644 --- a/pythonforandroid/bootstraps/common/build/build.py +++ b/pythonforandroid/bootstraps/common/build/build.py @@ -340,6 +340,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( @@ -698,6 +716,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.')) diff --git a/pythonforandroid/toolchain.py b/pythonforandroid/toolchain.py index 1b81aa923c..daf895e897 100644 --- a/pythonforandroid/toolchain.py +++ b/pythonforandroid/toolchain.py @@ -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' + @@ -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: