Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

docs.flutter.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.

Learn more

Flutter 3.41 is live! Check out theFlutter 3.41 blog post!

Swift Package Manager for app developers

How to use Swift Package Manager for native iOS or macOS dependencies

Warning

Flutter is migrating toSwift Package Manager to manage iOS and macOS native dependencies. Flutter's support of Swift Package Manager is under development. If you find a bug in Flutter's Swift Package Manager support,open an issue. Swift Package Manager support isoff by default. Flutter continues to support CocoaPods.

Flutter's Swift Package Manager integration has several benefits:

  1. Provides access to the Swift package ecosystem. Flutter plugins can use the growing ecosystem ofSwift packages.
  2. Simplifies Flutter installation. Xcode includes Swift Package Manager. You don't need to install Ruby and CocoaPods if your project uses Swift Package Manager.

How to turn on Swift Package Manager

#

Flutter's Swift Package Manager support is turned off by default. To turn it on:

  1. Upgrade to the latest Flutter SDK:

    sh
    flutter upgrade
  2. Turn on the Swift Package Manager feature:

    sh
    flutter config --enable-swift-package-manager

Using the Flutter CLI to run an appmigrates the project to add Swift Package Manager integration. This makes your project download the Swift packages that your Flutter plugins depend on. An app with Swift Package Manager integration requires Flutter version 3.24 or higher. To use an older Flutter version, you will need toremove Swift Package Manager integration from the app.

Flutter falls back to CocoaPods for dependencies that do not support Swift Package Manager yet.

How to turn off Swift Package Manager

#
Plugin authors

Disabling Swift Package Manager causes Flutter to use CocoaPods for all dependencies. However, Swift Package Manager remains integrated with your project. To remove Swift Package Manager integration completely from your project, follow theHow to remove Swift Package Manager integration instructions.

Turn off for a single project

#

In the project'spubspec.yaml file, under theflutter section, setenable-swift-package-manager tofalse in theconfig subsection.

pubspec.yaml
yaml
# The following section is specific to Flutter packages.flutter:config:enable-swift-package-manager:false

This turns off Swift Package Manager for all contributors to this project.

Migrating from deprecated syntax

If you were previously usingdisable-swift-package-manager: true, update yourpubspec.yaml to use the newconfig section format shown above. The old syntax is deprecated and will produce an error in Flutter 3.38 and later.

Run the following command:

sh
flutter config --no-enable-swift-package-manager

This turns off Swift Package Manager for the current user.

If a project is incompatible with Swift Package Manager, all contributors need to run this command.

How to add Swift Package Manager integration

#

Add to a Flutter app

#

Once youturn on Swift Package Manager, the Flutter CLI tries to migrate your project the next time you run your app using the CLI. This migration updates your Xcode project to use Swift Package Manager to add Flutter plugin dependencies.

To migrate your project:

  1. Turn on Swift Package Manager.

  2. Run the iOS app using the Flutter CLI.

    If your iOS project doesn't have Swift Package Manager integration yet, the Flutter CLI tries to migrate your project and outputs something like:

    flutter runAdding Swift Package Manager integration...

    The automatic iOS migration modifies theios/Runner.xcodeproj/project.pbxproj andios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme files.

  3. If the Flutter CLI's automatic migration fails, follow the steps inadd Swift Package Manager integration manually.

[Optional] To check if your project is migrated:

  1. Run the app in Xcode.

  2. Ensure thatRun Prepare Flutter Framework Script runs as a pre-action and thatFlutterGeneratedPluginSwiftPackage is a target dependency.

    Ensure **Run Prepare Flutter Framework Script** runs as a pre-action

    EnsureRun Prepare Flutter Framework Script runs as a pre-action

Once youturn on Swift Package Manager, the Flutter CLI tries to migrate your project the next time you run your app using the CLI. This migration updates your Xcode project to use Swift Package Manager to add Flutter plugin dependencies.

To migrate your project:

  1. Turn on Swift Package Manager.

  2. Run the macOS app using the Flutter CLI.

    If your macOS project doesn't have Swift Package Manager integration yet, the Flutter CLI tries to migrate your project and outputs something like:

    flutter run -d macosAdding Swift Package Manager integration...

    The automatic iOS migration modifies themacos/Runner.xcodeproj/project.pbxproj andmacos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme files.

  3. If the Flutter CLI's automatic migration fails, follow the steps inadd Swift Package Manager integration manually.

[Optional] To check if your project is migrated:

  1. Run the app in Xcode.

  2. Ensure thatRun Prepare Flutter Framework Script runs as a pre-action and thatFlutterGeneratedPluginSwiftPackage is a target dependency.

    Ensure **Run Prepare Flutter Framework Script** runs as a pre-action

    EnsureRun Prepare Flutter Framework Script runs as a pre-action

Add to a Flutter appmanually

#

Once youturn on Swift Package Manager, the Flutter CLI tries to migrate your project to use Swift Package Manager the next time you run your app using the CLI.

However, the Flutter CLI tool might be unable to migrate your project automatically if there are unexpected modifications.

If the automatic migration fails, use the steps below to add Swift Package Manager integration to a project manually.

Before migrating manually,file an issue; this helps the Flutter team improve the automatic migration process. Include the error message and, if possible, include a copy of the following files in your issue:

  • ios/Runner.xcodeproj/project.pbxproj
  • ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme (or the xcsheme for the flavor used)

Step 1: Add FlutterGeneratedPluginSwiftPackage Package Dependency

#
  1. Open your app (ios/Runner.xcworkspace) in Xcode.

  2. Navigate toPackage Dependencies for the project.

    The project's package dependencies

    The project's package dependencies

  3. Click theadd button.

  4. In the dialog that opens, clickAdd Local....

  5. Navigate toios/Flutter/ephemeral/Packages/FlutterGeneratedPluginSwiftPackage and clickAdd Package.

  6. Ensure that it's added to theRunner target and clickAdd Package.

    Ensure that the package is added to the `Runner` target

    Ensure that the package is added to theRunner target

  7. Ensure thatFlutterGeneratedPluginSwiftPackage was added toFrameworks, Libraries, and Embedded Content.

    Ensure that `FlutterGeneratedPluginSwiftPackage` was added to **Frameworks, Libraries, and Embedded Content**

    Ensure thatFlutterGeneratedPluginSwiftPackage was added toFrameworks, Libraries, and Embedded Content

Step 2: Add Run Prepare Flutter Framework Script Pre-Action

#

The following steps must be completed for each flavor.

  1. Go toProduct > Scheme > Edit Scheme.

  2. Expand theBuild section in the left side bar.

  3. ClickPre-actions.

  4. Click theadd button and selectNew Run Script Action from the menu.

  5. Click theRun Script title and change it to:

    Run Prepare Flutter Framework Script
  6. Change theProvide build settings from to theRunner app.

  7. Input the following in the text box:

    sh
    "$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" prepare
    Add **Run Prepare Flutter Framework Script** build pre-action

    AddRun Prepare Flutter Framework Script build pre-action

Step 3: Run app

#
  1. Run the app in Xcode.

  2. Ensure thatRun Prepare Flutter Framework Script runs as a pre-action and thatFlutterGeneratedPluginSwiftPackage is a target dependency.

    Ensure **Run Prepare Flutter Framework Script** runs as a pre-action

    EnsureRun Prepare Flutter Framework Script runs as a pre-action

  3. Ensure that the app runs on the command line withflutter run.

Once youturn on Swift Package Manager, the Flutter CLI tries to migrate your project to use Swift Package Manager the next time you run your app using the CLI.

However, the Flutter CLI tool might be unable to migrate your project automatically if there are unexpected modifications.

If the automatic migration fails, use the steps below to add Swift Package Manager integration to a project manually.

Before migrating manually,file an issue; this helps the Flutter team improve the automatic migration process. Include the error message and, if possible, include a copy of the following files in your issue:

  • macos/Runner.xcodeproj/project.pbxproj
  • macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme (or the xcscheme for the flavor used)

Step 1: Add FlutterGeneratedPluginSwiftPackage Package Dependency

#
  1. Open your app (macos/Runner.xcworkspace) in Xcode.

  2. Navigate toPackage Dependencies for the project.

    The project's package dependencies

    The project's package dependencies

  3. Click theadd button.

  4. In the dialog that opens, click theAdd Local....

  5. Navigate tomacos/Flutter/ephemeral/Packages/FlutterGeneratedPluginSwiftPackage and click theAdd Package.

  6. Ensure that it's added to the Runner Target and clickAdd Package.

    Ensure that the package is added to the `Runner` target

    Ensure that the package is added to theRunner target

  7. Ensure thatFlutterGeneratedPluginSwiftPackage was added toFrameworks, Libraries, and Embedded Content.

    Ensure that `FlutterGeneratedPluginSwiftPackage` was added to **Frameworks, Libraries, and Embedded Content**

    Ensure thatFlutterGeneratedPluginSwiftPackage was added toFrameworks, Libraries, and Embedded Content

Step 2: Add Run Prepare Flutter Framework Script Pre-Action

#

The following steps must be completed for each flavor.

  1. Go toProduct > Scheme > Edit Scheme.

  2. Expand theBuild section in the left side bar.

  3. ClickPre-actions.

  4. Click theadd button and selectNew Run Script Action from the menu.

  5. Click theRun Script title and change it to:

    Run Prepare Flutter Framework Script
  6. Change theProvide build settings from to theRunner target.

  7. Input the following in the text box:

    sh
    "$FLUTTER_ROOT"/packages/flutter_tools/bin/macos_assemble.sh prepare
    Add **Run Prepare Flutter Framework Script** build pre-action

    AddRun Prepare Flutter Framework Script build pre-action

Step 3: Run app

#
  1. Run the app in Xcode.

  2. Ensure thatRun Prepare Flutter Framework Script runs as a pre-action and thatFlutterGeneratedPluginSwiftPackage is a target dependency.

    Ensure `Run Prepare Flutter Framework Script` runs as a pre-action

    EnsureRun Prepare Flutter Framework Script runs as a pre-action

  3. Ensure that the app runs on the command line withflutter run.

Add to an existing app (add-to-app)

#

Flutter's Swift Package Manager support doesn't work with add-to-app scenarios.

To keep current on status updates, consultflutter#146957.

Add to a custom Xcode target

#

Your Flutter Xcode project can have customXcode targets to build additional products, like frameworks or unit tests. You can add Swift Package Manager integration to these custom Xcode targets.

Follow the steps inHow to add Swift Package Manager integration to a projectmanually.

InStep 1, list item 6 use your custom target instead of theFlutter target.

InStep 2, list item 6 use your custom target instead of theFlutter target.

How to remove Swift Package Manager integration

#

To add Swift Package Manager integration, the Flutter CLI migrates your project. This migration updates your Xcode project to add Flutter plugin dependencies.

To undo this migration:

  1. Turn off Swift Package Manager.

  2. Clean your project:

    sh
    flutter clean
  3. Open your app (ios/Runner.xcworkspace ormacos/Runner.xcworkspace) in Xcode.

  4. Navigate toPackage Dependencies for the project.

  5. Click theFlutterGeneratedPluginSwiftPackage package, then click theremove button.

    The `FlutterGeneratedPluginSwiftPackage` to remove

    TheFlutterGeneratedPluginSwiftPackage to remove

  6. Navigate toFrameworks, Libraries, and Embedded Content for theRunner target.

  7. ClickFlutterGeneratedPluginSwiftPackage, then click theremove button.

    The `FlutterGeneratedPluginSwiftPackage` to remove

    TheFlutterGeneratedPluginSwiftPackage to remove

  8. Go toProduct > Scheme > Edit Scheme.

  9. Expand theBuild section in the left side bar.

  10. ClickPre-actions.

  11. ExpandRun Prepare Flutter Framework Script.

  12. Click thedelete button.

    The build pre-action to remove

    The build pre-action to remove

How to use a Swift Package Manager Flutter plugin that requires a higher OS version

#

If a Swift Package Flutter Manager plugin requires a higher OS version than the project, you might get an error like this:

Target Integrity (Xcode): The package product 'plugin_name_ios' requires minimum platform version 14.0 for the iOS platform, but this target supports 12.0

To use the plugin:

  1. Open your app (ios/Runner.xcworkspace ormacos/Runner.xcworkspace) in Xcode.

  2. Increase your app's targetMinimum Deployments.

    The target's **Minimum Deployments** setting

    The target'sMinimum Deployments setting

  3. If you updated your iOS app'sMinimum Deployments, regenerate the iOS project's configuration files:

    sh
    flutter build ios --config-only
  4. If you updated your macOS app'sMinimum Deployments, regenerate the macOS project's configuration files:

    sh
    flutter build macos --config-only
Was this page's content helpful?

Unless stated otherwise, the documentation on this site reflects Flutter 3.38.6. Page last updated on 2025-12-08.View source orreport an issue.


[8]ページ先頭

©2009-2026 Movatter.jp