Why do I need this?
I'm creating a Flutter package (for android) and I want to create the MethodCallHandler's code in a separate AAR file that can be added in my plugin's code
So I'll have:
pluginNativeCode/android/MyPlugin.kt
import ir.malv.android.flutter.MySeparateMethodCallHandler
class MyFlutterPlugin: FlutterPlugin {
override fun onAttachedToEngine(...) {
// Here's the source of the problem
channel.setMethodCallHandler(MySeparateMethodCallHandler())
}
}
The MySeparateMethodCallHandler is not in the plugin/android part and it's imported as a gradle dependency
However in that library I don't have access to flutter's codes like MethodCallHandler class.
Similar behaviour in unity and react native
In react-native there is
com.facebook.react:react-native:+that can be added ascompileOnlyto get the needed react-native codes
In unity we have
unity.jarfile which contains unity native codes and can be added ascompileOnlyas well to provide engine's native APIs
What do we have in Flutter for this?
Is there a dependency that I can include as compileOnly and use it to get the needed classes and create my aar file in a separate project?
Here's a solution:
repositoriesofbuild.gradlebuild.gradleNotice the
compileOnly. This will avoid the class duplication. Plugin just requires it. The flutter app will provide it itself.in your code you will have access to needed classes such as
MethodChannel,MethodChannel.Resultand ...Bonus: How do I know what engine I'm making the plugin with?
I think it should not matter if you just need a class like
MethodChannelto invoke a method, but here's a way to get it using Android Studioproject/example/androidof your plugin using Android studio (that has Flutter plugin activated)gradle plugin_name:dependenciestask and look forflutter_embedding_phrase.