also looking at this
fix(android): enforce the API 26 runtime contract
#301
3 files
+46
-14
Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.
| @@ -145,7 +145,11 @@ also enables foreground geolocation. Push notifications require a regular | ||
| 145 | 145 | `googleServicesFile` whose JSON contains a client matching the generated |
| 146 | 146 | Android package. Deep links require at least one valid URI scheme; scheme names |
| 147 | 147 | are normalized to lowercase. |
| 148 | Health Connect normalizes `minSdk` to at least 26 and `compileSdk` to at least 36. | |
| 148 | The generated runtime supports Android 8.0 / API 26 and newer, exported as | |
| 149 | `ANDROID_MIN_SDK`. `minSdk` defaults to that floor; lower values are rejected | |
| 150 | before project files are written, including when Health Connect is enabled. | |
| 151 | Health Connect normalizes `compileSdk` to at least 36. | |
| 152 | The same effective `minSdk` is written to Gradle and both configuration files. | |
| 149 | 153 | A custom `appIconPath` must be a regular PNG, WebP, JPG, or GIF file; its supported |
| 150 | 154 | extension is preserved in the generated drawable resource. |
| 151 | 155 | |
| @@ -1,8 +1,8 @@ | ||
| 1 | import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, writeFileSync } from 'node:fs' | |
| 1 | import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs' | |
| 2 | 2 | import { tmpdir } from 'node:os' |
| 3 | 3 | import { join } from 'node:path' |
| 4 | 4 | import { describe, expect, it } from 'bun:test' |
| 5 | import { build, init, installRuntime, renderAndroidDeepLinks, renderAndroidPermissions, resolveRuntimeDir, syncAndroidWebAssets } from './index' | |
| 5 | import { ANDROID_MIN_SDK, build, init, installRuntime, renderAndroidDeepLinks, renderAndroidPermissions, resolveRuntimeDir, syncAndroidWebAssets } from './index' | |
| 6 | 6 | |
| 7 | 7 | function generatedFiles(path: string): string[] { |
| 8 | 8 | return readdirSync(path, { withFileTypes: true }).flatMap((entry) => { |
| @@ -12,6 +12,34 @@ function generatedFiles(path: string): string[] { | ||
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | describe('Craft Android builder', () => { |
| 15 | it('rejects Android API levels below the runtime floor before writing a project', async () => { | |
| 16 | const root = mkdtempSync(join(tmpdir(), 'craft-android-sdk-floor-')) | |
| 17 | try { | |
| 18 | for (const enableHealthConnect of [false, true]) { | |
| 19 | const output = join(root, String(enableHealthConnect)) | |
| 20 | await expect(init({ name: 'OldDevice', output, runtimeDir: null, config: { minSdk: 25, enableHealthConnect } })) | |
| 21 | .rejects.toThrow('Android minSdk must be at least 26') | |
| 22 | expect(existsSync(output)).toBe(false) | |
| 23 | } | |
| 24 | } | |
| 25 | finally { rmSync(root, { recursive: true, force: true }) } | |
| 26 | }) | |
| 27 | ||
| 28 | it('keeps the default and supported Android API levels aligned across generated metadata', async () => { | |
| 29 | const root = mkdtempSync(join(tmpdir(), 'craft-android-sdk-supported-')) | |
| 30 | try { | |
| 31 | for (const minSdk of [undefined, 26, 28]) { | |
| 32 | const output = join(root, String(minSdk)) | |
| 33 | await init({ name: 'SupportedDevice', output, runtimeDir: null, config: minSdk === undefined ? {} : { minSdk } }) | |
| 34 | const expected = minSdk ?? 26 | |
| 35 | expect(JSON.parse(readFileSync(join(output, 'craft.config.json'), 'utf8')).minSdk).toBe(expected) | |
| 36 | expect(JSON.parse(readFileSync(join(output, 'app/src/main/assets/craft.config.json'), 'utf8')).minSdk).toBe(expected) | |
| 37 | expect(readFileSync(join(output, 'app/build.gradle.kts'), 'utf8')).toContain(`minSdk = ${expected}`) | |
| 38 | } | |
| 39 | } | |
| 40 | finally { rmSync(root, { recursive: true, force: true }) } | |
| 41 | }) | |
| 42 | ||
| 15 | 43 | it('routes every Kotlin template through the project generator', () => { |
| 16 | 44 | const templates = readdirSync(join(import.meta.dir, '../templates')) |
| 17 | 45 | .filter(name => name.endsWith('.kt.template')) |
| @@ -1298,7 +1326,7 @@ describe('Craft Android builder', () => { | ||
| 1298 | 1326 | name: 'WildLoop', |
| 1299 | 1327 | packageName: 'org.wildloop.app', |
| 1300 | 1328 | output, |
| 1301 | config: { compileSdk: 34, enableHealthConnect: true, minSdk: 24 }, | |
| 1329 | config: { compileSdk: 34, enableHealthConnect: true, minSdk: ANDROID_MIN_SDK }, | |
| 1302 | 1330 | }) |
| 1303 | 1331 | |
| 1304 | 1332 | const bridge = readFileSync(join(output, 'app/src/main/java/org/wildloop/app/CraftBridge.kt'), 'utf8') |
| @@ -1489,12 +1517,6 @@ describe('the Zig library and the generated app agree on how old a device may be | ||
| 1489 | 1517 | |
| 1490 | 1518 | expect(declared).not.toBeNull() |
| 1491 | 1519 | |
| 1492 | // DEFAULT_CONFIG is not exported, so read it the same way: from the source | |
| 1493 | // that defines it, which is the thing that would have to change. | |
| 1494 | const generator = readFileSync(join(import.meta.dir, 'index.ts'), 'utf8') | |
| 1495 | const minSdk = generator.match(/minSdk:\s*(\d+)/) | |
| 1496 | ||
| 1497 | expect(minSdk).not.toBeNull() | |
| 1498 | expect(Number(declared![1])).toBeLessThanOrEqual(Number(minSdk![1])) | |
| 1520 | expect(Number(declared![1])).toBeLessThanOrEqual(ANDROID_MIN_SDK) | |
| 1499 | 1521 | }) |
| 1500 | 1522 | }) |
| @@ -42,6 +42,9 @@ const KOTLIN_KEYWORDS = new Set([ | ||
| 42 | 42 | 'while', |
| 43 | 43 | ]) |
| 44 | 44 | |
| 45 | /** Oldest Android API level supported by the generated Kotlin runtime. */ | |
| 46 | export const ANDROID_MIN_SDK = 26 | |
| 47 | ||
| 45 | 48 | export interface CraftAndroidConfig { |
| 46 | 49 | appName: string |
| 47 | 50 | packageName: string |
| @@ -67,6 +70,7 @@ export interface CraftAndroidConfig { | ||
| 67 | 70 | googleServicesFile?: string |
| 68 | 71 | devServerURL?: string |
| 69 | 72 | hasBundledFallback?: boolean |
| 73 | /** Android API level, at least ANDROID_MIN_SDK (26). */ | |
| 70 | 74 | minSdk?: number |
| 71 | 75 | compileSdk?: number |
| 72 | 76 | targetSdk?: number |
| @@ -125,7 +129,7 @@ const DEFAULT_CONFIG: Omit<CraftAndroidConfig, 'appName' | 'packageName'> = { | ||
| 125 | 129 | enableDeepLinks: false, |
| 126 | 130 | urlSchemes: [], |
| 127 | 131 | trustedOrigins: [], |
| 128 | minSdk: 26, | |
| 132 | minSdk: ANDROID_MIN_SDK, | |
| 129 | 133 | compileSdk: 36, |
| 130 | 134 | targetSdk: 35, |
| 131 | 135 | } |
| @@ -258,6 +262,9 @@ function validateAndroidConfig(config: CraftAndroidConfig): void { | ||
| 258 | 262 | } |
| 259 | 263 | } |
| 260 | 264 | |
| 265 | if (Number(config.minSdk) < ANDROID_MIN_SDK) { | |
| 266 | throw new Error(`Android minSdk must be at least ${ANDROID_MIN_SDK} (Android 8.0) for the Craft runtime`) | |
| 267 | } | |
| 261 | 268 | if (Number(config.minSdk) > Number(config.targetSdk)) { |
| 262 | 269 | throw new Error('Android minSdk must not exceed targetSdk') |
| 263 | 270 | } |
| @@ -462,7 +469,6 @@ export async function init(options: InitOptions): Promise<void> { | ||
| 462 | 469 | } |
| 463 | 470 | if (config.enableBackgroundLocation) config.enableGeolocation = true |
| 464 | 471 | if (config.enableHealthConnect) { |
| 465 | config.minSdk = Math.max(config.minSdk ?? 26, 26) | |
| 466 | 472 | config.compileSdk = Math.max(config.compileSdk ?? 36, 36) |
| 467 | 473 | } |
| 468 | 474 | normalizeAndroidNetworkConfig(config) |
| @@ -614,7 +620,7 @@ export async function init(options: InitOptions): Promise<void> { | ||
| 614 | 620 | .replace(/\{\{PACKAGE_NAME\}\}/g, finalPackageName) |
| 615 | 621 | .replace(/\{\{VERSION_NAME\}\}/g, escapeKotlinString(config.version || '1.0.0')) |
| 616 | 622 | .replace(/\{\{VERSION_CODE\}\}/g, String(config.versionCode || 1)) |
| 617 | .replace(/\{\{MIN_SDK\}\}/g, String(config.minSdk || 24)) | |
| 623 | .replace(/\{\{MIN_SDK\}\}/g, String(config.minSdk ?? ANDROID_MIN_SDK)) | |
| 618 | 624 | .replace(/\{\{COMPILE_SDK\}\}/g, String(config.compileSdk || 36)) |
| 619 | 625 | .replace(/\{\{TARGET_SDK\}\}/g, String(config.targetSdk || 35)) |
| 620 | 626 | .replace(/\{\{GOOGLE_SERVICES_PLUGIN\}\}/g, hasGoogleServices ? ' id("com.google.gms.google-services")' : '') |