diff --git a/App_Resources/Android/before-plugins.gradle b/App_Resources/Android/before-plugins.gradle index 9faffb81..f643993d 100644 --- a/App_Resources/Android/before-plugins.gradle +++ b/App_Resources/Android/before-plugins.gradle @@ -10,6 +10,7 @@ project.ext { // androidXTransition = "1.4.1" // androidXViewPager = "1.0.0" - // useKotlin = true - // kotlinVersion = "1.6.0" + // NativeScript 9 already applies kotlin-android. Pin its current compiler + // for our .kt sources; no separate Kotlin installation is required. + kotlinVersion = "2.2.20" } diff --git a/App_Resources/Android/src/main/java/com/faceclaw/shared/KotlinBridge.kt b/App_Resources/Android/src/main/java/com/faceclaw/shared/KotlinBridge.kt new file mode 100644 index 00000000..809537dd --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/shared/KotlinBridge.kt @@ -0,0 +1,21 @@ +package com.faceclaw.shared + +/** NativeScript implements this Kotlin-owned contract in TypeScript. */ +interface KotlinBridgeListener { + fun transform(message: String): String +} + +/** + * First step toward commonMain: only portable Kotlin, with no Android/JVM APIs. + * Calls and callbacks are synchronous on the caller's thread. The listener is + * used only during roundTrip and is never retained. + */ +class KotlinBridge { + fun greet(name: String): String = "Hello, $name, from Kotlin" + + fun roundTrip(message: String, listener: KotlinBridgeListener): String { + val reply = listener.transform("Kotlin received: $message") + // Consume the TypeScript return value, then return it across the bridge. + return "Kotlin received callback: $reply" + } +} diff --git a/app/app.ts b/app/app.ts index 7a2f44f9..22a27f87 100644 --- a/app/app.ts +++ b/app/app.ts @@ -8,6 +8,11 @@ import { Application } from '@nativescript/core' import { registerShareIntentHandler } from './native/share-intents' import { installNativeUserAgent } from './util/http' import { registerPhoneRotation } from './native/phone-rotation' +import { runKotlinBridgeSmokeTest } from './native/kotlin-bridge' + +declare const __DEV__: boolean; + +if (__DEV__) runKotlinBridgeSmokeTest() installNativeUserAgent() registerShareIntentHandler() diff --git a/app/native/kotlin-bridge.ts b/app/native/kotlin-bridge.ts new file mode 100644 index 00000000..6baa866e --- /dev/null +++ b/app/native/kotlin-bridge.ts @@ -0,0 +1,49 @@ +import { isAndroid } from "@nativescript/core"; + +// Handwritten declarations for this small native surface. NativeScript builds +// the runtime metadata from Kotlin's compiled classes, not these declarations. +declare namespace com.faceclaw.shared { + class KotlinBridge extends java.lang.Object { + constructor(); + greet(name: string): string; + roundTrip(message: string, listener: KotlinBridgeListener): string; + } + class KotlinBridgeListener extends java.lang.Object { + constructor(implementation: { transform(message: string): string }); + } +} + +/** Exercise the real native metadata and both directions of return values. */ +export function runKotlinBridgeSmokeTest(): void { + if (!isAndroid) return; + + try { + const bridge = new com.faceclaw.shared.KotlinBridge(); + const greeting = bridge.greet("Faceclaw"); + const message = "Faceclaw \u2194 Kotlin \ud83d\udc53"; + let callbackCount = 0; + let callbackMessage = ""; + // Keep the proxy in scope through the synchronous native call. Do not + // throw from the callback; validate after returning across the boundary. + const listener = new com.faceclaw.shared.KotlinBridgeListener({ + transform(value: string): string { + callbackCount++; + callbackMessage = value; + return `TypeScript received: ${value}`; + }, + }); + const reply = bridge.roundTrip(message, listener); + if (greeting !== "Hello, Faceclaw, from Kotlin" + || callbackCount !== 1 + || callbackMessage !== `Kotlin received: ${message}` + || reply !== `Kotlin received callback: TypeScript received: Kotlin received: ${message}`) { + throw new Error(`Unexpected Kotlin bridge result: ${JSON.stringify({ + greeting, callbackCount, callbackMessage, reply, + })}`); + } + console.log("FACECLAW_KOTLIN_BRIDGE_PASS " + reply); + } catch (error) { + // Diagnostics must not prevent the rest of the app from launching. + console.error("FACECLAW_KOTLIN_BRIDGE_FAIL " + error); + } +}