From 3ccb4c4b5b5a8422276dd5f6f211b42e05ec9534 Mon Sep 17 00:00:00 2001 From: RustDesk <71636191+rustdesk@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:02:45 +0800 Subject: [PATCH] fix(android): move the log toggle native into ffi.FFI (#16993) * fix(android): move the log toggle native into ffi.FFI Custom clients move the Kotlin classes out of com.carriez.flutter_hbb. DocumentLogs declared setEnabled as a native method, so its export Java_com_carriez_flutter_1hbb_DocumentLogs_setEnabled did not link there and the app crashed on start with UnsatisfiedLinkError. The ffi package keeps its name, so declare the native in ffi.FFI like the others and call it from DocumentLogs. Fixes rustdesk/rustdesk-server-pro#1035 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01JAxkkiaGp2k8okvs4MpCUq * ci(android): fail when a JNI native is outside ffi.FFI Only ffi.FFI survives the package rename custom clients do. Check in the Android build that every Rust JNI export is Java_ffi_FFI_* and that no Kotlin file but ffi.kt declares an external fun, and write the rule down in AGENTS.md. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01JAxkkiaGp2k8okvs4MpCUq * ci(android): report every JNI rule violation and check class lookups Run the checks independently so one CI round lists both the Rust and the Kotlin offenders, and also fail on a Rust lookup of an app class by name. Note in AGENTS.md that a Kotlin method Rust calls by name needs @Keep to survive R8. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01JAxkkiaGp2k8okvs4MpCUq --------- Co-authored-by: Claude Opus 5.5 --- .github/workflows/flutter-build.yml | 14 ++++++++++++++ AGENTS.md | 6 ++++++ .../kotlin/com/carriez/flutter_hbb/DocumentLogs.kt | 4 ++-- flutter/android/app/src/main/kotlin/ffi.kt | 1 + src/platform/android_log.rs | 2 +- 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/.github/workflows/flutter-build.yml b/.github/workflows/flutter-build.yml index c37601232..081efd377 100644 --- a/.github/workflows/flutter-build.yml +++ b/.github/workflows/flutter-build.yml @@ -1512,6 +1512,20 @@ jobs: with: submodules: recursive + - name: Check JNI natives are in ffi.FFI + # Custom clients rename com.carriez.flutter_hbb; only ffi.FFI keeps its name. + shell: bash + run: | + bad=0 + grep -rnE --include='*.rs' 'fn Java_' src libs | grep -v 'fn Java_ffi_FFI_' && bad=1 + grep -rl --include='*.kt' 'external fun' flutter/android \ + | grep -vx 'flutter/android/app/src/main/kotlin/ffi.kt' && bad=1 + grep -rn --include='*.rs' 'com/carriez' src libs && bad=1 + if [ "$bad" = 1 ]; then + echo "::error::Declare Android natives in ffi.FFI, export them as Java_ffi_FFI_*, and do not look up app classes by name (see AGENTS.md)" + exit 1 + fi + - name: Install flutter uses: subosito/flutter-action@1a449444c387b1966244ae4d4f8c696479add0b2 # v2 with: diff --git a/AGENTS.md b/AGENTS.md index 1f41b7517..735d8587f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -80,6 +80,12 @@ workspace member. `base::config::keys` re-exports the handful of keys * Web bridge (committed): edit `flutter/lib/web/bridge.dart` directly. Follow the existing patterns there for `SyncReturn` / `Future` and the `dart:js` glue. * Native bridge (`flutter/lib/generated_bridge.dart`, `src/bridge_generated.rs`, `src/bridge_generated.io.rs`): these are gitignored and regenerated by the project's CI codegen. Manually editing them locally is fine for development testing, but those edits do not persist into commits. +## Android JNI + +* Custom clients move the Kotlin classes out of `com.carriez.flutter_hbb` into their own package; only the `ffi` package keeps its name. A `Java_com_carriez_flutter_1hbb_*` export therefore does not link there and the app crashes with `UnsatisfiedLinkError`. +* Declare every `external fun` in `ffi.FFI` (`flutter/android/app/src/main/kotlin/ffi.kt`) and export it from Rust as `Java_ffi_FFI_*`. A class elsewhere that needs one calls it through `FFI`. +* From Rust, reach app classes only through objects Kotlin passed in (`call_method` on a `GlobalRef`), never by a class name such as `find_class("com/carriez/...")`. Mark each Kotlin method Rust calls this way `@Keep`, or R8 renames it in release builds and the call fails with `NoSuchMethodError`. + ## Web (Flutter Web) Architecture Flutter Web in this repo is **not** "Dart compiled to JS via Flutter alone". The runtime is split: diff --git a/flutter/android/app/src/main/kotlin/com/carriez/flutter_hbb/DocumentLogs.kt b/flutter/android/app/src/main/kotlin/com/carriez/flutter_hbb/DocumentLogs.kt index 990547d78..73e983373 100644 --- a/flutter/android/app/src/main/kotlin/com/carriez/flutter_hbb/DocumentLogs.kt +++ b/flutter/android/app/src/main/kotlin/com/carriez/flutter_hbb/DocumentLogs.kt @@ -7,6 +7,7 @@ import android.os.ParcelFileDescriptor import android.provider.DocumentsContract import android.provider.DocumentsContract.Document import android.util.Log +import ffi.FFI import java.io.FileNotFoundException import java.io.IOException import java.util.UUID @@ -31,8 +32,7 @@ internal class DocumentLogs(private val context: Context) { setEnabled(grant() != null) } - @androidx.annotation.Keep - private external fun setEnabled(enabled: Boolean) + private fun setEnabled(enabled: Boolean) = FFI.setDocumentLogsEnabled(enabled) private fun grant(): Uri? = prefs.getString(LOG_GRANT, null)?.let(Uri::parse) diff --git a/flutter/android/app/src/main/kotlin/ffi.kt b/flutter/android/app/src/main/kotlin/ffi.kt index 02e6606ae..1f2283228 100644 --- a/flutter/android/app/src/main/kotlin/ffi.kt +++ b/flutter/android/app/src/main/kotlin/ffi.kt @@ -28,4 +28,5 @@ object FFI { external fun getBuildinOption(key: String): String external fun onClipboardUpdate(clips: ByteBuffer) external fun isServiceClipboardEnabled(): Boolean + external fun setDocumentLogsEnabled(enabled: Boolean) } diff --git a/src/platform/android_log.rs b/src/platform/android_log.rs index 2b41621d3..b761f9a1d 100644 --- a/src/platform/android_log.rs +++ b/src/platform/android_log.rs @@ -26,7 +26,7 @@ const DEFAULT_LOG_FILE_COUNT: usize = 90; static PUBLIC_LOGS_ENABLED: AtomicBool = AtomicBool::new(true); #[no_mangle] -pub extern "system" fn Java_com_carriez_flutter_1hbb_DocumentLogs_setEnabled( +pub extern "system" fn Java_ffi_FFI_setDocumentLogsEnabled( _env: jni::JNIEnv, _instance: jni::objects::JObject, enabled: jni::sys::jboolean,