Sub topic 2
Creating the plugin project
This chapter covers generating a plugin project with flutter create, what the tool writes to disk, and how to check that the unmodified template builds. It assumes you can write Dart and build Flutter apps, have Android Studio and Xcode installed with a working Flutter toolchain, and can read basic Gradle and CocoaPods files. The next chapter edits the pubspec.yaml configuration, so here you only need to know where it lives.
Choosing the template
flutter create has several templates. The plugin template produces a package that contains Dart code plus native code for each platform you list. Two neighboring templates are easy to confuse with it:
| Template | Contains native code | Calls native code through |
|---|---|---|
| `package` | No | Not applicable, Dart only |
| `plugin` | Yes, per platform | Platform channels |
| `plugin_ffi` | Yes, C or C++ | dart:ffi bindings |
Pick plugin when the native side is an operating system API written in Kotlin or Swift, such as the battery level. Pick plugin_ffi when you wrap a C library. Pick package when Dart alone is enough.
Running the generator
The command below creates a plugin named battery_info with Kotlin and Swift native code. It then starts the generated example app on a connected device.
flutter create --template=plugin --platforms=android,ios -a kotlin -i swift --org com.example battery_info
cd battery_info/example
flutter runEach option has a specific effect:
--template=pluginselects the plugin template.--platforms=android,ioslimits the generated folders to those platforms. Without it, current Flutter versions may generate only a default set of platforms, so always state the list.-a kotlin(long form--android-language) chooses Kotlin. The alternative isjava. Kotlin is the default in current versions, but stating it makes the command reproducible.-i swift(long form--ios-language) chooses Swift. The alternative isobjc.--org com.exampleis the reverse-domain prefix. The tool combines it with the project name to form the Android package, herecom.example.battery_info. It also forms the bundle identifier of the example app.- The last argument,
battery_info, is the directory name and, unless you pass--project-name, the package name.
The package name must be a valid Dart package name: lowercase letters, digits and underscores, starting with a letter, and not a reserved word. Choose it carefully. A name on pub.dev cannot be changed after publishing, and the name also appears in the channel name, the class names, and the native directory structure.
Use your own reverse-domain value for --org if you plan to publish. The Android package name is baked into the Kotlin file path and the package line, so changing it later means moving files and editing several references.
What the tool generates
The exact file list depends on your Flutter version, but the structure looks like this:
battery_info/
├── pubspec.yaml
├── README.md
├── CHANGELOG.md
├── LICENSE
├── lib/
│ └── battery_info.dart
├── test/
│ └── battery_info_test.dart
├── android/
│ ├── build.gradle
│ ├── settings.gradle
│ └── src/main/
│ ├── AndroidManifest.xml
│ └── kotlin/com/example/battery_info/
│ └── BatteryInfoPlugin.kt
├── ios/
│ ├── battery_info.podspec
│ └── Classes/
│ └── BatteryInfoPlugin.swift
└── example/
├── pubspec.yaml
├── lib/main.dart
├── android/
├── ios/
└── integration_test/Newer Flutter releases may place the Swift file in a Swift Package Manager layout under ios/battery_info/Sources/battery_info/. They may also split lib/ into a public file, a platform interface file, and a method channel file. The roles stay the same, so read your generated tree before following paths in this tutorial.
The plugin root
The root pubspec.yaml describes the package and, under the flutter: plugin: key, tells the Flutter tool which native class to register on each platform. The next chapter covers that section. For now, notice that the generator already filled it in with the package name and class names it derived from the project name.
lib/ holds the public Dart API. Application developers import only this folder's public library, for example package:battery_info/battery_info.dart.
test/ holds Dart unit tests. A later chapter replaces the generated test with one that mocks the channel.
CHANGELOG.md, LICENSE and README.md are placeholders. pub.dev reads them when you publish, so the generator creates them early. The license file starts with a "TODO" line that you must replace.
The Android folder
android/ is a Gradle library project. Its build.gradle sets the namespace, compileSdk, and minSdk. Native dependencies for the Android side go here. src/main/kotlin/... contains BatteryInfoPlugin.kt, a class that implements FlutterPlugin and MethodCallHandler. The generated version answers a single getPlatformVersion method. You replace that method with your own in a later chapter.
The iOS folder
ios/battery_info.podspec is the CocoaPods specification. It names the pod, lists source files, sets the minimum iOS version, and declares dependencies on Flutter and on any native pods you add. The Swift class BatteryInfoPlugin conforms to FlutterPlugin. It also answers getPlatformVersion as generated.
The example app
example/ is a complete Flutter app that depends on the plugin with a path dependency (path: ../). It is the place to run and debug your native code. The plugin and the example app are separate Flutter projects. The plugin has no main() and cannot run by itself.
Verifying the untouched template
Run the template before changing anything. A broken toolchain is much easier to find when you have edited nothing.
- From the plugin root, run
flutter pub get. - Enter
example/and runflutter runwith an Android emulator or device connected. - Repeat with an iOS simulator or device.
The example shows "Running on: ..." followed by the OS version. That text comes from the generated getPlatformVersion call, so seeing it proves that the Dart code, the channel, and the native class all work.
If you plan to open the native code in an IDE, build the example first. Android Studio's Gradle sync for example/android and Xcode's workspace at example/ios/Runner.xcworkspace both depend on files that a first build creates. The commands flutter build apk --debug and flutter build ios --no-codesign create them without a device.
Common errors
- "Invalid package name": the project name has uppercase letters, hyphens, or a leading digit. Rename it to
snake_case. - Missing platform folder later: if you generated only Android and now want iOS, run
flutter create --template=plugin --platforms=ios .in the plugin root. Do the same insideexample/with--platforms=ios, using the regular app template. - CocoaPods failures on first iOS run: run
pod repo updateor deleteexample/ios/Podfile.lockandexample/ios/Pods, then run again. - Kotlin or Gradle version mismatch: the template targets the Gradle and Android Gradle Plugin versions of your Flutter release. An old Android Studio installation can disagree with them, so update it before editing the Gradle files by hand.
- Editing the wrong native copy: the files in
example/androidandexample/iosbelong to the host app. Plugin code belongs under the plugin's ownandroid/andios/folders.
Version control
Commit the generated tree before you edit it. The generator also writes a .gitignore and a .metadata file. Keep .metadata, since Flutter uses it to track which template version created the project. Do not commit example/ios/Pods, build/, or .dart_tool/, which the generated ignore rules already exclude.
Exercise
You ran flutter create --template=plugin --platforms=android -a kotlin --org com.acme weather_kit. What is the Android package name, which template option decides whether native code is Kotlin or Java, and what command adds iOS support to the existing project afterwards?
Show answer
The Android package is com.acme.weather_kit, built from --org plus the project name. The -a (--android-language) option picks Kotlin or Java. To add iOS, run flutter create --template=plugin --platforms=ios . from the plugin root, then add iOS to the example app with flutter create --platforms=ios . inside example/.
Exercise
Why should you run the generated example app before changing any code, and which folder contains the Xcode workspace you should open to edit the Swift plugin code?
Show answer
A clean run proves that the toolchain, the channel, and the native registration all work, so later failures come from your changes. The workspace is example/ios/Runner.xcworkspace, after a first build has resolved the pods.