118 lines
6.1 KiB
Python
118 lines
6.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Build the R1 app without Gradle (shared-resources rabbit.html#toolchain).
|
|
|
|
aapt2 compile/link -> javac --release 11 -> d8 -> zipalign -> apksigner, then copy the APK
|
|
to android/apk/routine.apk with its version in apk/routine-apk.json (the desktop installs it).
|
|
|
|
python3 android/build.py the real app -> android/apk/routine.apk (committed)
|
|
python3 android/build.py --dev me.teafry.routine.dev, "Routine dev", own storage -> build/routine-dev.apk
|
|
"""
|
|
import datetime as dt
|
|
import glob
|
|
import json
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import zipfile
|
|
|
|
VERSION_NAME = "0.17.0"
|
|
PKG = "me.teafry.routine"
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
REPO = os.path.dirname(HERE)
|
|
SDK = os.environ.get("ANDROID_HOME", os.path.expanduser("~/Android/Sdk"))
|
|
JAVA = os.environ.get("JAVA_HOME", "/usr/lib/jvm/java-21-openjdk")
|
|
BT = os.path.join(SDK, "build-tools", "34.0.0")
|
|
JAR = os.path.join(SDK, "platforms", "android-33", "android.jar")
|
|
B = os.path.join(HERE, "build")
|
|
KEYSTORE = os.path.join(HERE, "routine.keystore")
|
|
KS_PASS = "routine-r1" # a sideloaded app: nothing for the key to protect (rabbit.html)
|
|
|
|
|
|
def run(*cmd):
|
|
env = dict(os.environ, JAVA_HOME=JAVA, PATH=os.path.join(JAVA, "bin") + os.pathsep + os.environ["PATH"])
|
|
r = subprocess.run(cmd, env=env, capture_output=True, text=True)
|
|
if r.returncode:
|
|
sys.exit("FAILED: %s\n%s%s" % (" ".join(cmd), r.stdout, r.stderr))
|
|
return r.stdout
|
|
|
|
|
|
def main():
|
|
dev = "--dev" in sys.argv
|
|
pkg, label = (PKG + ".dev", "Routine dev") if dev else (PKG, "Routine")
|
|
shutil.rmtree(B, ignore_errors=True)
|
|
for d in ("assets/sounds", "gen", "classes", "dex"):
|
|
os.makedirs(os.path.join(B, d))
|
|
for f in ("schedule.json", "animations.json"):
|
|
shutil.copy(os.path.join(REPO, "shared", f), os.path.join(B, "assets", f))
|
|
for f in glob.glob(os.path.join(REPO, "shared", "sounds", "*.wav")):
|
|
shutil.copy(f, os.path.join(B, "assets", "sounds"))
|
|
os.makedirs(os.path.join(B, "assets", "fonts"))
|
|
for f in glob.glob(os.path.join(REPO, "shared", "fonts", "*.ttf")): # Caveat: the note's title
|
|
shutil.copy(f, os.path.join(B, "assets", "fonts"))
|
|
edition = os.path.expanduser("~/.local/share/fonts/edition_.ttf") # the light look's date, if
|
|
if os.path.exists(edition): # this machine has it (not in
|
|
shutil.copy(edition, os.path.join(B, "assets", "fonts", "Edition.ttf")) # the repo: its licence is unknown)
|
|
# the tasks' font, as on the desktop: Century Gothic if this machine has it (not in the repo,
|
|
# it's licensed), else URW Gothic (free, from Fedora's urw-base35 fonts), else the system's
|
|
for src, name in ((os.path.expanduser("~/.local/share/fonts/GOTHIC.TTF"), "Tasks.ttf"),
|
|
("/usr/share/fonts/urw-base35/URWGothic-Book.otf", "Tasks.otf")):
|
|
if os.path.exists(src):
|
|
shutil.copy(src, os.path.join(B, "assets", "fonts", name))
|
|
break
|
|
|
|
with open(os.path.join(HERE, "src/main/AndroidManifest.xml")) as f:
|
|
manifest = f.read().replace("@PKG@", pkg).replace("@LABEL@", label)
|
|
with open(B + "/AndroidManifest.xml", "w") as f:
|
|
f.write(manifest)
|
|
code = int((dt.datetime.now() - dt.datetime(2026, 1, 1)).total_seconds() // 60)
|
|
run(os.path.join(BT, "aapt2"), "compile", "--dir", os.path.join(HERE, "src/main/res"), "-o", B + "/res.zip")
|
|
run(os.path.join(BT, "aapt2"), "link", "-I", JAR, "--manifest", B + "/AndroidManifest.xml",
|
|
"--custom-package", PKG,
|
|
"-o", B + "/base.apk", "-A", B + "/assets", "--java", B + "/gen", "--min-sdk-version", "26",
|
|
"--target-sdk-version", "33", "--version-code", str(code), "--version-name", VERSION_NAME,
|
|
"-0", "wav", "-0", "json", B + "/res.zip")
|
|
|
|
srcs = glob.glob(os.path.join(HERE, "src/main/java/**/*.java"), recursive=True) + \
|
|
glob.glob(B + "/gen/**/*.java", recursive=True)
|
|
run(os.path.join(JAVA, "bin/javac"), "--release", "11", "-classpath", JAR, "-d", B + "/classes", "-Xlint:-options", *srcs)
|
|
|
|
# build-tools 34's d8 crashes on javac 21's nameless MethodParameters in inner-class constructors:
|
|
# only static nested classes and lambdas are allowed (rabbit.html#toolchain).
|
|
classes = glob.glob(B + "/classes/**/*.class", recursive=True)
|
|
bad = []
|
|
for c in classes:
|
|
if "$" not in os.path.basename(c):
|
|
continue
|
|
name = os.path.basename(c)[:-6].split("$")[-1]
|
|
if name.isdigit():
|
|
bad.append(c)
|
|
elif "MethodParameters" in run(os.path.join(JAVA, "bin/javap"), "-v", c):
|
|
bad.append(c)
|
|
if bad:
|
|
sys.exit("anonymous/inner classes are not allowed:\n " + "\n ".join(bad))
|
|
|
|
run(os.path.join(BT, "d8"), "--release", "--min-api", "26", "--lib", JAR, "--output", B + "/dex", *classes)
|
|
with zipfile.ZipFile(B + "/base.apk", "a") as z:
|
|
z.write(B + "/dex/classes.dex", "classes.dex")
|
|
run(os.path.join(BT, "zipalign"), "-f", "-p", "4", B + "/base.apk", B + "/aligned.apk")
|
|
if not os.path.exists(KEYSTORE):
|
|
run(os.path.join(JAVA, "bin/keytool"), "-genkeypair", "-keystore", KEYSTORE, "-storepass", KS_PASS,
|
|
"-keypass", KS_PASS, "-alias", "routine", "-keyalg", "RSA", "-keysize", "2048", "-validity", "36500",
|
|
"-dname", "CN=routine")
|
|
run(os.path.join(BT, "apksigner"), "sign", "--ks", KEYSTORE, "--ks-pass", "pass:" + KS_PASS,
|
|
"--out", B + "/routine.apk", B + "/aligned.apk")
|
|
if dev:
|
|
print("built android/build/routine.apk as %s (dev), %d" % (pkg, code))
|
|
return
|
|
os.makedirs(os.path.join(HERE, "apk"), exist_ok=True)
|
|
shutil.copy(B + "/routine.apk", os.path.join(HERE, "apk", "routine.apk"))
|
|
with open(os.path.join(HERE, "apk", "routine-apk.json"), "w") as f:
|
|
json.dump({"package": PKG, "versionName": VERSION_NAME, "versionCode": code}, f, indent=2)
|
|
print("built android/apk/routine.apk %s (%d), %d KiB" % (VERSION_NAME, code,
|
|
os.path.getsize(B + "/routine.apk") // 1024))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|