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.
In react-native there is
com.facebook.react:react-native:+
that can be added ascompileOnly
to get the needed react-native codes
In unity we have
unity.jar
file which contains unity native codes and can be added ascompileOnly
as 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:
repositories
of build.gradle
allprojects {
repositories {
// .. rest of remote urls
maven { url("http://download.flutter.io/")}
}
}
build.gradle
dependencies {
compileOnly("io.flutter:flutter_embedding_debug:1.0.0-0fdb562ac8068ce3dda6b69aca3f355f4d1d2718")
// rest
}
Notice the compileOnly
. This will avoid the class duplication. Plugin just requires it. The flutter app will provide it itself.
What is
0fdb562ac8068ce3dda6b69aca3f355f4d1d2718
?
It's the engine version that is good to match the version you are developing the plugin with.
in your code you will have access to needed classes such as MethodChannel
, MethodChannel.Result
and ...
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 MethodChannel
to invoke a method, but here's a way to get it using Android Studio
project/example/android
of your plugin using Android studio (that has Flutter plugin activated)gradle plugin_name:dependencies
task and look for flutter_embedding_
phrase.