diff --git a/docs/running.md b/docs/running.md index f23896d..5593b89 100644 --- a/docs/running.md +++ b/docs/running.md @@ -151,6 +151,18 @@ python3 -m http.server -d build/web 8000 # then open http://localhost:8000 "$RENPY_SDK"/renpy.sh $RENPY_SDK/launcher distribute vn --package linux --destination build/dist ``` +```sh +# Android APK (needs the `[android]` table in work.toml, RAPT in the SDK and JDK 21) +"$RENPY_SDK"/renpy.sh $RENPY_SDK/launcher android_build vn --destination build/dist +``` + +RAPT (https://www.renpy.org/dl//renpy--rapt.zip) is unzipped into the SDK +folder, and its Android SDK is installed once with `python android.py installsdk` run from +`rapt/` using the SDK's own Python with the SDK folder on `sys.path` (it asks to accept +Google's Android SDK licence). The first build creates `vn/android.keystore` and +`vn/bundle.keystore`: back them up, keep them out of git and reuse the same ones for every +release, or installed apps cannot be updated. + `--package` also accepts `win` and `mac`; omit it to build every package. The web build also leaves a `build/web.zip` next to the folder. A web build writes `vn/progressive_download.txt`; delete it (it is not part of the generated project). diff --git a/docs/work-config.md b/docs/work-config.md index 448552b..e9a8036 100644 --- a/docs/work-config.md +++ b/docs/work-config.md @@ -134,3 +134,23 @@ Optional `[translations.]` tables: other languages from a published transl text (verbatim, never machine-translated). Configuration, mapping rules and the Ren'Py output are in [translations.md](translations.md); the mapping is committed as `data/translations/.json`. + +### Android + +Optional `[android]` table: `vnpipe renpy` then also writes the inputs RAPT (Ren'Py's Android +tool) needs into the project, so the APK can be built without the launcher GUI +(`docs/running.md`, section 5). + +```toml +[android] +package = "io.github.you.work" # required; permanent once published +name = "Title" # default: [work].title +orientation = "sensorLandscape" # default +icon = "assets/sprites/x.webp" # optional: subject for the adaptive icon (top square) +icon_background = "#14110e" # optional: icon background colour +presplash = "assets/bg/y.webp" # optional: image shown while the game loads +``` + +Files written: `android.json`, `android-icon_foreground.png`, `android-icon_background.png`, +`android-presplash.jpg`. The signing keystores (`android.keystore`, `bundle.keystore`) are never +generated by vnpipe: RAPT creates them on the first build and they must stay secret. diff --git a/src/vnpipe/android.py b/src/vnpipe/android.py new file mode 100644 index 0000000..498c04d --- /dev/null +++ b/src/vnpipe/android.py @@ -0,0 +1,80 @@ +"""Android packaging inputs for RAPT (Ren'Py's Android tool): ``android.json``, the adaptive +icon and the presplash. Written into the Ren'Py project only when the work has an +``[android]`` table; building the APK itself is a launcher/RAPT step (``docs/running.md``).""" + +from __future__ import annotations + +import json +from pathlib import Path + +from PIL import Image + +from vnpipe.config import Work + +ICON_SIZE = 432 # adaptive icon layer, in px; the safe zone is the central 2/3 +ICON_ART = 288 +PRESPLASH_SIZE = (1280, 720) + + +def android_config(work: Work) -> dict | None: + cfg = work.data.get("android") + if not cfg: + return None + if not cfg.get("package"): + raise ValueError("[android].package is required (e.g. 'io.github.you.work')") + return cfg + + +def render_android_json(work: Work, cfg: dict) -> str: + data = { + "package": cfg["package"], + "name": cfg.get("name", work.data["work"]["title"]), + "icon_name": cfg.get("name", work.data["work"]["title"]), + "orientation": cfg.get("orientation", "sensorLandscape"), + "permissions": cfg.get("permissions", []), + "store": "none", + "update_icons": True, + "update_always": True, + } + return json.dumps(data, indent=2, sort_keys=True) + "\n" + + +def foreground(source: Path) -> Image.Image: + """The subject of ``source`` (its top square: head and shoulders for a sprite), centred + in the transparent adaptive-icon layer.""" + img = Image.open(source).convert("RGBA") + box = img.getchannel("A").getbbox() or (0, 0, *img.size) + left, top, right, _ = box + side = min(right - left, img.height - top) + square = img.crop((left, top, left + side, top + side)).resize((ICON_ART, ICON_ART)) + layer = Image.new("RGBA", (ICON_SIZE, ICON_SIZE), (0, 0, 0, 0)) + offset = (ICON_SIZE - ICON_ART) // 2 + layer.alpha_composite(square, (offset, offset)) + return layer + + +def write_android(work: Work, out: Path) -> list[Path]: + """Write the RAPT inputs into the Ren'Py project ``out``; returns the files written.""" + cfg = android_config(work) + if cfg is None: + return [] + written = [] + + def save(name: str, content: str | Image.Image, **kwargs) -> None: + dest = out / name + dest.parent.mkdir(parents=True, exist_ok=True) + if isinstance(content, str): + dest.write_text(content, encoding="utf-8", newline="\n") + else: + content.save(dest, **kwargs) + written.append(dest) + + save("android.json", render_android_json(work, cfg)) + if cfg.get("icon"): + save("android-icon_foreground.png", foreground(work.path(cfg["icon"])), format="PNG") + background = Image.new("RGB", (ICON_SIZE, ICON_SIZE), cfg.get("icon_background", "#000000")) + save("android-icon_background.png", background, format="PNG") + if cfg.get("presplash"): + splash = Image.open(work.path(cfg["presplash"])).convert("RGB").resize(PRESPLASH_SIZE) + save("android-presplash.jpg", splash, format="JPEG", quality=88) + return written diff --git a/src/vnpipe/renpy_gen.py b/src/vnpipe/renpy_gen.py index b7bd115..8d7fd42 100644 --- a/src/vnpipe/renpy_gen.py +++ b/src/vnpipe/renpy_gen.py @@ -21,6 +21,7 @@ from typing import Any from PIL import Image, ImageDraw from vnpipe import PACKAGE_DIR +from vnpipe.android import write_android from vnpipe.assets import load_manifest, placeholder, style_file, usable_rendition from vnpipe.config import Work, load_work from vnpipe.schema import validate @@ -1323,6 +1324,7 @@ def generate( else: placeholder(e.asset, label=False).save(dest, "WEBP", quality=90) + written.extend(write_android(work, out)) return sorted(written) diff --git a/tests/test_android.py b/tests/test_android.py new file mode 100644 index 0000000..0319286 --- /dev/null +++ b/tests/test_android.py @@ -0,0 +1,63 @@ +import json +import shutil +from pathlib import Path + +import pytest +from PIL import Image + +from vnpipe import android +from vnpipe.config import load_work + +FIXTURES = Path(__file__).parent / "fixtures" + + +def make_work(tmp_path, android_cfg): + root = tmp_path / "w" + shutil.copytree(FIXTURES / "mini_work", root) + sprite = Image.new("RGBA", (200, 500), (0, 0, 0, 0)) + sprite.paste((200, 50, 50, 255), (50, 20, 150, 480)) + sprite.save(root / "sprite.png") + Image.new("RGB", (640, 360), "#123456").save(root / "bg.png") + work = load_work(root) + work.data["android"] = android_cfg + return work + + +def test_no_android_table_writes_nothing(tmp_path): + work = make_work(tmp_path, {}) + assert android.write_android(work, tmp_path / "out") == [] + + +def test_package_is_required(tmp_path): + work = make_work(tmp_path, {"name": "X"}) + with pytest.raises(ValueError, match="package"): + android.write_android(work, tmp_path / "out") + + +def test_writes_json_icon_and_presplash(tmp_path): + cfg = { + "package": "io.example.work", + "icon": "sprite.png", + "icon_background": "#102030", + "presplash": "bg.png", + } + work = make_work(tmp_path, cfg) + out = tmp_path / "out" + written = android.write_android(work, out) + assert sorted(p.name for p in written) == [ + "android-icon_background.png", + "android-icon_foreground.png", + "android-presplash.jpg", + "android.json", + ] + data = json.loads((out / "android.json").read_text()) + assert data["package"] == "io.example.work" + assert data["name"] == work.data["work"]["title"] + assert data["orientation"] == "sensorLandscape" + fg = Image.open(out / "android-icon_foreground.png") + assert fg.size == (432, 432) and fg.getpixel((0, 0))[3] == 0 # transparent margin + assert fg.getpixel((216, 216))[3] == 255 # subject in the middle + assert Image.open(out / "android-icon_background.png").getpixel((5, 5)) == (16, 32, 48) + assert Image.open(out / "android-presplash.jpg").size == (1280, 720) + again = android.write_android(work, out) # idempotent + assert [p.read_bytes() for p in again] == [p.read_bytes() for p in written]