Section 10/151 menit
10. Migrasi dari CocoaPods ke SPM
10. Migrasi dari CocoaPods ke SPM
Cek Apakah Library Tersedia di SPM
swift
# Cara cek apakah pod sudah tersedia di SPM:
# 1. Cek README library — biasanya ada bagian "Swift Package Manager"
# 2. Cek apakah ada Package.swift di root repo GitHub
# 3. Gunakan SwiftPackageIndex.com — search library
# Contoh library yang sudah pindah ke SPM:
# ✓ Alamofire → https://github.com/Alamofire/Alamofire
# ✓ Kingfisher → https://github.com/onevcat/Kingfisher
# ✓ SnapKit → https://github.com/SnapKit/SnapKit
# ✓ Lottie → https://github.com/airbnb/lottie-ios
# ✓ SwiftyJSON → https://github.com/SwiftyJSON/SwiftyJSON
# ✓ RxSwift → https://github.com/ReactiveX/RxSwift
# Library yang masih perlu CocoaPods atau workaround (per 2025):
# ⚠ Firebase → ada SPM (tidak semua modul), tapi lebih kompleks
# ⚠ Google Ads → masih CocoaPods-primary
# ⚠ Some Mapbox → versi tertentu CocoaPods only
Langkah Migrasi Bertahap
swift
# Fase 1: Audit dependencies
# Buat spreadsheet/tabel:
# | Pod | SPM Available? | Risk | Priority |
# |-----|----------------|------|----------|
# | Alamofire | ✓ | Low | High |
# | Firebase/Analytics | Partial | High | Low |
# Fase 2: Mulai dari leaf dependencies (yang tidak punya dep lain)
# Pod yang tidak bergantung ke pod lain → migrasi lebih mudah
# Fase 3: Update Podfile (hapus yang sudah ke SPM)
# Sebelum:
pod 'Alamofire', '~> 5.9'
pod 'Kingfisher', '~> 7.0'
pod 'Firebase/Analytics'
# Sesudah (setelah Alamofire dan Kingfisher ke SPM):
pod 'Firebase/Analytics' # hanya yang belum ada di SPM
swift
# Fase 4: Tambahkan via Xcode SPM
# File → Add Package Dependencies → masukkan URL
# Fase 5: Update imports di source code
# Biasanya sama (nama module umumnya identik)
# import Alamofire → tetap sama
# import Kingfisher → tetap sama
# Fase 6: Update CI workflow
# Hapus CocoaPods cache jika Pods/ kosong
# Tambahkan SPM cache
# Fase 7: Verifikasi build dan test berjalan normal
# Fase 8: Remove Podfile jika semua sudah ke SPM (opsional)
Migrasi Firebase ke SPM (Kasus Kompleks)
swift
// Firebase ke SPM: perlu Package.swift yang tepat
// Di Xcode: File → Add Package Dependencies
// URL: https://github.com/firebase/firebase-ios-sdk
// Pilih modul yang dibutuhkan:
// - FirebaseAnalytics
// - FirebaseCrashlytics
// - FirebaseMessaging
// - FirebaseAuth
// dll.
// CATATAN: Firebase SPM lebih besar dari CocoaPods karena semua
// modul ada dalam satu package. Download pertama kali bisa lama.
// Untuk Crashlytics, butuh upload dSYMs:
// Build Phase → Run Script:
// "${BUILD_DIR%Build/*}SourcePackages/checkouts/firebase-ios-sdk/Crashlytics/run"
// PLIST upload untuk Crashlytics di CI:
// xcodebuild archive ...
// "${BUILD_DIR%Build/*}SourcePackages/checkouts/firebase-ios-sdk/Crashlytics/upload-symbols" \
// -gsp GoogleService-Info.plist \
// -p ios \
// MyApp.xcarchive/dSYMs