A catalog app for the LihonT mod: avatar frames and chat bubbles made by members. Browse, add with the plus button, author profiles, publishing with a check before anything is shown. Signs in through the TikTok that carries the mod, which hands over only the public account data to an app signed with the same key. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
103 lines
4.0 KiB
Python
103 lines
4.0 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
Builds LihonT Store into dist/lihonstore.apk: aapt2 (resources), javac, D8 (dex) and a signature.
|
|
|
|
What it needs, found by itself or named in the environment:
|
|
ANDROID_HOME the Android SDK (build-tools with aapt2, platforms/android-34/android.jar); default: the usual place of the platform
|
|
R8_JAR r8.jar of the Android build tools (it holds D8); default: tools/r8.jar
|
|
UBER_SIGNER uber-apk-signer.jar; default: tools/uber-apk-signer.jar
|
|
JDK 17 or newer with javac and java on the PATH.
|
|
|
|
Signing: LihonT Store and the TikTok that carries the LihonT mod must be signed with the same key, or the mod will not hand over the
|
|
account. uber-apk-signer signs with its built-in debug key unless you give it your own (see its --ks option), the same as the mod.
|
|
|
|
py build.py
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import zipfile
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parent
|
|
BUILD = ROOT / "build"
|
|
DIST = ROOT / "dist"
|
|
FINAL = DIST / "lihonstore.apk"
|
|
|
|
|
|
def sdk() -> Path:
|
|
home = os.environ.get("ANDROID_HOME") or os.environ.get("ANDROID_SDK_ROOT")
|
|
if home:
|
|
return Path(home)
|
|
for candidate in (Path.home() / "AppData" / "Local" / "Android" / "Sdk", Path.home() / "Android" / "Sdk", Path.home() / "Library" / "Android" / "sdk"):
|
|
if candidate.is_dir():
|
|
return candidate
|
|
sys.exit("Android SDK not found: set ANDROID_HOME")
|
|
|
|
|
|
def newest(folder: Path) -> Path:
|
|
versions = sorted(p for p in folder.iterdir() if p.is_dir())
|
|
if not versions:
|
|
sys.exit("nothing in " + str(folder))
|
|
return versions[-1]
|
|
|
|
|
|
def tool(path: Path, what: str) -> Path:
|
|
if not path.is_file():
|
|
sys.exit("%s not found: %s" % (what, path))
|
|
return path
|
|
|
|
|
|
def run(command: list, what: str) -> None:
|
|
print("->", what)
|
|
done = subprocess.run([str(part) for part in command], capture_output=True, text=True)
|
|
if done.returncode != 0:
|
|
print(done.stdout + done.stderr)
|
|
sys.exit("failed: " + what)
|
|
|
|
|
|
def main() -> None:
|
|
home = sdk()
|
|
suffix = ".exe" if os.name == "nt" else ""
|
|
aapt2 = tool(newest(home / "build-tools") / ("aapt2" + suffix), "aapt2")
|
|
android_jar = tool(home / "platforms" / "android-34" / "android.jar", "android.jar of platform 34")
|
|
r8 = tool(Path(os.environ.get("R8_JAR", ROOT / "tools" / "r8.jar")), "r8.jar")
|
|
signer = tool(Path(os.environ.get("UBER_SIGNER", ROOT / "tools" / "uber-apk-signer.jar")), "uber-apk-signer.jar")
|
|
|
|
shutil.rmtree(BUILD, ignore_errors=True)
|
|
classes, dex, gen = BUILD / "classes", BUILD / "dex", BUILD / "gen"
|
|
for folder in (classes, dex, gen, DIST):
|
|
folder.mkdir(parents=True, exist_ok=True)
|
|
|
|
compiled = BUILD / "resources.zip"
|
|
run([aapt2, "compile", "--dir", ROOT / "res", "-o", compiled], "compiling resources")
|
|
unaligned = BUILD / "unaligned.apk"
|
|
run([aapt2, "link", "-I", android_jar, "--manifest", ROOT / "AndroidManifest.xml", "-o", unaligned,
|
|
"--java", gen, "--auto-add-overlay", compiled], "linking the package and generating R.java")
|
|
|
|
sources = list((ROOT / "src").rglob("*.java")) + list(gen.rglob("*.java"))
|
|
run(["javac", "--release", "17", "-encoding", "UTF-8", "-cp", android_jar, "-d", classes] + sources, "compiling %d sources" % len(sources))
|
|
|
|
class_files = [str(path) for path in classes.rglob("*.class")]
|
|
run(["java", "-cp", r8, "com.android.tools.r8.D8", "--release", "--min-api", "31", "--lib", android_jar, "--output", dex] + class_files,
|
|
"making classes.dex")
|
|
with zipfile.ZipFile(unaligned, "a") as package:
|
|
package.write(dex / "classes.dex", "classes.dex")
|
|
|
|
signed = BUILD / "signed"
|
|
run(["java", "-jar", signer, "--apks", unaligned, "--out", signed], "signing")
|
|
built = sorted(signed.glob("*.apk"))
|
|
if not built:
|
|
sys.exit("no signed apk was produced")
|
|
shutil.copy2(built[0], FINAL)
|
|
print("built %s (%d KB)" % (FINAL, FINAL.stat().st_size // 1024))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|