Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add dependency on a local swift package in Xcode 11

I am trying to add a dependency on a local swift package in my Xcode project. (I don't want to add a dependency using a file url file://me/dev/app/package as this cannot be shared to other environments)

I tried to drag & drop the package to the target. This creates a references to the package in the project that I was able to add in the "Link Binary With Libraries" build phase, but the library is still not found.

No such module 'Analytics'

My original configuration

enter image description here

After drag & drop

enter image description here

and here is my package configuration:

// swift-tools-version:5.1
import PackageDescription

let package = Package(
    name: "Analytics",
    products: [
        .library(name: "Analytics", targets: ["Analytics"]),
    ],
    dependencies: [
        .package(path: "../SomeOtherPackage"),
    ],
    targets: [
        .target(name: "Analytics", dependencies: [
            "SomeOtherPackage",
        ]),
    ]
)

I've also included the Package in the target linked binaries and/or bundle resources and/or compiled sources to no avail:

enter image description here

I've read this long discussion that doesn't seem to be conclusive.

Here is a simple example of what I'm trying: https://github.com/gsabran/SPM_Xcode_test

like image 533
Guig Avatar asked Dec 01 '19 01:12

Guig


People also ask

How do I update Swift package dependencies?

To resolve, Open the project from the project panel, select the project (not the targets), then select the "Swift Packages" tab. Double click on the package you want to update and change the minimum version to the next major version.

How do I add a Swift package to Xcode project from github?

Open your Xcode project, navigate the File tab within the macOS bar, and click on “Add Packages”. In the Add New Package window, you can select from recently used or Apple Swift Packages. Alternatively, you can search for a package via the name or the URL to the Github page.

Where are Swift package dependencies stored?

In order to cache the dependencies, they should be located in the project directory, but by default, SPM downloads the dependencies into the system folder: ~/Library/Developer/Xcode/DerivedData.

How do I add a local package?

Select your project in the Project navigator, then select your app target and navigate to its General pane. Click the + button in the “Frameworks, Libraries, and Embedded Content” section, select the local package's library product, and add it as a dependency.


1 Answers

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.

like image 169
Asperi Avatar answered Oct 03 '22 22:10

Asperi