Builds Flutter plugins that provide native interop for other apps to use. Use when creating reusable packages that bridge Flutter with platform-specific functionality.
Implement federated plugins to split a plugin's API across multiple packages, allowing independent teams to build platform-specific implementations. Structure federated plugins into three distinct components:
my_plugin_android, my_plugin_windows).Choose the correct plugin template based on your native interoperability requirements:
--template=plugin): Use for accessing platform-specific APIs (e.g., Android SDK, iOS frameworks) via Method Channels.--template=plugin_ffi): Use for accessing C/C++ native libraries, configuring Google Play services on Android, or using static linking on iOS/macOS.
Follow this workflow to initialize a new plugin package.
Task Progress:
flutter create command.Conditional Initialization:
flutter create --template=plugin \
--platforms=android,ios,web,linux,macos,windows \
--org com.example.organization \
-i objc -a java \
my_plugin
lib (using dart:ffi) and native source code in src (with a CMakeLists.txt):
flutter create --template=plugin_ffi my_ffi_plugin
Always edit Android platform code using Android Studio to ensure proper code completion and Gradle synchronization.
Task Progress:
FlutterPlugin and lifecycle-aware interfaces.registerWith logic.cd example
flutter build apk --config-only
example/android/build.gradle or example/android/build.gradle.kts file.java/<organization-path>/<PluginName>.FlutterPlugin interface.registerWith() method and the new onAttachedToEngine() method into a single private method. Both entry points must call this private method to maintain backward compatibility without duplicating logic.Activity reference: Implement the ActivityAware interface and handle the onAttachedToActivity, onDetachedFromActivityForConfigChanges, onReattachedToActivityForConfigChanges, and onDetachedFromActivity callbacks.Service: Implement the ServiceAware interface.MainActivity.java extends the v2 embedding io.flutter.embedding.android.FlutterActivity.Always edit Windows platform code using Visual Studio.
Task Progress:
cd example
flutter build windows
example/build/windows/hello_example.sln file.hello_plugin/Source Files and hello_plugin/Header Files in the Solution Explorer.Use this workflow to retrofit an existing plugin with support for additional platforms.
Task Progress:
flutter create --template=plugin --platforms=web,macos .
.podspec file and configure the required dependencies and deployment targets.High-fidelity example of an Android plugin implementing FlutterPlugin and ActivityAware while maintaining legacy compatibility.
package com.example.myplugin;
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
/** MyPlugin */
public class MyPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {
private MethodChannel channel;
// Public constructor required for v2 embedding
public MyPlugin() {}
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
setupChannel(flutterPluginBinding.getBinaryMessenger());
}
// Legacy v1 embedding support
public static void registerWith(Registrar registrar) {
MyPlugin plugin = new MyPlugin();
plugin.setupChannel(registrar.messenger());
}
// Shared initialization logic
private void setupChannel(BinaryMessenger messenger) {
channel = new MethodChannel(messenger, "my_plugin");
channel.setMethodCallHandler(this);
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
} else {
result.notImplemented();
}
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}
@Override
public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
// Handle Activity attachment
}
@Override
public void onDetachedFromActivityForConfigChanges() {
// Handle config changes
}
@Override
public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
// Handle reattachment
}
@Override
public void onDetachedFromActivity() {
// Clean up Activity references
}
}