Sub topic 3
Telling Flutter about your native code in pubspec.yaml
This chapter assumes you have generated a plugin project (see the previous chapter) and can read basic YAML. You do not need to write any native code yet. The goal is to understand the one file that connects your Kotlin and Swift classes to the Flutter tool.
Why pubspec.yaml matters for plugins
A plugin contains Dart code and native code. Flutter has to know about the native code when it builds an app that depends on your plugin. It cannot discover Kotlin or Swift classes by scanning your files. It learns about them from the flutter.plugin section of the plugin's pubspec.yaml.
When an app runs flutter pub get, the Flutter tool reads that section from every dependency. It records which plugins exist and which native class each platform must register. It then writes registration code into the app's own Android and iOS projects. If the section is missing or wrong, the Dart code compiles but the native side is never started. At runtime you then see a MissingPluginException.
The plugin section
Here is the pubspec.yaml for the battery_info plugin from this tutorial:
name: battery_info
description: Reads the battery level on Android and iOS.
version: 0.0.1
environment:
sdk: ^3.0.0
flutter: ">=3.10.0"
dependencies:
flutter:
sdk: flutter
flutter:
plugin:
platforms:
android:
package: com.example.battery_info
pluginClass: BatteryInfoPlugin
ios:
pluginClass: BatteryInfoPluginThe nesting is flutter → plugin → platforms → one entry per platform. Each entry lists the keys that platform needs.
Android keys
packageis the Kotlin or Java package that contains the plugin class. It must match thepackageline at the top of your Kotlin file.pluginClassis the simple class name, without the package. The class must implementFlutterPlugin.
The tool combines the two into a fully qualified name, here com.example.battery_info.BatteryInfoPlugin.
iOS keys
pluginClassis the name of the Swift (or Objective-C) class. It must conform toFlutterPluginand provideregister(with:).
iOS has no package key because Swift classes are found through the module that CocoaPods builds. The module name comes from the pod, which is named after the plugin in ios/battery_info.podspec.
Keys for other platforms
You only declare platforms you implement. The table lists the common entries for completeness.
| Platform | Key for the native class | Notes |
|---|---|---|
| `android` | `package` and `pluginClass` | Both required for a native Android class |
| `ios` | `pluginClass` | Swift or Objective-C |
| `macos` | `pluginClass` | Same shape as iOS |
| `windows`, `linux` | `pluginClass` | A C++ class name |
| `web` | `pluginClass` and `fileName` | The Dart file that holds the class |
| any | `dartPluginClass` | A Dart-only implementation that registers itself |
This tutorial uses only android and ios.
What the Flutter tool does with it
The declaration is read at flutter pub get, and its effect shows up at build time. The flow looks like this:
The tool produces three outputs:
- A JSON file named
.flutter-plugins-dependenciesin the app root. It lists every plugin and platform. Build scripts read it. - An Android file,
GeneratedPluginRegistrant, that adds each plugin class to the engine. - An iOS file, also named
GeneratedPluginRegistrant, that calls each plugin'sregistermethod.
The Android registrant contains lines like this. You never edit this file because it is regenerated on each pub get:
// Excerpt of a generated file. Do not edit.
try {
flutterEngine.getPlugins().add(new com.example.battery_info.BatteryInfoPlugin());
} catch (Exception e) {
Log.e(TAG, "Error registering plugin battery_info, com.example.battery_info.BatteryInfoPlugin", e);
}The try block is why a typo in pluginClass sometimes shows up as a log message or a build failure instead of a clean crash. Read your logs when a plugin seems silent.
The pluginClass string is also the link between your YAML and your code. The Android registrant instantiates the class by name. The iOS registrant calls BatteryInfoPlugin.register(with:) through the Swift-to-Objective-C bridge. Neither platform checks that the class does something useful. They only check that it exists.
Seeing the result yourself
After you add the plugin to an app, search the generated files to confirm the registration:
cd battery_info/example
flutter pub get
grep -R "BatteryInfoPlugin" android/app/src/main/java ios/Runner/GeneratedPluginRegistrant.mOn recent Flutter versions the Android registrant is under android/app/src/main/java/io/flutter/plugins/. If grep prints nothing, the declaration is not being picked up. The next section covers the usual reasons.
Common errors
Most pubspec mistakes are small and produce confusing symptoms.
- Class name mismatch.
pluginClass: BatteryInfoPluginbut the Kotlin class isBatteryPlugin. The Android build fails with an unresolved reference in the generated registrant, or the plugin is silently skipped. - Wrong
package. The YAML sayscom.example.battery_infobut the Kotlin file declarespackage com.example.batteryinfo. The result is the same class-not-found failure. Copy the package from the Kotlin file instead of retyping it. - Missing platform entry. You wrote the Swift code but left out the
ios:block. The app builds on iOS, and the first channel call throwsMissingPluginException(No implementation found for method getBatteryLevel on channel battery_info). - Wrong indentation. If
plugin:is not nested underflutter:, YAML still parses, but Flutter ignores the section. The package then acts as a plain Dart package. - Stale registrant. After editing the plugin's
pubspec.yamlyou must runflutter pub getagain in the app. For a path dependency, editing the plugin's pubspec does not trigger regeneration by itself. - Hot reload and hot restart. Neither re-registers plugins. After you change native code or the declaration, stop the app and run it again.
Other fields that affect plugins
Some top-level fields interact with plugin behavior.
environment.fluttersets the lowest Flutter version that supports your code. If your Kotlin uses theFlutterPluginAPI withFlutterPluginBinding, a very old SDK cannot build it. Set a floor you have tested.environment.sdksets the Dart range.^3.0.0means 3.0.0 up to but not including 4.0.0.versionfollows semantic versioning. Adding a new platform entry is a feature change, so it deserves a minor bump. Removing one is a breaking change.dependencies.flutter.sdk: flutteris required. Plugin Dart code importspackage:flutter/services.dartforMethodChannel.
Android also has its own version settings in android/build.gradle, such as minSdk and namespace. Those are separate from the pubspec, and a later chapter covers them with native dependencies. The ios/battery_info.podspec file similarly holds the minimum iOS version. The pubspec decides what Flutter registers. The Gradle and podspec files decide how the native code builds.
Checking your declaration
A short routine catches most mistakes before they reach a device:
- Confirm that each platform you implement has an entry under
platforms. - Open the Kotlin file and compare its
packageand class name to the YAML. - Open the Swift file and compare its class name to the YAML.
- Run
flutter pub getin theexampledirectory. - Run
flutter runand call the plugin once.
Exercise
A teammate adds an ios: entry with pluginClass: BatteryInfoPlugin to a plugin's pubspec.yaml. The Swift class in the project is named BatteryPlugin. The Android side works. What do you expect on iOS, and how do you fix it?
Show answer
The iOS registrant refers to BatteryInfoPlugin, which does not exist in the module, so the build fails or the plugin is not registered and Dart calls throw MissingPluginException. Fix it by making the names agree: either rename the Swift class to BatteryInfoPlugin or change pluginClass to BatteryPlugin. Then run flutter pub get in the app and do a full rebuild, because hot reload does not re-register plugins.
Exercise
Why does the Android entry need a package key while the iOS entry does not?
Show answer
Android classes are identified by a package plus a class name, so the Flutter tool needs both to build the fully qualified name for the registrant. On iOS the Swift class is found through the module that CocoaPods builds from the pod, so the class name alone is enough.