A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(android): a native Android app can reach the network
esengine77a9258d3e23
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 1ca3b0901..efaa2ff02 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -27,6 +27,7 @@ published separately; it ships inside the editor.### Fixed+- A native Android app can reach the network: its manifest never asked for it, so a remote group's assets, hot updates and the debug channel all failed on Android. A development build also allows plain http (a LAN CDN); a shipping build is held to https, and the export warns when its CDN is http.- A mini-game reads the host's pixel ratio once and again after a resize, instead of calling getSystemInfoSync several times a frame (on vivo, a synchronous call across its bridge each time).- `createButton({ userGesture: true })` runs its click inside the host's own release event: on a WeChat phone, sharing a recording from a button was refused ("require user interaction (only in touchend event)"), because a click ran on the frame after the tap.- Mini-game exports name assets by their content, as web exports already did: a hot update to a remote texture now shows on a phone (it kept the old picture under the same URL), and a CDN can hold two versions without v1 players getting v2 files.diff --git a/build-tools/utils/aab.js b/build-tools/utils/aab.jsindex 4fd5ed663..806c19d8e 100644--- a/build-tools/utils/aab.js+++ b/build-tools/utils/aab.js@@ -60,6 +60,7 @@ export function assembleAab(options) {VERSION_CODE: app.versionCode,SCREEN_ORIENTATION: androidScreenOrientation(app.orientation),HAS_CODE: hasDex ? 'true' : 'false',+ USES_CLEARTEXT: app.allowHttp ? 'true' : 'false',}), resources.references);// Every architecture: Play splits the bundle per device, which is what thediff --git a/build-tools/utils/androidBinaryXml.js b/build-tools/utils/androidBinaryXml.jsindex e29466da5..dd2f98c71 100644--- a/build-tools/utils/androidBinaryXml.js+++ b/build-tools/utils/androidBinaryXml.js@@ -39,6 +39,7 @@ export const ANDROID_ATTR_IDS = {targetSdkVersion: 0x01010270,required: 0x0101028e,extractNativeLibs: 0x010104ea,+ usesCleartextTraffic: 0x010104ec,version: 0x01010519,};diff --git a/build-tools/utils/apk.js b/build-tools/utils/apk.jsindex 25f9937de..2170650b1 100644--- a/build-tools/utils/apk.js+++ b/build-tools/utils/apk.js@@ -155,6 +155,7 @@ export function assembleApk(options) {VERSION_CODE: app.versionCode,SCREEN_ORIENTATION: androidScreenOrientation(app.orientation),HAS_CODE: hasDex ? 'true' : 'false',+ USES_CLEARTEXT: app.allowHttp ? 'true' : 'false',}), resources.references);// Every architecture the template carries, so one package installs on a phonediff --git a/native/android/host/AndroidManifest.xml.in b/native/android/host/AndroidManifest.xml.inindex 420811453..bf7959031 100644--- a/native/android/host/AndroidManifest.xml.in+++ b/native/android/host/AndroidManifest.xml.in@@ -37,9 +37,16 @@android:versionCode="@VERSION_CODE@"android:versionName="@VERSION_NAME@"><uses-sdk android:minSdkVersion="29" android:targetSdkVersion="33" />+ <!-- Without it every socket the app opens is refused: a remote group's assets,+ a hot update and the debug channel all go over the network. A normal+ permission, granted at install without asking. -->+ <uses-permission android:name="android.permission.INTERNET" /><uses-feature android:name="android.hardware.vulkan.version" android:version="0x400003" android:required="true" />+ <!-- usesCleartextTraffic: a development build talks to a LAN server over plain+ http; a shipping build is held to https, as Android 9+ holds every app. --><application android:label="@APP_NAME@" android:icon="@mipmap/ic_launcher"- android:hasCode="@HAS_CODE@" android:extractNativeLibs="false">+ android:hasCode="@HAS_CODE@" android:extractNativeLibs="false"+ android:usesCleartextTraffic="@USES_CLEARTEXT@"><!-- A game's window, stated before any code runs: no title, no status bar.The host asks for the rest (navigation bar, display cutout) once ithas the UI thread — see onWindowFocusChanged in platform/android.cpp. -->diff --git a/native/host/platform/android.cpp b/native/host/platform/android.cppindex 0776382dc..f552298f9 100644--- a/native/host/platform/android.cpp+++ b/native/host/platform/android.cpp@@ -42,6 +42,27 @@ using esengine::WebGPUDevice;namespace {+/** Clear the pending Java exception and say what it was: a bare "no response"+ * hid that Android refused cleartext HTTP to a LAN CDN. */+std::string takeJavaException(JNIEnv* env) {+ jthrowable ex = env->ExceptionOccurred();+ env->ExceptionClear();+ if (!ex) return "unknown error";+ jclass cls = env->GetObjectClass(ex);+ auto text = static_cast<jstring>(env->CallObjectMethod(ex, env->GetMethodID(cls, "toString", "()Ljava/lang/String;")));+ std::string out = "unknown error";+ if (!env->ExceptionCheck() && text) {+ const char* s = env->GetStringUTFChars(text, nullptr);+ out = s ? s : out;+ if (s) env->ReleaseStringUTFChars(text, s);+ }+ env->ExceptionClear();+ if (text) env->DeleteLocalRef(text);+ env->DeleteLocalRef(cls);+ env->DeleteLocalRef(ex);+ return out;+}+// Read a java.io.InputStream fully into `out`, reusing one byte[] buffer.void jniReadStream(JNIEnv* env, jobject stream, std::vector<u8>& out) {if (!stream) return;@@ -110,7 +131,7 @@ void performHttp(JNIEnv* env, const eshost::FetchRequest& req, eshost::FetchResu}jint code = env->CallIntMethod(conn, env->GetMethodID(httpCls, "getResponseCode", "()I"));- if (env->ExceptionCheck()) { env->ExceptionClear(); r.error = "no response"; env->DeleteLocalRef(httpCls); env->DeleteLocalRef(conn); return; }+ if (env->ExceptionCheck()) { r.error = takeJavaException(env); env->DeleteLocalRef(httpCls); env->DeleteLocalRef(conn); return; }r.status = static_cast<int>(code);r.ok = code >= 200 && code < 300;diff --git a/pipeline/src/export/exportGame.ts b/pipeline/src/export/exportGame.tsindex eb8b7eeab..56e254de5 100644--- a/pipeline/src/export/exportGame.ts+++ b/pipeline/src/export/exportGame.ts@@ -84,6 +84,15 @@ import { MODULES, NATIVE_MODULE_REGISTRY } from '../../../tools/nativeScriptModuexport type { ExportPlatform };+/** What to say about a CDN a shipping Android build cannot reach: Android refuses+ * plain http from an app that has not asked for it, and only a development+ * build asks. */+export function cleartextWarning(platform: string, shipping: boolean, remoteRoot?: string): string | null {+ if (platform !== 'android' || !shipping || !remoteRoot?.startsWith('http://')) return null;+ return `The CDN ${remoteRoot} is plain http://, which Android refuses in a shipping build: `+ + 'remote assets and hot updates will not load. Serve it over https.';+}+/*** `app.config.json` — what the native packagers need to build an *application** around the content, as opposed to what the runtime needs to play it.@@ -96,6 +105,9 @@ export type { ExportPlatform };export interface NativeAppConfig {/** Reverse-DNS: the Android manifest package / the iOS bundle identifier. */id: string;+ /** Plain `http://` is allowed (Android's usesCleartextTraffic): a development+ * build reaches a LAN server, and a shipping one is held to https. */+ allowHttp?: boolean;/** The name under the launcher icon. */name: string;/** Version as a store displays it (`versionName` / `CFBundleShortVersionString`). */@@ -1109,8 +1121,11 @@ async function produceExport(opts: ExportGameOptions): Promise<ExportGameResult>version: opts.appVersion ?? '1.0',versionCode: opts.androidVersionCode ?? 1,orientation,+ allowHttp: !opts.minify,};await writeFile(path.join(payloadDir, 'app.config.json'), JSON.stringify(appConfig, null, 2) + '\n');+ const http = cleartextWarning(platform, !!opts.minify, hotUpdate?.remoteRoot);+ if (http) warnings.push(http);// One icon for both targets, read once. A path that no longer exists is a// warning rather than a failed export: the package is still correct, it justdiff --git a/pipeline/tests/android-cleartext.test.ts b/pipeline/tests/android-cleartext.test.tsnew file mode 100644index 000000000..e3b60b4d5--- /dev/null+++ b/pipeline/tests/android-cleartext.test.ts@@ -0,0 +1,21 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file A shipping Android build is held to https; the export says so when the+ * project's CDN is plain http, rather than letting it fail on the phone.+ */+import { describe, it, expect } from 'vitest';+import { cleartextWarning } from '../src/export/exportGame';++describe('a plain-http CDN', () => {+ it('is named in a shipping Android build', () => {+ expect(cleartextWarning('android', true, 'http://192.168.1.5:8080/cdn')).toMatch(/http:\/\/192\.168\.1\.5:8080\/cdn.*https/);+ });++ it('is fine in a development build, on other targets, and over https', () => {+ expect(cleartextWarning('android', false, 'http://lan/cdn')).toBeNull();+ expect(cleartextWarning('ios', true, 'http://lan/cdn')).toBeNull();+ expect(cleartextWarning('android', true, 'https://cdn.example.com')).toBeNull();+ expect(cleartextWarning('android', true)).toBeNull();+ });+});diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 49f405a85..9b1683a53 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -1130,4 +1130,6 @@ export const NOTED = {{ note: 'A desktop (native) development build connects to the editor like a web or mini-game one — log, profiler, the world, content status, pause and step; its renderer cannot capture frames yet and says so. The native host gained a `ws://` client for it.' },'feat(build): a desktop development build can dial the editor':{ note: 'A desktop (native) development build connects to the editor like a web or mini-game one — log, profiler, the world, content status, pause and step; its renderer cannot capture frames yet and says so. The native host gained a `ws://` client for it.' },+ 'fix(android): a native Android app can reach the network':+ { note: 'A native Android app can reach the network: its manifest never asked for it, so a remote group\'s assets, hot updates and the debug channel all failed on Android. A development build also allows plain http (a LAN CDN); a shipping build is held to https, and the export warns when its CDN is http.' },};