Skip to content

An open-source remote desktop application designed for self-hosting, as an alternative to TeamViewer.

fix(android): move the log toggle native into ffi.FFI (#16993)

RustDesk3ccb4c4b5b5a
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<T>` / `Future<T>` 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,