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 as compileOnly to get the needed react-native codes

In unity we have unity.jar file which contains unity native codes and can be added as compileOnly 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?


Solution 1: Mahdi-Malv

Here's a solution:

  1. Add this remote to repositories of build.gradle
allprojects {
  repositories {
    // .. rest of remote urls

    maven { url("http://download.flutter.io/")}
  }
}
  1. Add this dependency to module's 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

  • Open the project/example/android of your plugin using Android studio (that has Flutter plugin activated)
  • run gradle plugin_name:dependencies task and look for flutter_embedding_ phrase.