mcpField
F
flutter-spm

flutter-spm

github.com/dhruvanbhalaraDevelopment3040 installsUpdated 8w ago
Repository

About flutter-spm

A guide for Flutter developers and plugin authors adopting Swift Package Manager for iOS and macOS dependencies in Flutter 3.44+. It covers project setup, dependency declarations, CI caching, and troubleshooting.

Key features

  1. 01Flutter 3.44+ SPM setup
  2. 02Native package management through Xcode
  3. 03Plugin dependency declarations
  4. 04CI cache guidance
  5. 05Dependency resolution troubleshooting

Use cases

  1. 01Adding a Swift package to the iOS Runner
  2. 02Declaring SPM dependencies in a Flutter plugin
  3. 03Caching SPM files in CI builds
  4. 04Resolving package failures or version conflicts

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/dhruvanbhalara/skills --skill flutter-spm

After installing, type/flutter-spmin Claude Code to confirm it loaded.

Details

From the project README

Swift Package Manager (SPM) in Flutter

Introduced in Flutter 3.44

Swift Package Manager is the default dependency manager for iOS and macOS applications in Flutter, replacing CocoaPods. This modern integration improves compilation speeds and simplifies native package dependency trees.

Contents


Project Configuration

In Flutter 3.44+, newly generated iOS and macOS platforms are configured to use SPM out of the box.

Key Changes
  • No Podfile or Podfile.lock: The iOS directory no longer requires CocoaPods.
  • Xcode Workspace Integration: Native dependencies are managed directly via Xcode's Package Dependencies.
  • Flutter Plugins: Plugins with native Swift code declare their SPM configurations inside their pubspec.yaml or Package.swift files, which Flutter automatically integrates into the runner target.

Managing Dependencies

Adding a Swift Package

To add a native package directly to the iOS Runner:

  1. Open ios/Runner.xcworkspace in Xcode.
  2. Select File > Add Package Dependencies...
  3. Enter the package URL (e.g., https://github.com/Alamofire/Alamofire) and select target version.
  4. Link the package to the Runner target.
For Plugin Authors

To define an SPM dependency for a Flutter plugin, use the swift_packages field in pubspec.yaml:

flutter:
  plugin:
    platforms:
      ios:
        pluginClass: MyPlugin
        swift_packages:
          - name: Alamofire
            url: https://github.com/Alamofire/Alamofire.git
            version: 5.9.0

CI/CD Caching Strategies

SPM dependencies are stored outside the project workspace. To optimize CI build times (e.g., in GitHub Actions), cache the following directories:

  • SPM Cache Directory: ~/Library/Caches/org.swift.swiftpm
  • Xcode DerivedData: ~/Library/Developer/Xcode/DerivedData
GitHub Actions Caching Example
- name: Cache Swift Package Manager Dependencies
  uses: actions/cache@v4
  with:
    path: |
      ~/Library/Caches/org.swift.swiftpm
      ~/Library/Developer/Xcode/DerivedData
    key: ${{ runner.os }}-spm-${{ hashFiles('**/pubspec.yaml', 'ios/Runner.xcodeproj/project.pbxproj') }}
    restore-keys: |
      ${{ runner.os }}-spm-

Troubleshooting & Conflict Resolution

Package Resolution Failure

If SPM dependencies fail to resolve during CLI builds:

# 1. Clean the project
flutter clean

# 2. Resolve package dependencies manually via Xcode CLI
xcodebuild -resolvePackageDependencies -workspace ios/Runner.xcworkspace -scheme Runner
Version Conflicts

If multiple plugins request incompatible versions of the same Swift Package:

  1. Open the project in Xcode.
  2. Navigate to Runner > Package Dependencies.
  3. Manually override the package dependency rule to enforce a unified, compatible version.

Comments

Markdown supported