How to delete Swift Package Dependency in Xcode 11?
Asked Answered
S

12

334

I have a project in Xcode 11 that I added Swift Package Manager dependencies to. I now realized that I no longer need one of the package dependencies I previously added to my project.

As you can see below, there are no options under File > Swift Packages for deleting a packager from Swift Package Manager for this project.

Swift Packages Menu Options

I have tried removing the package from the array in the project.xcworkspace/xcshareddata/swiftpm/Package.resolved file. But it still doesn't remove it from Xcode, and the next time I Update to Latest Package Versions it readds the entry to the Package.resolved file.

How can I delete a Swift Package Manager dependency in my project?

Slicer answered 22/6, 2019 at 18:34 Comment(2)
I am unable to follow any of the answers to this question in Xcode 13 – Bayberry
Navigate to the youAppName.xcodeproj file and access the "Package Dependencies" section in the top bar. Once there, open the "Packages" tab, where you'll find a list of packages. You can delete a package there. – Garrote
C
771
  1. Open Xcode
  2. Select your project
  3. Look at the top middle
  4. Select Swift Package Manager menu

You'll be able to manage your packages (add / remove)

enter image description here

Carcinoma answered 22/6, 2019 at 22:44 Comment(11)
@CharlieFish You can right-click and delete in the sidebar all you want! It just doesn't do anything. πŸ˜‚ – Dybbuk
I tried the steps above and I still can't remove the dependency! – Particularize
Warning: Xcode 11.7 (11E801a) crashes when I attempt to remove Swift Packages this way (-[__NSSetM addObject:]: object cannot be nil, sending removeItems: to Xcode3PackageReferenceListViewController from <DVTGradientImageButton: 0x7fce1a7d83d0>) – Westphal
I don't understand the instructions. What window is this? – Freezedry
@xinux, you have to click on your project at the top of the file navigator then it will convert your center view that normally houses code or interface builder into this project view. – Towelling
The fact that I had to google it and that this is actually a stupid issue -> bad apple. – Rollerskate
Update: in Xcode 13.3 the tab is called "Package Dependencies" – Nobe
Too bad I can't upvote this twice. I ran into the same problem a second time; found this answer and realized I have already upvoted it... thanks again. – Garver
Thanks for this. Quite perplexing that Apple makes it easy to Add and Update packages, but not Remove? – Satori
Oh nice why put all relevant controls in in one place when you can scatter them across two interfaces. – Tulatulip
Also nice to see that you can select "add package dependency" from the file menu, then click "recent" (or something like that) and see the package that you just installed, you can right-click and select "remove". It will disappear from the list but remain in your project. Just in case you want to do such a thing. It's one of Xcode's amazing features. – Tulatulip
C
38

select your project and move to the package dependencies tab then select and remove

Please follow the image

enter image description here

Caruthers answered 22/2, 2023 at 11:21 Comment(2)
You'd think there would be a simple "left click -> remove" under the "Package Dependencies" lol – Counterreply
@PaulCarlton brother you are too much genius. But do you know that everything start from zero. I think no one come here for learning shortcut right. I just give a simple solution. Please don't de-motivate anyone. Sorry for interrupt – Caruthers
M
35

Swift Package Manager(SPM) Dependency

Add dependency

1. Project Settings contains information about dependencies. 
2. File -> Swift Packages -> Add Package Dependency...
3. Target -> General -> Frameworks, Libraries, and Embedded Content -> Add Items -> Add Other... -> Add Package Dependency...

Target Settings includes product from dependency

Edit dependency

To edit URL you can edit .pbxproj with repositoryURL

Delete dependency

Project -> Packages -> <Select dependency> -> -

[Local Swift Package Manager(SPM)]
[iOS Dependency manager]

Mcpherson answered 31/3, 2021 at 16:15 Comment(1)
Confirmed this is the most direct answer to the question. Reproducible in Xcode 14 – Wage
U
16

Dependencies keep poping up even after remove pod and reinstall pod.

enter image description here

Uncinus answered 14/12, 2022 at 5:11 Comment(0)
3
11

Please follow the steps below to remove a particular package from the Xcode project smoothly.

enter image description here

3d answered 19/1, 2023 at 9:11 Comment(2)
this is what finally worked for me – Mercer
well explained. – Touraco
R
2

In addition to Pierre's answer, this was driving me crazy, I had a sub project that I was editing, I forgot about that (it was in a subfolder). Even though I removed it in the "Swift Packages" pane it kept coming back. Removing that sub folder reference made sure the PM dependencies went away.

Rosas answered 11/7, 2019 at 1:46 Comment(0)
E
2

I removed the swift package, but its dependancies were still showing in the project. I saw the swift package was still in the Frameworks folder at the bottom of left pane, enter image description here

I removed it from there and the dependencies are gone.

Electrolyze answered 7/1, 2021 at 5:56 Comment(0)
C
1

Firstly I removed it from dependencies and targets in Package.swift, then i regenerated my project file with swift package generate-xcodeproj

Corpulent answered 1/11, 2019 at 12:0 Comment(0)
S
1

Nested Swift Package Dependencies

Background

As other answers have mentioned, we can import Swift Packages into a project very easy through the File -> Swift Packages -> Add Package Dependency workflow, and that works for the majority of applications. I have added this answer as a further optimisation for packages with nested dependencies.

Swift Packages are imported not just with the Git source code checkout, but also with one, or several Package Products. In my case, I wanted to keep the Package because I used it in one target but not in another target. Sometimes a Package contains multiple dependencies which we don't need, and this is a great opportunity to prune unused dependencies.

Unused imports

I recently made a mistake where I automatically imported all the modules referenced by a Swift Package dependency, even those I don't need. This is common because Packages can have multiple Products which each expose different APIs for different applications.

If you aren't sure if you need an import, check it and delete it. For example, a Package could include an Objective-C Module that adds an additional unnecessary import.

image of build phases

In my case, I imported a Swift Package that was exported via multiple nested libraries: OHHTTPStubs and OHHTTPStubsSwift in the above example.

General idea

We can delete nested Swift Package dependencies via Build Phases or the Target General settings tab without deleting the Package itself. Deleting unnecessary dependencies is a good practice to save your app's memory footprint and also streamlines build times.

Nested Dependencies for Unit/UI Testing

Each target should only import the libraries it uses.

Rules:

  1. Import only the Swift Package Manager Products you actually need when importing for the whole project. Import only the wrapper subspec if that is all you use.
  2. The Host Application Target doesn't need to import UI testing libraries. We can safely delete these libraries from the Target General tab in Frameworks, Libraries, and Embedded Content. This will automatically unlink the Product from the Build Phases tab for this Target.
  3. Our UI Testing Target can import the Package Products it needs via Build Phases -> Link Binary with Libraries. If a dependency is only used in UI Tests, delete it from the Host Application Target Frameworks, Libraries, and Embedded Content.
  4. The Unit Testing Target can't link to libraries that are not embedded in the Host Application. Thus, we need to add products used in Unit Tests to the Host Application Target in the General settings tab for Frameworks, Libraries, and Embedded Content. We DON'T need to add any Products to Link Binary with Libraries for the Unit Tests Target.

Example from my experience

OHHTTPStubsSwift is the equivalent Swift CocoaPods subspec that adds a nicer API wrapper over the ObjC API but already imports the ObjC API (OHHTTPStubs).

I deleted the Package Products from the Host Target because I was only using it in UI Tests. I then only imported the OHHTTPStubsSwift via Build Phases.

Shipe answered 28/8, 2021 at 14:38 Comment(1)
This is a better response for me than using "Swift Package" menu, because I did installed FirebaseKit, but needed to add the Database module. With the "Swift Package" menu I just could remove all than install again, choosing Database option at this step. This is not exactly the question above, but your way can do the both. – Schism
S
0

There's no answer to what I've found. If the git link has been broken, *.xcodeproj/project.pbxproj must be changed to the proper link. Xcode will catch the modification and update automatically.

Seiler answered 1/7, 2022 at 5:48 Comment(0)
S
0

try deleting the package dependencies and reinstalling using the latest major version (i.e. 10.0.0). i downloaded realm using swift package manager with the latest version 10.48.1 and couldnt import swiftRealm. make sure to add to target. after adding the dependencies it automatically updated to latest version since i used dependency rule "up to next major version"

Scoundrel answered 21/3 at 17:53 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review – Unciform
T
-1

Step 1. Navigate to your project directory. Step 2. Find 'your-project.xcodeproj' Step 3. Open it in text editor, not Xcode (you have to use finder and use any text editor by opening with-all applications -> text editor Step 4. Search for all instances of the package in question, for instance...I had a package 'UIKit' that was causing issues, and I just removed any instances of it and made sure not to disturb the rest of the file. Step 4. Open/Re-open xcode project with xcode and enjoy.

Twain answered 21/12, 2021 at 7:16 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.