Adding Local Dependencies in Xcode11 Using Spm

Adding Local dependencies in XCode11 using SPM

This is the way I did it :

  • Drag and drop your package folder (in my example "DataStructures") from the finder directly into the Frameworks group of your target. You will see that the dropped item take a brown folder color (you can use the arrow to "get into it").
  • Go to your project target page, in the "Framework and Libraries" click the "+" button. Your package should show up in the "Workspace" area as a library.

Sample Image

Add dependency on a local swift package in Xcode 11

Here is literally what I do and it just works... Xcode 11.2

I. Prepare package

  1. File > New > Swift Package > Select Empty Folder (or create new) > Enter Library Name (MyLibrary)

  2. Here in project find MyLibrary.swift and make public structure & variable, so there is some export to use in app (I made static constant and fixed UT)

  3. Build > OK > Close Project

II. Prepare project

  1. File > New > Project > iOS > Single View App > Enter Name (MyApp) > Next > Select Same Folder as above (it is by default) > Create

  2. Build > OK

  3. From Finder drag MyLibrary folder right below project name in Xcode Project Navigator

  4. Build > OK

  5. Click MyApp project icon in Project Navigator > Select MyApp application target > section Frameworks, Libraries, … click + > In topmost Workspace section select MyLibrary > Add

  6. Clean > Build > OK

  7. Open ContentView.swift > type import MyLibrary (autocompletion already see it)
    Replace “Hello, World” with "(MyLibrary.text)"

  8. Build & Run > OK

That’s it.

To verify used relative path open project.pbxproj in TextEdit, here is screenshot

Swift package relative path

Update:

Note1 - I've considered some delay in package resolving after Clean, so during some period of time, 1-2 mins here, Build fails exactly with reported error, but after that delay it is visible that index restarted and following Build succeeded.

Note2 - Adding second dependent package in graph (MyApp > MyLibrary > AnotherLibrary) is the same.

Import local SPM Library in local SPM package

There are a couple of issues to resolve here.

(If a requirement of your design is to use 'dynamic' linking, then this approach may not work for you.)

  • type: .dynamic:

Unless you absolutely need to guarantee how library linking is achieved, it is recommended that you leave this as the default value of nil (just remove the line). This allows the swift package manager to determine how to best link the libraries (the default being 'static').

  • .package(path: "../LocationService/Sources/LocationLiveClient"),

LocationLiveClient is a product & target of the LocationService package. In the dependencies here, a reference to the package as a whole should be made. So change this to .package(path: "../LocationService"),

  • dependencies: ["RouterService", "LocationLiveClient"])

Once the change to depend on the whole location service package, the compiler needs a little extra information. You can update your target dependencies to specifically use the LocationLiveClient library in the LocationService package: .product(name: "LocationLiveClient", package: "LocationService").

With those changes in place you end up with a Package definition like this:

let package = Package(
name: "HomePage",
products: [
.library(
name: "HomePage",
targets: ["HomePage"]),
],
dependencies: [
.package(path: "../RouterService"),
.package(path: "../LocationService"),
],
targets: [
.target(
name: "HomePage",
dependencies: [
"RouterService",
.product(name: "LocationLiveClient", package: "LocationService")
]
),
]
)

You should then be able to import LocationLiveClient as expected.


Side note: Assuming your 'LocationService' package has the following folder structure, then you can safely remove path: "Sources/LocationLiveClient" from your LocationLiveClient target definition.

LocationService
-> Sources
-> LocationService
-> LocationLiveClient

How do I use a local SPM package without a git repository?

Note that even for a local directory you have to create a git repository (local). Just run git init in the local directory, git commit and add a tag, e.g. git tag 1.0.0.

Save packages downloaded by SPM into project GIT using Xcode 11

SwiftPM integration has been setup to prevent this. It clones the files into a DerivedData/ProjectName-[RandomStuff]. You should commit your Package.resolved into the repo to ensure that you get the same version of each dependency across clones of the project.



Related Topics



Leave a reply



Submit