Skip to content

feat(darwin): add Swift Package Manager support by cherry-picking upstream SPM commits#30

Closed
0Chencc wants to merge 7 commits into
Predidit:mainfrom
0Chencc:feat/spm-support
Closed

feat(darwin): add Swift Package Manager support by cherry-picking upstream SPM commits#30
0Chencc wants to merge 7 commits into
Predidit:mainfrom
0Chencc:feat/spm-support

Conversation

@0Chencc
Copy link
Copy Markdown

@0Chencc 0Chencc commented May 21, 2026

背景

上游 media-kit/media-kit 自 2026-05 起加入了 Swift Package Manager (SPM) 支持(#1412),让 `media_kit_video` 和 `media_kit_libs_{ios,macos}_video` 这几个 plugin package 可以被 Flutter 3.44+ 通过 SPM 而非 CocoaPods 集成。

本 fork (`Predidit/media-kit`) 目前 main HEAD 是 `21aacaf9`,stops at 2026 之前的状态,没有这部分上游改动,因此下游用户(如 Kazumi)的 macOS / iOS 集成仍只能走 CocoaPods 兜底。

改动内容

本 PR 把上游的 7 个 SPM 相关 commit 干净地 cherry-pick 到本 fork:

Cherry-picked SHA 原始 SHA 来源 commit
`f88f73cb` `b505adfb` Add SPM to media_kit_video
`588f2e3e` `1ef82966` New xcframework (#1)
`49f8d81d` `d37a5e5e` Update package.swift and checksums
`c49077d9` `2f5e6fe4` feat: add Mpv library product to iOS and macOS audio SPM packages
`75342734` `5c8719f2` chore: restore original minimum OS deployment targets
`efedbb67` `b623d0b5` chore: restore original Flutter/Dart SDK constraints
`7aad695e` `672ac777` feat: make native libs dependency optional in SPM manifests

改动后的 4 个 Package.swift

  • `media_kit_video/macos/media_kit_video/Package.swift`
  • `media_kit_video/ios/media_kit_video/Package.swift`
  • `libs/macos/media_kit_libs_macos_video/macos/media_kit_libs_macos_video/Package.swift`
  • `libs/ios/media_kit_libs_ios_video/ios/media_kit_libs_ios_video/Package.swift`

上游设计的 `packageExists()` 函数被完整保留 —— 它在 manifest evaluation 时探测 sibling SPM package 是否存在,让 `media_kit_video` 即使 user 没装 libs 包也能编译(fallback 到 stub)。

设计要点(来自上游)

```swift
func packageExists(at relativePath: String) -> Bool {
let base = URL(fileURLWithPath: #file).deletingLastPathComponent()
let path = base.appendingPathComponent(relativePath).standardized.path
return FileManager.default.fileExists(atPath: path)
}

let libsPath = "../media_kit_libs_macos_video"
let hasLibs = packageExists(at: libsPath)

let package = Package(
...
dependencies: hasLibs ? [.package(...)] : [],
...
)
```

⚠️ Warning(保留上游原文):增删 libs 包后需手动清缓存:
`rm -rf ~/Library/Caches/org.swift.swiftpm/`

不改的内容

  • 本 fork 自有的 ahead commit(win32 改动、OHOS 支持、ad blocker、各平台 binary bump 等)全部保留,cherry-pick 全部 clean apply 或合理合并
  • 其他平台(Linux/Android/Windows/OHOS)配置完全不变

验证情况

在下游 Kazumi (Flutter 3.44.0) 验证:

  • ✅ `flutter pub get` 后 `media_kit_video` / `media_kit_libs_macos_video` 出现在 SPM packages 列表
  • ✅ `flutter build macos --debug` / `--release` 均通过
  • ✅ 实测在 macOS 上启动 + 视频播放 + 弹幕渲染 + 音量控制全部正常
  • ✅ macOS Pods 兜底列表从 6 降到 3(仅剩 flutter_inappwebview_macos / screen_retriever_macos / tray_manager)

下游影响

合入后,Kazumi 等下游项目只需把 pubspec.yaml 里 `ref: 21aacaf...` 升到本 PR 的 merge commit SHA,即可让 `media_kit_video` / `media_kit_libs_{ios,macos}_video` 自动切到 SPM 集成,无需任何代码改动。

Carapacik and others added 7 commits May 21, 2026 15:51
Revert the deployment target bumps (macOS 10.15→10.9, iOS 13.0→9.0).
The lower values match the actual targets encoded in the xcframework
binaries from media-kit/libmpv-darwin-build and should be preferred.
The minimum Flutter version was bumped to 3.27.4 to support Swift Package
Manager, but CocoaPods is still supported. There is no reason to force
developers onto a recent Flutter version until SPM becomes the only
supported option.
Previously, the SPM manifests for iOS and macOS unconditionally declared
`media_kit_libs_{ios,macos}_video` as a required dependency, causing the
build to fail if the package was not included in the developer's
`pubspec.yaml`.

The dependency is now resolved dynamically at manifest evaluation time by
checking whether the sibling package directory exists in the SPM ephemeral
packages directory. If found, `media_kit_video` is linked against it as
before; otherwise it falls back to a stub implementation, mirroring the
existing CocoaPods behavior in `media_kit_utils.rb`.

Note: after adding or removing the libs package from `pubspec.yaml`,
the SPM cache must be cleared manually:

    rm -rf ~/Library/Caches/org.swift.swiftpm/
@Predidit
Copy link
Copy Markdown
Owner

这个PR现在没有指向我们的自定义 libmpv 二进制构建,我们需要自己的自定义构建来实现广告过滤等功能

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants