dart-lang / native

Dart packages related to FFI and native assets bundling.
BSD 3-Clause "New" or "Revised" License
157 stars 44 forks source link

Accessing flutter plugin (FlutterPlugin) using jnigen #1708

Open award-wq opened 2 weeks ago

award-wq commented 2 weeks ago

I created a plugin similar to this:

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware

@Keep
public class MyPlugin: FlutterPlugin, ActivityAware {
 ...my custom implementation
}

And registering it in the MainActivity like this:

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)

        flutterEngine.plugins.add(MyPlugin())
    }

Accessing MyPlugin from the generated jni bindings returns a new instance of the class that hasn't been initialized by the flutter engine. How can I implement a workflow that allows me to access always the same instance of MyPlugin using jnigen generated bindings that was added to the flutter engine plugins in configureFlutterEngine.

HosseinYousefi commented 2 weeks ago

You can have a static singleton of your plugin that you register and access it from Dart.