Sub topic 14
Adding Native Libraries with Gradle and CocoaPods
This chapter assumes you can build a Flutter plugin and read basic Kotlin, Swift, Gradle, and CocoaPods files. You will learn how to add a native dependency to the plugin package, rather than directly to an application that uses the plugin.
Where native dependencies belong
A Flutter plugin has two native projects:
- The Android project uses Gradle.
- The iOS project uses CocoaPods.
Add an Android library to the plugin's android/build.gradle file. Add an iOS pod to the plugin's .podspec file. Consumers of the plugin then receive these dependencies when they run flutter pub get or build their applications.
| Platform | Dependency file | Dependency declaration |
|---|---|---|
| Android | `android/build.gradle` | `implementation(...)` |
| iOS | `ios/battery_info.podspec` | `s.dependency(...)` |
Do not add the dependency only to the example application's Gradle or Podfile. That would make the example work while leaving the published plugin incomplete.
Adding an Android library with Gradle
Open the plugin's Android build file. Add the library inside the dependencies block:
group 'com.example.battery_info'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.9.24'
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:8.5.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
android {
namespace 'com.example.battery_info'
compileSdk 34
defaultConfig {
minSdk 21
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.13.1'
}The implementation configuration keeps the library as an internal detail of the plugin. Use api only when consumers must compile directly against types from that library.
Check the library's minimum Android SDK, required repositories, and transitive dependencies. A transitive dependency is a library required by another library. Conflicting versions can produce Gradle resolution errors or runtime failures.
Adding an iOS library with CocoaPods
Open the plugin podspec, commonly named ios/battery_info.podspec:
Pod::Spec.new do |s|
s.name = 'battery_info'
s.version = '1.0.0'
s.summary = 'Battery information for Flutter.'
s.description = 'A Flutter plugin that reads battery information.'
s.homepage = 'https://example.com/battery_info'
s.license = { :file => '../LICENSE' }
s.author = { 'Example' => '[email protected]' }
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.dependency 'Flutter'
s.dependency 'Alamofire', '~> 5.9'
s.platform = :ios, '12.0'
s.swift_version = '5.0'
endThe version constraint ~> 5.9 accepts compatible releases starting at 5.9 but prevents an incompatible major-version upgrade. Confirm that the pod supports the deployment target declared by the plugin.
If the library is distributed as a Swift package rather than a pod, check whether the Flutter plugin tooling and the library's integration method support it. Do not assume that an iOS dependency can be copied between CocoaPods and Swift Package Manager.
Validate the package
From the plugin root, run:
flutter pub get
flutter analyze
flutter test
flutter pub publish --dry-runBuild the example application on each supported platform. This catches native issues that Dart tests cannot detect, including missing repositories, incompatible deployment targets, and duplicate symbols.
Keep native library versions documented and update them deliberately. Review licenses and platform support before publishing. A dependency can change your plugin's license obligations, binary size, minimum OS version, and privacy requirements.
Exercise
Where should you declare an Android library used by a plugin, and why should you not declare it only in the example app?
Show answer
Declare it in the plugin's android/build.gradle dependencies block. The plugin must carry its own native dependency so every consuming application receives it during its build.