diff --git a/pythonforandroid/bootstraps/common/build/build.py b/pythonforandroid/bootstraps/common/build/build.py index 8eb1a6fe0e..6b902dd0b1 100644 --- a/pythonforandroid/bootstraps/common/build/build.py +++ b/pythonforandroid/bootstraps/common/build/build.py @@ -87,11 +87,6 @@ def get_bootstrap_name(): join(curdir, 'templates'))) -def try_unlink(fn): - if exists(fn): - os.unlink(fn) - - def ensure_dir(path): if not exists(path): makedirs(path) @@ -239,8 +234,7 @@ def make_package(args): assets_dir = "src/main/assets" # Delete the old assets. - try_unlink(join(assets_dir, 'public.mp3')) - try_unlink(join(assets_dir, 'private.mp3')) + shutil.rmtree(assets_dir, ignore_errors=True) ensure_dir(assets_dir) # Add extra environment variable file into tar-able directory: @@ -304,6 +298,15 @@ def make_package(args): tar_dirs.append(python_bundle_dir) if get_bootstrap_name() == "webview": tar_dirs.append('webview_includes') + + for asset in args.assets: + asset_src, asset_dest = asset.split(":") + if isfile(realpath(asset_src)): + ensure_dir(dirname(join(assets_dir, asset_dest))) + shutil.copy(realpath(asset_src), join(assets_dir, asset_dest)) + else: + shutil.copytree(realpath(asset_src), join(assets_dir, asset_dest)) + if args.private or args.launcher: make_tar( join(assets_dir, 'private.mp3'), tar_dirs, args.ignore_path, @@ -599,6 +602,10 @@ def parse_args_and_make_package(args=None): help='Custom key=value to add in application metadata') ap.add_argument('--uses-library', dest='android_used_libs', action='append', default=[], help='Used shared libraries included using tag in AndroidManifest.xml') + ap.add_argument('--asset', dest='assets', + action="append", default=[], + metavar="/path/to/source:dest", + help='Put this in the assets folder at assets/dest') ap.add_argument('--icon', dest='icon', help=('A png file to use as the icon for ' 'the application.')) diff --git a/pythonforandroid/recipes/pygame/__init__.py b/pythonforandroid/recipes/pygame/__init__.py new file mode 100644 index 0000000000..37238b8661 --- /dev/null +++ b/pythonforandroid/recipes/pygame/__init__.py @@ -0,0 +1,56 @@ +from pythonforandroid.recipe import CompiledComponentsPythonRecipe +from os.path import join +from pythonforandroid.toolchain import current_directory + + +class Pygame2Recipe(CompiledComponentsPythonRecipe): + + version = '2.0.0-dev7' + url = 'https://github.com/pygame/pygame/archive/android-2.0.0-dev7.tar.gz' + + site_packages_name = 'pygame' + name = 'pygame' + + depends = ['sdl2', 'sdl2_image', 'sdl2_mixer', 'sdl2_ttf', 'setuptools', 'jpeg', 'png'] + call_hostpython_via_targetpython = False # Due to setuptools + install_in_hostpython = False + + def prebuild_arch(self, arch): + super().prebuild_arch(arch) + with current_directory(self.get_build_dir(arch.arch)): + setup_template = open(join("buildconfig", "Setup.Android.SDL2.in")).read() + env = self.get_recipe_env(arch) + env['ANDROID_ROOT'] = join(self.ctx.ndk_platform, 'usr') + + ndk_lib_dir = join(self.ctx.ndk_platform, 'usr', 'lib') + + png = self.get_recipe('png', self.ctx) + png_lib_dir = join(png.get_build_dir(arch.arch), '.libs') + png_inc_dir = png.get_build_dir(arch) + + jpeg = self.get_recipe('jpeg', self.ctx) + jpeg_inc_dir = jpeg_lib_dir = jpeg.get_build_dir(arch.arch) + + setup_file = setup_template.format( + sdl_includes=( + " -I" + join(self.ctx.bootstrap.build_dir, 'jni', 'SDL', 'include') + + " -L" + join(self.ctx.bootstrap.build_dir, "libs", str(arch)) + + " -L" + png_lib_dir + " -L" + jpeg_lib_dir + " -L" + ndk_lib_dir), + sdl_ttf_includes="-I"+join(self.ctx.bootstrap.build_dir, 'jni', 'SDL2_ttf'), + sdl_image_includes="-I"+join(self.ctx.bootstrap.build_dir, 'jni', 'SDL2_image'), + sdl_mixer_includes="-I"+join(self.ctx.bootstrap.build_dir, 'jni', 'SDL2_mixer'), + jpeg_includes="-I"+jpeg_inc_dir, + png_includes="-I"+png_inc_dir, + freetype_includes="" + ) + open("Setup", "w").write(setup_file) + + def get_recipe_env(self, arch): + env = super(Pygame2Recipe, self).get_recipe_env(arch) + env['USE_SDL2'] = '1' + env["PYGAME_CROSS_COMPILE"] = "TRUE" + env["PYGAME_ANDROID"] = "TRUE" + return env + + +recipe = Pygame2Recipe() diff --git a/pythonforandroid/toolchain.py b/pythonforandroid/toolchain.py index bc427f6d35..90bcb5e149 100644 --- a/pythonforandroid/toolchain.py +++ b/pythonforandroid/toolchain.py @@ -491,6 +491,10 @@ def add_parser(subparsers, *args, **kwargs): # However, it is also needed before the distribution is finally # assembled for locating the setup.py / other build systems, which # is why we also add it here: + parser_packaging.add_argument( + '--add-asset', dest='assets', + action="append", default=[], + help='Put this in the assets folder in the apk.') parser_packaging.add_argument( '--private', dest='private', help='the directory with the app source code files' + @@ -949,6 +953,14 @@ def _fix_args(args): fix_args = ('--dir', '--private', '--add-jar', '--add-source', '--whitelist', '--blacklist', '--presplash', '--icon') unknown_args = args.unknown_args + + for asset in args.assets: + if ":" in asset: + asset_src, asset_dest = asset.split(":") + else: + 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 i, arg in enumerate(unknown_args): argx = arg.split('=') if argx[0] in fix_args: