GingerCal 1.1: apksigner v1 signing so Android 2.3 installs it; add Pangolin VPS setup notes
This commit is contained in:
1 parent
01f49852ab
commit
f44ecf4871
10 files changed
+267
-246
No files matched your search
@@ -0,0 +1,31 @@
|
||||
import java.io.File;
|
||||
|
||||
import com.android.tools.smali.baksmali.Baksmali;
|
||||
import com.android.tools.smali.baksmali.BaksmaliOptions;
|
||||
import com.android.tools.smali.dexlib2.DexFileFactory;
|
||||
import com.android.tools.smali.dexlib2.Opcodes;
|
||||
import com.android.tools.smali.dexlib2.iface.DexFile;
|
||||
import com.android.tools.smali.dexlib2.iface.MultiDexContainer;
|
||||
|
||||
/**
|
||||
* Disassembles d8's classes.dex to smali with the baksmali bundled in apktool.jar, so that
|
||||
* apktool re-assembles it with dexlib2 (clean dex 035, correct checksums).
|
||||
* usage: java -cp apktool.jar:. Disassemble classes.dex out-dir api-level
|
||||
*/
|
||||
public class Disassemble {
|
||||
public static void main(String[] a) throws Exception {
|
||||
int api = Integer.parseInt(a[2]);
|
||||
MultiDexContainer<? extends DexFile> c = DexFileFactory.loadDexContainer(new File(a[0]), Opcodes.forApi(api));
|
||||
if (c.getDexEntryNames().size() != 1) throw new IllegalStateException("expected one dex, got " + c.getDexEntryNames());
|
||||
DexFile dex = c.getEntry(c.getDexEntryNames().get(0)).getDexFile();
|
||||
BaksmaliOptions o = new BaksmaliOptions();
|
||||
o.apiLevel = api;
|
||||
o.debugInfo = true; // keep line numbers for readable stack traces
|
||||
o.localsDirective = true;
|
||||
o.sequentialLabels = true;
|
||||
if (!Baksmali.disassembleDexFile(dex, new File(a[1]), 1, o)) {
|
||||
throw new IllegalStateException("baksmali reported errors");
|
||||
}
|
||||
System.out.println("disassembled " + dex.getClasses().size() + " classes");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Checks the dex inside an APK the way Dalvik's dexopt does at install time:
|
||||
format version, file size, Adler-32 checksum, SHA-1 signature, and no post-035 sections.
|
||||
usage: check_dex.py app.apk"""
|
||||
import hashlib
|
||||
import struct
|
||||
import sys
|
||||
import zipfile
|
||||
import zlib
|
||||
|
||||
d = zipfile.ZipFile(sys.argv[1]).read("classes.dex")
|
||||
problems = []
|
||||
if d[:8] != b"dex\n035\x00":
|
||||
problems.append("dex version %r (Android 2.3 needs 035)" % d[:8])
|
||||
if struct.unpack("<I", d[32:36])[0] != len(d):
|
||||
problems.append("file_size field wrong")
|
||||
if zlib.adler32(d[12:]) & 0xFFFFFFFF != struct.unpack("<I", d[8:12])[0]:
|
||||
problems.append("Adler-32 checksum wrong")
|
||||
if hashlib.sha1(d[32:]).digest() != d[12:32]:
|
||||
problems.append("SHA-1 signature wrong")
|
||||
map_off = struct.unpack("<I", d[52:56])[0]
|
||||
for i in range(struct.unpack("<I", d[map_off:map_off + 4])[0]):
|
||||
t = struct.unpack("<H", d[map_off + 4 + i * 12:map_off + 6 + i * 12])[0]
|
||||
if t in (7, 8):
|
||||
problems.append("contains call_site/method_handle sections (needs Android 8+)")
|
||||
for p in problems:
|
||||
print("DEX PROBLEM:", p)
|
||||
print("classes.dex: %d bytes, %s" % (len(d), "OK" if not problems else "REJECTED"))
|
||||
sys.exit(1 if problems else 0)
|
||||
-213
@@ -1,213 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Packs an APK without aapt: binary AndroidManifest.xml, a tiny resources.arsc
|
||||
(icon + app name), classes.dex. Output is unsigned; build.sh signs and aligns it.
|
||||
|
||||
usage: mkapk.py AndroidManifest.xml classes.dex icon.png out.apk
|
||||
"""
|
||||
import struct
|
||||
import sys
|
||||
import zipfile
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
ANDROID_NS = "http://schemas.android.com/apk/res/android"
|
||||
|
||||
# android.R.attr ids (stable since API 1-4)
|
||||
ATTR_IDS = {
|
||||
"theme": 0x01010000, "label": 0x01010001, "icon": 0x01010002, "name": 0x01010003,
|
||||
"permission": 0x01010006, "enabled": 0x0101000e, "exported": 0x01010010,
|
||||
"configChanges": 0x0101001f, "minSdkVersion": 0x0101020c, "versionCode": 0x0101021b,
|
||||
"versionName": 0x0101021c, "windowSoftInputMode": 0x0101022b,
|
||||
"targetSdkVersion": 0x01010270, "installLocation": 0x010102b7,
|
||||
}
|
||||
INT_ATTRS = {"versionCode", "minSdkVersion", "targetSdkVersion"}
|
||||
BOOL_ATTRS = {"exported", "enabled"}
|
||||
FLAGS = {
|
||||
"configChanges": {"mcc": 1, "mnc": 2, "locale": 4, "touchscreen": 8, "keyboard": 0x10,
|
||||
"keyboardHidden": 0x20, "navigation": 0x40, "orientation": 0x80,
|
||||
"screenLayout": 0x100, "uiMode": 0x200, "fontScale": 0x40000000},
|
||||
"windowSoftInputMode": {"stateUnspecified": 0, "stateHidden": 2, "stateAlwaysHidden": 3,
|
||||
"adjustResize": 0x10, "adjustPan": 0x20},
|
||||
"installLocation": {"auto": 0, "internalOnly": 1, "preferExternal": 2},
|
||||
}
|
||||
|
||||
T_REF, T_STRING, T_INT_DEC, T_INT_HEX, T_BOOL = 0x01, 0x03, 0x10, 0x11, 0x12
|
||||
|
||||
# Our own resources: type id -> (type name, [(key, value)])
|
||||
RES_TYPES = [("drawable", [("icon", "res/drawable/icon.png")]),
|
||||
("string", [("app_name", "GingerCal")])]
|
||||
|
||||
|
||||
def res_id(ref):
|
||||
typ, key = ref.lstrip("@").split("/")
|
||||
for ti, (tname, entries) in enumerate(RES_TYPES):
|
||||
for ei, (k, _) in enumerate(entries):
|
||||
if tname == typ and k == key:
|
||||
return 0x7F000000 | ((ti + 1) << 16) | ei
|
||||
raise SystemExit("unknown resource " + ref)
|
||||
|
||||
|
||||
# ------------------------------------------------------------- string pool
|
||||
|
||||
def string_pool(strings):
|
||||
"""UTF-16 ResStringPool (understood by every Android version)."""
|
||||
offsets, data = [], b""
|
||||
for s in strings:
|
||||
offsets.append(len(data))
|
||||
u = s.encode("utf-16-le")
|
||||
n = len(u) // 2
|
||||
if n > 0x7FFF:
|
||||
raise SystemExit("string too long")
|
||||
data += struct.pack("<H", n) + u + b"\0\0"
|
||||
while len(data) % 4:
|
||||
data += b"\0"
|
||||
header = 28
|
||||
strings_start = header + 4 * len(strings)
|
||||
size = strings_start + len(data)
|
||||
return (struct.pack("<HHIIIIII", 0x0001, header, size, len(strings), 0, 0, strings_start, 0)
|
||||
+ b"".join(struct.pack("<I", o) for o in offsets) + data)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- AXML
|
||||
|
||||
def axml(manifest_path):
|
||||
tree = ET.parse(manifest_path)
|
||||
root = tree.getroot()
|
||||
|
||||
# collect strings: attribute names with resource ids first (resource map order)
|
||||
attr_names, other = [], []
|
||||
|
||||
def walk(e):
|
||||
for k in e.attrib:
|
||||
ns, local = split(k)
|
||||
if ns == ANDROID_NS:
|
||||
if local not in ATTR_IDS:
|
||||
raise SystemExit("add resource id for android:" + local)
|
||||
if local not in attr_names:
|
||||
attr_names.append(local)
|
||||
for c in e:
|
||||
walk(c)
|
||||
|
||||
def split(k):
|
||||
if k.startswith("{"):
|
||||
ns, local = k[1:].split("}")
|
||||
return ns, local
|
||||
return None, k
|
||||
|
||||
walk(root)
|
||||
attr_names.sort(key=lambda n: ATTR_IDS[n])
|
||||
strings = list(attr_names)
|
||||
|
||||
def idx(s):
|
||||
if s not in strings:
|
||||
strings.append(s)
|
||||
return strings.index(s)
|
||||
|
||||
idx("android")
|
||||
idx(ANDROID_NS)
|
||||
|
||||
events = [] # built after strings are known; strings may grow while encoding
|
||||
|
||||
def encode_value(local, ns, v):
|
||||
if v.startswith("@") and "/" in v and not v.startswith("@android:"):
|
||||
return 0xFFFFFFFF, T_REF, res_id(v)
|
||||
if ns == ANDROID_NS and local in INT_ATTRS:
|
||||
return 0xFFFFFFFF, T_INT_DEC, int(v)
|
||||
if ns == ANDROID_NS and local in BOOL_ATTRS:
|
||||
return 0xFFFFFFFF, T_BOOL, 0xFFFFFFFF if v == "true" else 0
|
||||
if ns == ANDROID_NS and local in FLAGS:
|
||||
n = 0
|
||||
for part in v.split("|"):
|
||||
n |= FLAGS[local][part.strip()]
|
||||
return 0xFFFFFFFF, T_INT_HEX, n
|
||||
si = idx(v)
|
||||
return si, T_STRING, si
|
||||
|
||||
def element(e, line):
|
||||
ns, local = split(e.tag)
|
||||
attrs = []
|
||||
for k, v in e.attrib.items():
|
||||
ans, alocal = split(k)
|
||||
raw, typ, data = encode_value(alocal, ans, v)
|
||||
attrs.append((ATTR_IDS.get(alocal, 0) if ans == ANDROID_NS else 0,
|
||||
idx(ANDROID_NS) if ans == ANDROID_NS else 0xFFFFFFFF, idx(alocal), raw, typ, data))
|
||||
attrs.sort(key=lambda a: a[0]) # Android looks attributes up in resource-id order
|
||||
name = idx(local)
|
||||
body = struct.pack("<IIHHHHHH", 0xFFFFFFFF, name, 20, 20, len(attrs), 0, 0, 0)
|
||||
for _, ans, an, raw, typ, data in attrs:
|
||||
body += struct.pack("<IIIHBBI", ans, an, raw, 8, 0, typ, data & 0xFFFFFFFF)
|
||||
events.append(struct.pack("<HHIII", 0x0102, 16, 16 + len(body), line, 0xFFFFFFFF) + body)
|
||||
for c in e:
|
||||
line = element(c, line + 1)
|
||||
events.append(struct.pack("<HHIIIII", 0x0103, 16, 24, line, 0xFFFFFFFF, 0xFFFFFFFF, name))
|
||||
return line + 1
|
||||
|
||||
ns_chunk = lambda t: struct.pack("<HHIIIII", t, 16, 24, 1, 0xFFFFFFFF, idx("android"), idx(ANDROID_NS))
|
||||
events.append(ns_chunk(0x0100))
|
||||
element(root, 2)
|
||||
events.append(ns_chunk(0x0101))
|
||||
|
||||
pool = string_pool(strings)
|
||||
resmap = struct.pack("<HHI", 0x0180, 8, 8 + 4 * len(attr_names)) + b"".join(
|
||||
struct.pack("<I", ATTR_IDS[n]) for n in attr_names)
|
||||
body = pool + resmap + b"".join(events)
|
||||
return struct.pack("<HHI", 0x0003, 8, 8 + len(body)) + body
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- ARSC
|
||||
|
||||
def arsc(package):
|
||||
values = [v for _, entries in RES_TYPES for _, v in entries]
|
||||
global_pool = string_pool(values)
|
||||
type_pool = string_pool([t for t, _ in RES_TYPES])
|
||||
key_pool = string_pool([k for _, entries in RES_TYPES for k, _ in entries])
|
||||
|
||||
chunks = b""
|
||||
vi = 0
|
||||
ki = 0
|
||||
for ti, (tname, entries) in enumerate(RES_TYPES):
|
||||
tid = ti + 1
|
||||
n = len(entries)
|
||||
spec = struct.pack("<HHIBBHI", 0x0202, 16, 16 + 4 * n, tid, 0, 0, n) + b"\0\0\0\0" * n
|
||||
config = struct.pack("<I", 32) + b"\0" * 28 # default configuration
|
||||
header = 20 + len(config)
|
||||
entry_data = b""
|
||||
offsets = b""
|
||||
for _ in entries:
|
||||
offsets += struct.pack("<I", len(entry_data))
|
||||
entry_data += struct.pack("<HHI", 8, 0, ki) + struct.pack("<HBBI", 8, 0, T_STRING, vi)
|
||||
ki += 1
|
||||
vi += 1
|
||||
entries_start = header + 4 * n
|
||||
typ = struct.pack("<HHIBBHII", 0x0201, header, entries_start + len(entry_data), tid, 0, 0, n,
|
||||
entries_start) + config + offsets + entry_data
|
||||
chunks += spec + typ
|
||||
|
||||
pkg_header = 284 # pre-Lollipop ResTable_package (no typeIdOffset)
|
||||
name = package.encode("utf-16-le")[:254].ljust(256, b"\0")
|
||||
type_off = pkg_header
|
||||
key_off = type_off + len(type_pool)
|
||||
pkg_size = key_off + len(key_pool) + len(chunks)
|
||||
pkg = struct.pack("<HHII", 0x0200, pkg_header, pkg_size, 0x7F) + name + struct.pack(
|
||||
"<IIII", type_off, len(RES_TYPES), key_off, ki) + type_pool + key_pool + chunks
|
||||
assert len(struct.pack("<HHII", 0, 0, 0, 0) + name + struct.pack("<IIII", 0, 0, 0, 0)) == pkg_header
|
||||
body = global_pool + pkg
|
||||
return struct.pack("<HHII", 0x0002, 12, 12 + len(body), 1) + body
|
||||
|
||||
|
||||
def main():
|
||||
manifest, dex, icon, out = sys.argv[1:5]
|
||||
package = ET.parse(manifest).getroot().attrib["package"]
|
||||
with zipfile.ZipFile(out, "w") as z:
|
||||
z.writestr(zipfile.ZipInfo("AndroidManifest.xml", (2026, 1, 1, 0, 0, 0)), axml(manifest),
|
||||
compress_type=zipfile.ZIP_DEFLATED)
|
||||
z.writestr(zipfile.ZipInfo("classes.dex", (2026, 1, 1, 0, 0, 0)), open(dex, "rb").read(),
|
||||
compress_type=zipfile.ZIP_DEFLATED)
|
||||
z.writestr(zipfile.ZipInfo("resources.arsc", (2026, 1, 1, 0, 0, 0)), arsc(package),
|
||||
compress_type=zipfile.ZIP_STORED)
|
||||
z.writestr(zipfile.ZipInfo("res/drawable/icon.png", (2026, 1, 1, 0, 0, 0)), open(icon, "rb").read(),
|
||||
compress_type=zipfile.ZIP_STORED)
|
||||
print("wrote", out)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+4
-2
@@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Draws the 48x48 (mdpi) launcher icon: a little calendar page in ginger tones."""
|
||||
"""Draws the launcher icon: a little calendar page in ginger tones.
|
||||
usage: mkicon.py out.png [size] (48 = mdpi, 36 = ldpi like the Galaxy Mini)"""
|
||||
import sys
|
||||
from PIL import Image, ImageDraw
|
||||
|
||||
@@ -27,5 +28,6 @@ for row in range(3):
|
||||
d.rounded_rectangle(box, radius=1.5 * S, fill=(0, 130, 201, 255)) # Nextcloud blue
|
||||
else:
|
||||
d.rounded_rectangle(box, radius=1.5 * S, fill=(200, 188, 172, 255))
|
||||
img = img.resize((48, 48), Image.LANCZOS)
|
||||
size = int(sys.argv[2]) if len(sys.argv) > 2 else 48
|
||||
img = img.resize((size, size), Image.LANCZOS)
|
||||
img.save(sys.argv[1] if len(sys.argv) > 1 else "icon.png", optimize=True)
|
||||
+9
-5
@@ -1,6 +1,8 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Minimal zipalign: stored entries start on 4-byte boundaries. Keeps v1 (JAR) signatures valid,
|
||||
since those cover file contents, not the zip layout. usage: zipalign.py in.apk out.apk"""
|
||||
"""Minimal zipalign: uncompressed entries start on 4-byte boundaries (like the SDK's
|
||||
`zipalign 4`). Safe after v1 (JAR) signing, which covers entry contents, not zip layout.
|
||||
usage: zipalign.py in.apk out.apk"""
|
||||
import struct
|
||||
import sys
|
||||
import zipfile
|
||||
|
||||
@@ -16,9 +18,11 @@ with zipfile.ZipFile(src) as zin, open(dst, "wb") as f:
|
||||
header_end = f.tell() + 30 + len(info.filename.encode("utf-8"))
|
||||
out.extra = b"\0" * ((4 - header_end % 4) % 4)
|
||||
zout.writestr(out, data)
|
||||
with zipfile.ZipFile(dst) as z:
|
||||
|
||||
with open(dst, "rb") as raw, zipfile.ZipFile(dst) as z:
|
||||
for info in z.infolist():
|
||||
if info.compress_type == zipfile.ZIP_STORED:
|
||||
start = info.header_offset + 30 + len(info.filename.encode("utf-8")) + len(info.extra)
|
||||
assert start % 4 == 0, info.filename
|
||||
raw.seek(info.header_offset + 26)
|
||||
n, m = struct.unpack("<HH", raw.read(4))
|
||||
assert (info.header_offset + 30 + n + m) % 4 == 0, info.filename
|
||||
print("aligned", dst)
|
||||
Reference in new issue
Block a user