Skip to content

About

Google MLKit for iOS in Swift Package Manager

Topics

Resources

Stars

156 stars

Watchers

4 watching

Forks

Repository files navigation

Google ML Kit for Swift Package Manager

Google ships ML Kit only through CocoaPods. This project rebuilds those pods as XCFrameworks, publishes them as GitHub Release assets, and exposes every user-facing ML Kit iOS API as a SwiftPM library product.

Requirements

  • iOS 15.5 or later (the minimum the ML Kit 9 pods declare)
  • Xcode 15 or later to consume the package; Xcode with Swift 6.4 to build it

Apple Silicon simulators are supported — see Simulator support.

Installation

dependencies: [
    .package(url: "https://github.com/d-date/google-mlkit-swiftpm", from: "9.0.4")
]

Then add the modules you need:

.target(
    name: "YourTarget",
    dependencies: [
        .product(name: "MLKitBarcodeScanning", package: "google-mlkit-swiftpm"),
        .product(name: "MLKitTextRecognition", package: "google-mlkit-swiftpm"),
    ]
)

Three steps are not optional. Skip any of them and the module fails at runtime rather than at build time:

  1. Add the linker flags
  2. Add the resource bundles your modules need
  3. Nothing else — but read Simulator support if you are coming from an older release with EXCLUDED_ARCHS set.

Upgrading to 9.0.4? It is built with swiftpods instead of the old Makefile pipeline and raises the minimum to iOS 15.5. Resolving it prints two #warnings; see Dependency overrides.

Upgrading from 9.0.0 or 9.0.0-1? 9.0.3 is the same upstream ML Kit 9.0.0 build, repackaged. Since 9.0.1, text recognition, pose detection and selfie segmentation ship their models at all, a single product can be adopted without undefined symbols, the simulator slice covers arm64, and App Store Connect accepts the upload. If you set EXCLUDED_ARCHS[sdk=iphonesimulator*] = arm64 for an earlier release, remove it.

1. Add the linker flags

Add these to Other Linker Flags for your app target:

  • -ObjC
  • -all_load

ML Kit ships as static archives that register classes and models through Objective-C categories and static initialisers. Without these flags the linker drops the archive members that hold them, and you get unrecognized selector or a model that refuses to load.

2. Add the resource bundles

Every ML Kit model lives in a resource bundle, and Swift Package Manager cannot carry a resource bundle inside a binary target. Each bundle is published as a separate release asset: download the ones your modules need, add them to your Xcode project, and make sure they land in Copy Bundle Resources of your app target.

./scripts/download_bundles.sh 9.0.4
Bundle Needed by
GoogleMVFaceDetectorResources.bundle MLKitFaceDetection
LatinOCRResources.bundle MLKitTextRecognition
ChineseOCRResources.bundle MLKitTextRecognitionChinese
DevanagariOCRResources.bundle MLKitTextRecognitionDevanagari
JapaneseOCRResources.bundle MLKitTextRecognitionJapanese
KoreanOCRResources.bundle MLKitTextRecognitionKorean
MLKitImageLabelingResources.bundle MLKitImageLabeling
MLKitObjectDetectionResources.bundle MLKitObjectDetection
MLKitObjectDetectionCommonResources.bundle MLKitImageLabeling, MLKitImageLabelingCustom, MLKitObjectDetection, MLKitObjectDetectionCustom
MLKitPoseDetectionFastResources.bundle MLKitPoseDetection
MLKitPoseDetectionAccurateResources.bundle MLKitPoseDetectionAccurate
MLKitPoseDetectionCommonResources.bundle MLKitPoseDetection, MLKitPoseDetectionAccurate
MLKitSegmentationSelfieResources.bundle MLKitSegmentationSelfie
MLKitSegmentationCommonResources.bundle MLKitSegmentationSelfie
MLKitXenoResources.bundle MLKitPoseDetection, MLKitPoseDetectionAccurate, MLKitSegmentationSelfie
MLKitTranslate_resource.bundle MLKitTranslate
MLKitDigitalInkRecognition_resource.bundle MLKitDigitalInkRecognition
PredictOnDeviceResource.bundle MLKitSmartReply

PredictOnDevice_resource.bundle is also published: it is the same content under the name ML Kit nests inside its own framework, which is what releases before 9.0.1 shipped. Add whichever your build already refers to; adding both is harmless.

Since 9.0.4 the bundles of the two pods built from source are published too: GoogleToolboxForMac_Privacy.bundle, GoogleToolboxForMac_Logger_Privacy.bundle and SSZipArchive.bundle.

A missing bundle shows up as a runtime throw — text recognition, for example, raises MLKTextRecognizerInternalErrorCreationFailure with "Invalid model path.".

Products

Add only the products you use; each one links just the frameworks it needs.

Vision

Product What it does
MLKitBarcodeScanning Scan and decode barcodes
MLKitFaceDetection Detect faces, contours and facial features
MLKitTextRecognition Recognise Latin-script text (v2)
MLKitTextRecognitionChinese Chinese text
MLKitTextRecognitionDevanagari Devanagari text
MLKitTextRecognitionJapanese Japanese text
MLKitTextRecognitionKorean Korean text
MLKitImageLabeling Label objects, places and activities
MLKitImageLabelingCustom Image labeling with your own model
MLKitObjectDetection Detect and track objects
MLKitObjectDetectionCustom Object detection with your own model
MLKitPoseDetection Body pose detection
MLKitPoseDetectionAccurate Body pose detection, accurate model
MLKitSegmentationSelfie Separate people from the background

Language

Product What it does
MLKitLanguageID Identify the language of a string
MLKitTranslate Translate between languages on device
MLKitSmartReply Suggest contextual replies
MLKitEntityExtraction Find addresses, dates, phone numbers and the like in text
MLKitDigitalInkRecognition Recognise handwriting and drawn strokes

Simulator support

ML Kit's pre-built binaries contain no arm64 simulator code, and Xcode 26 dropped Rosetta simulators — so on Apple Silicon the published XCFrameworks used to be unusable in the Simulator entirely.

Since 9.0.1 the arm64 simulator slice is synthesised from the device slice by rewriting the Mach-O platform (done by swiftpods package since 9.0.4). Published XCFrameworks carry arm64 for iphoneos and arm64 + x86_64 for iphonesimulator, so no EXCLUDED_ARCHS workaround is needed.

Dependency overrides

Two Google dependencies are declared wider than the ML Kit pods accept, in Podfile.swift with overridingPodRequirements: .unsafe(reason:), because current Firebase needs them:

  • gtm-session-fetcher 3.5.0..<6.0.0 (the pods require >= 3.3.2, < 4.0); the ABI was checked by hand for Firebase 13 (#124, #125).
  • nanopb 2.30910.0..<2.30911.0 (the pods require ~> 3.30910.0); Firebase 13.1.1 requires 2.30910.1, a fork commit of the pods' 2.30910.0.

The generated Package.swift starts with one #warning per override, which swift package resolve prints when it evaluates the manifest. It is expected and does not need action.

Known limitations

  • No dSYMs. Google ships ML Kit without debug info, so Xcode's archive step reports "Upload Symbols Failed" for each ML Kit framework. The frameworks are linked statically, so their symbols end up in your app's own dSYM; the warnings are spurious and do not block submission. You could not symbolicate inside ML Kit anyway — it is closed source.
  • Resource bundles are manual. Swift Package Manager has no way to ship them inside a binary target. See above.
  • Digital ink recognition and entity extraction download their models at runtime from Google, so they ship no bundled model and need network access the first time you use them.
  • The wrapper version is not the pod version. Major and minor follow ML Kit; the patch number counts wrapper releases. 9.0.4 repackages upstream ML Kit 9.0.0; the pod versions each framework reports are Google's own.

Example app

./scripts/download_bundles.sh 9.0.4
cd Example && open Example.xcworkspace

Set code signing to your own team. The app runs on a device and on the Apple Silicon Simulator, and exercises every module.

Maintaining this package

The package is built by swiftpods from Podfile.swift. swiftpods package builds the pods, writes every *.xcframework.zip and *.bundle.zip to build/distribution/, regenerates Package.swift (do not edit it by hand) and links every product on its own. CLAUDE.md and AUTOMATION.md have the details.

swiftpods package --version <version>        # build into build/distribution/, regenerate Package.swift
./scripts/verify_local_archive.sh            # run ML Kit on the Simulator, archive for device
./scripts/verify_app_store_upload.sh --upload   # Apple-side validation and delivery
scripts/release.sh <version>                 # create the GitHub Release and upload the zips

verify_local_archive.sh is the gate that matters: it runs inference on the arm64 Simulator, launches the app, archives for device, and checks that every model bundle reached the app and that nothing was linked dynamically. A missing model bundle builds and archives perfectly and only fails when inference runs, so build-time checks alone are not enough.

To pick up a new upstream ML Kit version, raise the GoogleMLKit/* requirements in Podfile.swift, run swiftpods update, and release with the ML Kit major.minor (ruby scripts/check_mlkit_version.rb reports whether one is out). Normally run the Build MLKit XCFrameworks workflow from the Actions tab; it packages, commits, tags and calls scripts/release.sh.

About

Google MLKit for iOS in Swift Package Manager

Topics

Resources

Stars

156 stars

Watchers

4 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages