@threenative/runtime-native 0.3.2 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +236 -1
- package/android/app/build.gradle.kts +37 -2
- package/android/app/src/main/java/com/mystral/engine/MystralActivity.java +97 -1
- package/android/app/src/main/java/com/mystral/engine/TnUiOverlay.java +170 -8
- package/android/build.gradle.kts +1 -1
- package/android/gradle/wrapper/gradle-wrapper.properties +2 -2
- package/package.json +17 -4
- package/scripts/asset-manifest.mjs +209 -0
- package/scripts/asset-preflight.mjs +137 -6
- package/scripts/bundle.mjs +25 -5
- package/scripts/check-android-16kb-alignment.mjs +413 -0
- package/scripts/desktop-distribution.mjs +1084 -0
- package/scripts/inspect-container-brand.mjs +748 -0
- package/scripts/inspect-macos-icon.mjs +169 -0
- package/scripts/install-prebuilt.mjs +6 -3
- package/scripts/package-android.mjs +478 -32
- package/scripts/package-desktop.mjs +191 -15
- package/scripts/package-ios.mjs +83 -41
- package/scripts/physical-device-evidence.mjs +135 -1
- package/scripts/production-evidence.mjs +43 -12
- package/scripts/profile-production.mjs +735 -159
- package/scripts/qualify-physical-mobile.mjs +286 -69
- package/scripts/verify-starter-consumer.mjs +782 -0
- package/scripts/verify-starter-desktop-base.mjs +458 -0
- package/scripts/verify-starter-desktop.mjs +27 -220
- package/scripts/verify-windows-installer.mjs +63 -0
- package/scripts/windows-installer.mjs +181 -0
- package/scripts/windows-installer.nsi +114 -0
- package/scripts/xvfb.sh +48 -1
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { extname, join, resolve } from 'node:path';
|
|
5
|
+
import { pathToFileURL } from 'node:url';
|
|
6
|
+
import { parseArgs } from 'node:util';
|
|
7
|
+
import { inflateRawSync } from 'node:zlib';
|
|
8
|
+
|
|
9
|
+
export const ANDROID_16KB_ABIS = Object.freeze(['arm64-v8a', 'x86_64']);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Where to find an objdump that can read an arm64 shared object.
|
|
13
|
+
*
|
|
14
|
+
* `llvm-objdump` is not on a GitHub Ubuntu runner's PATH, so the check failed to *start* and the
|
|
15
|
+
* Android lane reported it as "Failed to download v8-android" — a tool that was missing, read as
|
|
16
|
+
* a dependency that was broken. The NDK ships one, and any job that reaches this check is
|
|
17
|
+
* building for Android, so it has an NDK. Look there before PATH.
|
|
18
|
+
*
|
|
19
|
+
* Ubuntu's GNU `objdump` prints the same `align 2**N` field this module parses, but its BFD is
|
|
20
|
+
* usually configured for the host target only and refuses an arm64 object, so it is the last
|
|
21
|
+
* resort rather than the first.
|
|
22
|
+
*/
|
|
23
|
+
export function resolveObjdumpCandidates(env = process.env) {
|
|
24
|
+
const candidates = [];
|
|
25
|
+
if (env.TN_LLVM_OBJDUMP) candidates.push(env.TN_LLVM_OBJDUMP);
|
|
26
|
+
|
|
27
|
+
const ndkRoots = [env.ANDROID_NDK_HOME, env.ANDROID_NDK_ROOT, env.ANDROID_NDK].filter(Boolean);
|
|
28
|
+
const sdkRoot = env.ANDROID_HOME ?? env.ANDROID_SDK_ROOT;
|
|
29
|
+
if (sdkRoot && existsSync(join(sdkRoot, 'ndk'))) {
|
|
30
|
+
try {
|
|
31
|
+
// Newest NDK first: the directory names are versions, and a newer llvm-objdump reads
|
|
32
|
+
// everything an older one does.
|
|
33
|
+
for (const version of readdirSync(join(sdkRoot, 'ndk')).sort().reverse())
|
|
34
|
+
ndkRoots.push(join(sdkRoot, 'ndk', version));
|
|
35
|
+
} catch {
|
|
36
|
+
// An unreadable SDK directory is not this check's problem; fall through to PATH.
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
for (const ndk of ndkRoots) {
|
|
40
|
+
const prebuilt = join(ndk, 'toolchains', 'llvm', 'prebuilt');
|
|
41
|
+
if (!existsSync(prebuilt)) continue;
|
|
42
|
+
try {
|
|
43
|
+
for (const host of readdirSync(prebuilt))
|
|
44
|
+
candidates.push(join(prebuilt, host, 'bin', 'llvm-objdump'));
|
|
45
|
+
} catch {
|
|
46
|
+
// Same: an unreadable toolchain directory just means this candidate does not exist.
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
candidates.push('llvm-objdump', 'objdump');
|
|
51
|
+
return candidates.filter((candidate, index) => candidates.indexOf(candidate) === index);
|
|
52
|
+
}
|
|
53
|
+
export const ANDROID_16KB_ALIGNMENT = 2 ** 14;
|
|
54
|
+
|
|
55
|
+
function alignmentDescription(alignment) {
|
|
56
|
+
return `0x${alignment.toString(16)} (2**${Math.log2(alignment)})`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function parseLoadSegmentAlignments(output, libraryPath = '<library>') {
|
|
60
|
+
const alignments = [];
|
|
61
|
+
for (const line of output.split(/\r?\n/u)) {
|
|
62
|
+
if (!/^\s*LOAD\b/u.test(line)) continue;
|
|
63
|
+
const match = /\balign\s+(?:2\*\*(\d+)|0x([0-9a-f]+))\b/iu.exec(line);
|
|
64
|
+
if (!match) {
|
|
65
|
+
throw new Error(`Android 16 KB alignment check could not read LOAD alignment for ${libraryPath}`);
|
|
66
|
+
}
|
|
67
|
+
const alignment = match[1] ? 2 ** Number(match[1]) : Number.parseInt(match[2], 16);
|
|
68
|
+
if (!Number.isSafeInteger(alignment) || alignment <= 0) {
|
|
69
|
+
throw new Error(`Android 16 KB alignment check read an invalid LOAD alignment for ${libraryPath}`);
|
|
70
|
+
}
|
|
71
|
+
alignments.push(alignment);
|
|
72
|
+
}
|
|
73
|
+
if (alignments.length === 0) {
|
|
74
|
+
throw new Error(`Android 16 KB alignment check found no LOAD segments in ${libraryPath}`);
|
|
75
|
+
}
|
|
76
|
+
return alignments;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function assertAndroid16KbAlignment(libraries, options = {}) {
|
|
80
|
+
if (!Array.isArray(libraries) || libraries.length === 0) {
|
|
81
|
+
throw new Error('Android 16 KB alignment check requires at least one shared library');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const candidates = options.objdump ? [options.objdump] : resolveObjdumpCandidates();
|
|
85
|
+
const runObjdump = options.runObjdump ?? ((libraryPath) => {
|
|
86
|
+
const failures = [];
|
|
87
|
+
for (const candidate of candidates) {
|
|
88
|
+
try {
|
|
89
|
+
return execFileSync(candidate, ['-p', libraryPath], { encoding: 'utf8' });
|
|
90
|
+
} catch (error) {
|
|
91
|
+
failures.push(`${candidate}: ${error instanceof Error ? error.message : String(error)}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
// Name every candidate that was tried. "llvm-objdump ENOENT" alone did not say that three
|
|
95
|
+
// other places had been looked at, which is the first thing the reader needs.
|
|
96
|
+
throw new Error(`no usable objdump (tried ${failures.join('; ')})`);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
return libraries.map((libraryPath) => {
|
|
100
|
+
let output;
|
|
101
|
+
try {
|
|
102
|
+
output = runObjdump(libraryPath);
|
|
103
|
+
} catch (error) {
|
|
104
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
105
|
+
throw new Error(`Android 16 KB alignment check could not inspect ${libraryPath}: ${reason}`, {
|
|
106
|
+
cause: error,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const alignments = parseLoadSegmentAlignments(output, libraryPath);
|
|
111
|
+
const invalid = alignments.filter((alignment) => alignment < ANDROID_16KB_ALIGNMENT);
|
|
112
|
+
if (invalid.length > 0) {
|
|
113
|
+
const observed = alignments.map(alignmentDescription).join(', ');
|
|
114
|
+
const error = new Error(
|
|
115
|
+
`Android 16 KB alignment check failed for ${libraryPath}: ` +
|
|
116
|
+
`LOAD alignments ${observed}; expected every segment >= 0x4000 (2**14)`,
|
|
117
|
+
);
|
|
118
|
+
// Distinguish "this library is misaligned" from "the check could not run". They are
|
|
119
|
+
// different events with different owners, and only the caller knows whether a given
|
|
120
|
+
// library is one this repository builds or one it pins.
|
|
121
|
+
error.code = 'ANDROID_16KB_MISALIGNED';
|
|
122
|
+
error.alignments = alignments;
|
|
123
|
+
throw error;
|
|
124
|
+
}
|
|
125
|
+
return { libraryPath, alignments };
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The final-artifact census.
|
|
131
|
+
*
|
|
132
|
+
* An ELF check over the build directory proves nothing about what shipped: the packaged APK is a
|
|
133
|
+
* different set of bytes, assembled by Gradle from prebuilts, AARs and transitive dependencies that
|
|
134
|
+
* the runtime checkout never names. The library that broke 16 KB devices was always one nobody
|
|
135
|
+
* listed. So the census enumerates what the archive actually contains and refuses to credit an
|
|
136
|
+
* inspection that covered nothing.
|
|
137
|
+
*
|
|
138
|
+
* Two independent alignment facts live in an APK and both are required:
|
|
139
|
+
*
|
|
140
|
+
* 1. **ELF LOAD alignment** — each `lib/<abi>/*.so` must carry 2**14 segments, the check above.
|
|
141
|
+
* 2. **ZIP alignment** — a library stored uncompressed (`extractNativeLibs="false"`, which is the
|
|
142
|
+
* default since AGP 4.2) is mapped straight out of the APK, so its *data offset inside the
|
|
143
|
+
* archive* must also be 16 KB aligned. A perfectly aligned ELF at a 4 KB archive offset still
|
|
144
|
+
* fails to load on a 16 KB device.
|
|
145
|
+
*/
|
|
146
|
+
|
|
147
|
+
const ZIP_END_OF_CENTRAL_DIRECTORY = 0x06054b50;
|
|
148
|
+
const ZIP_CENTRAL_FILE_HEADER = 0x02014b50;
|
|
149
|
+
const ZIP_LOCAL_FILE_HEADER = 0x04034b50;
|
|
150
|
+
const ZIP_STORED = 0;
|
|
151
|
+
const ZIP_DEFLATED = 8;
|
|
152
|
+
|
|
153
|
+
/** `lib/<abi>/<name>.so` — the only entries Android maps as native code. */
|
|
154
|
+
export const ANDROID_LIBRARY_ENTRY = /^lib\/([^/]+)\/([^/]+\.so)$/u;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Read an archive's entries from its central directory.
|
|
158
|
+
*
|
|
159
|
+
* Deliberately not shelling out to `unzip -l`: the data offset of each entry is the number this
|
|
160
|
+
* check exists to read, and no listing tool prints it.
|
|
161
|
+
*/
|
|
162
|
+
export function readZipEntries(archivePath) {
|
|
163
|
+
const bytes = readFileSync(archivePath);
|
|
164
|
+
let end = -1;
|
|
165
|
+
// The EOCD is last, but a trailing comment can push it back up to 64 KB.
|
|
166
|
+
for (let offset = bytes.length - 22; offset >= 0 && offset >= bytes.length - 22 - 0xffff; offset -= 1) {
|
|
167
|
+
if (bytes.readUInt32LE(offset) === ZIP_END_OF_CENTRAL_DIRECTORY) {
|
|
168
|
+
end = offset;
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (end === -1) {
|
|
173
|
+
throw new Error(`Android artifact ${archivePath} is not a ZIP archive: no end-of-central-directory record`);
|
|
174
|
+
}
|
|
175
|
+
const count = bytes.readUInt16LE(end + 10);
|
|
176
|
+
let cursor = bytes.readUInt32LE(end + 16);
|
|
177
|
+
const entries = [];
|
|
178
|
+
for (let index = 0; index < count; index += 1) {
|
|
179
|
+
if (bytes.readUInt32LE(cursor) !== ZIP_CENTRAL_FILE_HEADER) {
|
|
180
|
+
throw new Error(`Android artifact ${archivePath} has a corrupt central directory at entry ${index}`);
|
|
181
|
+
}
|
|
182
|
+
const compression = bytes.readUInt16LE(cursor + 10);
|
|
183
|
+
const compressedSize = bytes.readUInt32LE(cursor + 20);
|
|
184
|
+
const size = bytes.readUInt32LE(cursor + 24);
|
|
185
|
+
const nameLength = bytes.readUInt16LE(cursor + 28);
|
|
186
|
+
const extraLength = bytes.readUInt16LE(cursor + 30);
|
|
187
|
+
const commentLength = bytes.readUInt16LE(cursor + 32);
|
|
188
|
+
const localHeaderOffset = bytes.readUInt32LE(cursor + 42);
|
|
189
|
+
const name = bytes.toString('utf8', cursor + 46, cursor + 46 + nameLength);
|
|
190
|
+
if (bytes.readUInt32LE(localHeaderOffset) !== ZIP_LOCAL_FILE_HEADER) {
|
|
191
|
+
throw new Error(`Android artifact ${archivePath} has a corrupt local header for ${name}`);
|
|
192
|
+
}
|
|
193
|
+
// The local header repeats the name and carries its *own* extra field, which is what zipalign
|
|
194
|
+
// pads. Read the length from there; the central directory's copy is a different value.
|
|
195
|
+
const dataOffset =
|
|
196
|
+
localHeaderOffset +
|
|
197
|
+
30 +
|
|
198
|
+
bytes.readUInt16LE(localHeaderOffset + 26) +
|
|
199
|
+
bytes.readUInt16LE(localHeaderOffset + 28);
|
|
200
|
+
entries.push({ name, compression, size, compressedSize, dataOffset });
|
|
201
|
+
cursor += 46 + nameLength + extraLength + commentLength;
|
|
202
|
+
}
|
|
203
|
+
return { bytes, entries };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Group the packaged libraries by ABI and refuse a census that proves nothing.
|
|
208
|
+
*
|
|
209
|
+
* An empty result is the failure this function exists for. Reporting "0 libraries inspected, all
|
|
210
|
+
* aligned" is how an uninspected dependency gets credited as compatible.
|
|
211
|
+
*/
|
|
212
|
+
export function androidArtifactLibraryCensus(entries, { abis = ANDROID_16KB_ABIS, artifactPath = '<artifact>' } = {}) {
|
|
213
|
+
const census = new Map(abis.map((abi) => [abi, []]));
|
|
214
|
+
const unexpected = [];
|
|
215
|
+
for (const entry of entries) {
|
|
216
|
+
const match = ANDROID_LIBRARY_ENTRY.exec(entry.name);
|
|
217
|
+
if (!match) continue;
|
|
218
|
+
const [, abi] = match;
|
|
219
|
+
if (census.has(abi)) census.get(abi).push(entry);
|
|
220
|
+
else unexpected.push(entry.name);
|
|
221
|
+
}
|
|
222
|
+
if (unexpected.length > 0) {
|
|
223
|
+
// A third ABI is not a harmless extra: it ships to devices and nothing above declared it.
|
|
224
|
+
throw new Error(
|
|
225
|
+
`Android 16 KB census found libraries for undeclared ABIs in ${artifactPath}: ${unexpected.join(', ')}`,
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
const empty = [...census].filter(([, found]) => found.length === 0).map(([abi]) => abi);
|
|
229
|
+
if (empty.length > 0) {
|
|
230
|
+
throw new Error(
|
|
231
|
+
`Android 16 KB census found no native libraries for ${empty.join(', ')} in ${artifactPath}; ` +
|
|
232
|
+
'an artifact with an uninspected or omitted ABI cannot be credited as 16 KB compatible',
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
return census;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Where to find the SDK's `zipalign`, the official archive-alignment tool. */
|
|
239
|
+
export function resolveZipalignCandidates(env = process.env) {
|
|
240
|
+
const candidates = [];
|
|
241
|
+
if (env.TN_ZIPALIGN) candidates.push(env.TN_ZIPALIGN);
|
|
242
|
+
const sdkRoot = env.ANDROID_HOME ?? env.ANDROID_SDK_ROOT;
|
|
243
|
+
if (sdkRoot && existsSync(join(sdkRoot, 'build-tools'))) {
|
|
244
|
+
try {
|
|
245
|
+
for (const version of readdirSync(join(sdkRoot, 'build-tools')).sort().reverse())
|
|
246
|
+
candidates.push(join(sdkRoot, 'build-tools', version, 'zipalign'));
|
|
247
|
+
} catch {
|
|
248
|
+
// An unreadable build-tools directory just means this candidate does not exist.
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
candidates.push('zipalign');
|
|
252
|
+
return candidates.filter((candidate, index) => candidates.indexOf(candidate) === index);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Corroborate the archive offsets above with the SDK's own `zipalign -c -P 16`.
|
|
257
|
+
*
|
|
258
|
+
* The parser is authoritative — it always runs, and a check that can be skipped is not a gate. But
|
|
259
|
+
* an independent implementation of the same rule is cheap where the SDK is installed, and a
|
|
260
|
+
* disagreement between the two is worth failing on. Returns the tool that confirmed, or `null`
|
|
261
|
+
* when no `zipalign` is installed.
|
|
262
|
+
*/
|
|
263
|
+
export function verifyArchiveAlignmentWithZipalign(artifactPath, options = {}) {
|
|
264
|
+
const candidates = options.zipalign ? [options.zipalign] : resolveZipalignCandidates();
|
|
265
|
+
const run =
|
|
266
|
+
options.runZipalign ??
|
|
267
|
+
((tool) => execFileSync(tool, ['-c', '-P', '16', '-v', '4', artifactPath], { encoding: 'utf8' }));
|
|
268
|
+
for (const candidate of candidates) {
|
|
269
|
+
try {
|
|
270
|
+
run(candidate);
|
|
271
|
+
return candidate;
|
|
272
|
+
} catch (error) {
|
|
273
|
+
// A tool that ran and disagreed is a verdict; a tool that is not installed is not.
|
|
274
|
+
if (typeof error?.status === 'number') {
|
|
275
|
+
const detail = String(error.stdout ?? '')
|
|
276
|
+
.split(/\r?\n/u)
|
|
277
|
+
.filter((line) => /BAD|FAIL/iu.test(line))
|
|
278
|
+
.slice(0, 8)
|
|
279
|
+
.join('; ');
|
|
280
|
+
const failure = new Error(
|
|
281
|
+
`Android 16 KB alignment check failed for ${artifactPath}: ` +
|
|
282
|
+
`${candidate} -c -P 16 exited ${error.status}${detail ? ` (${detail})` : ''}`,
|
|
283
|
+
);
|
|
284
|
+
failure.code = 'ANDROID_16KB_MISALIGNED';
|
|
285
|
+
throw failure;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Inspect a finished Android artifact: every packaged library, on every declared ABI, for both
|
|
294
|
+
* ELF LOAD alignment and uncompressed archive alignment.
|
|
295
|
+
*
|
|
296
|
+
* `options` reaches both halves: `runObjdump`/`objdump` select the ELF reader, and `zipalign:
|
|
297
|
+
* false` suppresses the SDK corroboration for callers that have no SDK.
|
|
298
|
+
*
|
|
299
|
+
* Returns the inspected libraries — so the caller can print what was actually covered — plus the
|
|
300
|
+
* `zipalign` binary that corroborated the archive offsets, or `null` where none is installed.
|
|
301
|
+
* Throws on the first misaligned library, on an omitted ABI, and on an archive it cannot read.
|
|
302
|
+
*/
|
|
303
|
+
export function assertAndroidArtifact16KbAlignment(artifactPath, options = {}) {
|
|
304
|
+
if (!existsSync(artifactPath)) {
|
|
305
|
+
throw new Error(`Android 16 KB artifact check cannot read ${artifactPath}`);
|
|
306
|
+
}
|
|
307
|
+
const { bytes, entries } = readZipEntries(artifactPath);
|
|
308
|
+
const census = androidArtifactLibraryCensus(entries, { ...options, artifactPath });
|
|
309
|
+
// Archive geometry first, then ELF contents. An uncompressed library is mapped straight out of
|
|
310
|
+
// the APK, so its offset inside the archive is part of the contract; a deflated one is unpacked
|
|
311
|
+
// to the filesystem at install and is not. Reading every offset before extracting anything also
|
|
312
|
+
// keeps the reported failure independent of the order entries happen to sit in.
|
|
313
|
+
for (const [, libraries] of census) {
|
|
314
|
+
for (const entry of libraries) {
|
|
315
|
+
if (entry.compression === ZIP_STORED && entry.dataOffset % ANDROID_16KB_ALIGNMENT !== 0) {
|
|
316
|
+
const error = new Error(
|
|
317
|
+
`Android 16 KB alignment check failed for ${artifactPath}!${entry.name}: ` +
|
|
318
|
+
`uncompressed library stored at archive offset 0x${entry.dataOffset.toString(16)}, ` +
|
|
319
|
+
'which is not a multiple of 0x4000',
|
|
320
|
+
);
|
|
321
|
+
error.code = 'ANDROID_16KB_MISALIGNED';
|
|
322
|
+
throw error;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
const scratch = mkdtempSync(join(tmpdir(), 'tn-16kb-artifact-'));
|
|
327
|
+
try {
|
|
328
|
+
const inspected = [];
|
|
329
|
+
for (const [abi, libraries] of census) {
|
|
330
|
+
for (const entry of libraries) {
|
|
331
|
+
const extracted = join(scratch, `${abi}-${entry.name.split('/').pop()}`);
|
|
332
|
+
const raw = bytes.subarray(entry.dataOffset, entry.dataOffset + entry.compressedSize);
|
|
333
|
+
if (entry.compression === ZIP_STORED) writeFileSync(extracted, raw);
|
|
334
|
+
else if (entry.compression === ZIP_DEFLATED) writeFileSync(extracted, inflateRawSync(raw));
|
|
335
|
+
else {
|
|
336
|
+
throw new Error(
|
|
337
|
+
`Android 16 KB alignment check cannot read ${entry.name} in ${artifactPath}: ` +
|
|
338
|
+
`unsupported ZIP compression method ${entry.compression}`,
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
const [result] = assertAndroid16KbAlignment([extracted], options);
|
|
342
|
+
inspected.push({
|
|
343
|
+
abi,
|
|
344
|
+
entry: entry.name,
|
|
345
|
+
compression: entry.compression === ZIP_STORED ? 'stored' : 'deflated',
|
|
346
|
+
dataOffset: entry.dataOffset,
|
|
347
|
+
alignments: result.alignments,
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
const zipalign = options.zipalign === false ? null : verifyArchiveAlignmentWithZipalign(artifactPath, options);
|
|
352
|
+
return { artifactPath, libraries: inspected, zipalign };
|
|
353
|
+
} finally {
|
|
354
|
+
rmSync(scratch, { force: true, recursive: true });
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** Check the APK Google bundletool actually produces, not the AAB's compressed library entries. */
|
|
359
|
+
export function assertAndroidBundle16KbAlignment(artifactPath, bundletoolPath, options = {}) {
|
|
360
|
+
if (!bundletoolPath || !existsSync(bundletoolPath)) {
|
|
361
|
+
throw new Error('AAB verification requires --bundletool /path/to/bundletool-all.jar');
|
|
362
|
+
}
|
|
363
|
+
const scratch = mkdtempSync(join(tmpdir(), 'tn-16kb-bundle-'));
|
|
364
|
+
try {
|
|
365
|
+
const archive = join(scratch, 'game.apks');
|
|
366
|
+
const java = process.env.JAVA_HOME
|
|
367
|
+
? join(process.env.JAVA_HOME, 'bin', process.platform === 'win32' ? 'java.exe' : 'java')
|
|
368
|
+
: 'java';
|
|
369
|
+
const args = ['-jar', resolve(bundletoolPath)];
|
|
370
|
+
execFileSync(java, [...args, 'validate', `--bundle=${resolve(artifactPath)}`], { stdio: 'inherit' });
|
|
371
|
+
// Bundletool's standard debug signing is only for inspection; the input AAB is untouched.
|
|
372
|
+
execFileSync(java, [...args, 'build-apks', `--bundle=${resolve(artifactPath)}`,
|
|
373
|
+
`--output=${archive}`, '--mode=universal'], { stdio: 'inherit' });
|
|
374
|
+
const { bytes, entries } = readZipEntries(archive);
|
|
375
|
+
const apks = entries.filter((entry) => entry.name === 'universal.apk');
|
|
376
|
+
if (apks.length !== 1) throw new Error('Bundletool output must contain exactly one universal.apk');
|
|
377
|
+
const entry = apks[0];
|
|
378
|
+
const raw = bytes.subarray(entry.dataOffset, entry.dataOffset + entry.compressedSize);
|
|
379
|
+
if (entry.compression !== ZIP_STORED && entry.compression !== ZIP_DEFLATED) {
|
|
380
|
+
throw new Error(`Unsupported universal.apk ZIP compression: ${entry.compression}`);
|
|
381
|
+
}
|
|
382
|
+
const apk = join(scratch, 'universal.apk');
|
|
383
|
+
writeFileSync(apk, entry.compression === ZIP_STORED ? raw : inflateRawSync(raw));
|
|
384
|
+
return { ...assertAndroidArtifact16KbAlignment(apk, options), artifactPath };
|
|
385
|
+
} finally {
|
|
386
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (process.argv[1] && existsSync(process.argv[1]) &&
|
|
391
|
+
import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href) {
|
|
392
|
+
try {
|
|
393
|
+
const { values, positionals } = parseArgs({
|
|
394
|
+
options: { abis: { type: 'string' }, bundletool: { type: 'string' } }, allowPositionals: true,
|
|
395
|
+
});
|
|
396
|
+
if (positionals.length !== 1 || !['.apk', '.aab'].includes(extname(positionals[0]))) {
|
|
397
|
+
throw new Error(
|
|
398
|
+
'Usage: check-android-16kb-alignment.mjs <game.apk|game.aab> [--bundletool <jar>] [--abis arm64-v8a[,x86_64]]',
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
const artifact = resolve(positionals[0]);
|
|
402
|
+
// The ABI set the artifact was built for: a release ships arm64-v8a alone. Unnamed, both
|
|
403
|
+
// 64-bit ABIs are required, so an omitted slice still fails unless the caller declares it.
|
|
404
|
+
const options = values.abis === undefined ? {} : { abis: values.abis.split(',').map((abi) => abi.trim()) };
|
|
405
|
+
const result = extname(artifact) === '.aab'
|
|
406
|
+
? assertAndroidBundle16KbAlignment(artifact, values.bundletool, options)
|
|
407
|
+
: assertAndroidArtifact16KbAlignment(artifact, options);
|
|
408
|
+
console.log(`Android 16 KB check passed: ${artifact} (${result.libraries.length} native libraries)`);
|
|
409
|
+
} catch (error) {
|
|
410
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
411
|
+
process.exitCode = 1;
|
|
412
|
+
}
|
|
413
|
+
}
|