Closed debanjanbasu closed 6 months ago
Hi! Thanks for opening your first issue here! :smile:
Hi, I am also not an Android expert, and here are my two cents:
Finally got to solve it by using this code block in this way:
in MainActivity.kt
package com.example.spareshare
import android.content.Context
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(
@NonNull flutterEngine: FlutterEngine,
) {
super.configureFlutterEngine(flutterEngine)
flutterEngine.plugins.add(MyPlugin())
}
}
class MyPlugin : FlutterPlugin, MethodCallHandler {
companion object {
init {
System.loadLibrary("rust_lib_spareshare")
}
}
external fun init_android(ctx: Context)
override fun onAttachedToEngine(
@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding,
) {
init_android(flutterPluginBinding.applicationContext)
}
override fun onMethodCall(
@NonNull call: MethodCall,
@NonNull result: Result,
) {
result.notImplemented()
}
override fun onDetachedFromEngine(
@NonNull binding: FlutterPlugin.FlutterPluginBinding,
) {
}
}
and in lib.rs
#[cfg(target_os = "android")]
use {
jni::{objects::JClass, objects::JObject, JNIEnv},
mylib::setup_android,
};
#[cfg(target_os = "android")]
#[no_mangle]
pub extern "system" fn Java_com_example_spareshare_MyPlugin_init_1android(
env: JNIEnv,
_class: JClass,
ctx: JObject,
) {
setup_android(env, ctx);
}
Closing it, as has been resolved.
Happy to see it is resolved!
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.
Describe the bug
I'm working on a project, which does require android_context to be initialized beforehand. The challenge is the I followed this guide: https://cjycode.com/flutter_rust_bridge/manual/troubleshooting#android-context-was-not-initialized-or-ndk_context-initialization
and obviously in the the main Kotling file:
Also, I'm using regular await Rustlib.init()
When my application is trying to attach the current android thread, it's getting attached to different threads, as two threads are being spun up.
The above code is getting attached to the second thread, wherein the regular other FRB functions are to the first one. Is there a way to avoid this? 🤔 I've been splitting my head over this for almost a week now. I've tried all possibly ways, as am not an avid Kotling developer, I really want to avoid customizing the Kotlin files, also that might not even solve the issue, if FRB, and the JNI_Onload are running on two separate threads.
Steps to reproduce
It's a unique problem, but I would try to reproduce the problem from an example code. I'm sure there might be a simpler workaround.
Logs
Expected behavior
Expected to have one thread so when we're trying to get the android_context, it does work as expected, unless we're actually spinning up another thread.
Generated binding code
No response
OS
MacOS
Version of
flutter_rust_bridge_codegen
dev.30
Flutter info
Version of
clang++
No response
Additional context
No response