Adding a package through SPM with a binaryTarget in multiple targets in project - swift-package-manager

I have multiple targets in my project and I added a package dependency on the project that has a binaryTarget to a zip that contains my xcframework. It adds it to my project, my main target has it but when I try to add it to my other targets via General > Frameworks and Libraries it's not listed in the dialog for me to choose (nor is it in Link Binary with Libraries either.
Xcode 14.1
I can replicate this with other packages that just have binaryTarget as well like https://github.com/PSPDFKit/PSPDFKit-SP. Packages that just have source are fine.
I have tried deleting the package and then adding again after having added my new targets, tried shutting down xcode and restarting.

I had the same issue, and the solution for me was a small hack in the project.pbxproj file, as follows:
Navigate in the Finder to the project directory -> Right click on the .xcodeproj file -> Show Package Content. You will find the project.pbxproj file there.
Search for the productDependencies key. You will find as many as targets you have in your project with Embedded/Linked frameworks. If your second target does not have framework yet, just try to add a generic Swift package, so Xcode will make the necessary changes for you in the project.pbxproj
Copy the line with the given binary linked library to the required target section/list.
I hope this helps, for me it was finally working. However I think it is still some kind of bug in Xcode/SPM.

Related

How can I add a demo app target to a Swift Package?

I'm looking to include a demo app with a Swift Package of custom controls that shows implementation demos for each of the controls. Is there a way that I can include this in the Swift Package (and also develop/test the controls directly inside the package this way)?
Here are the steps that worked for me on Xcode 13:
Create a package called MyLibrary
Create a new Project App MyLibraryDemo inside the package directory
Open the project's .xcodeproj file
Go to PROJECT > MyLibraryDemo > Package Dependencies and press the 'plus' symbol
Press Add Local and select the directory containing your pacakge
Go to the app's TARGET > General > Frameworks, Libraries and Embeded Content, and press 'add'
Select your package from the list
Now you can import your MyLibrary package into your example app, and edit / update your package from within you demo project.
Some Notes:
If your package / demo has other dependencies, and those other dependencies get updated, you may have to clear DerivedData before doing an update
You may also have to close & re-open Xcode, especially if you've updated dependencies, but it still says "Cannot find package MyLibrary"
What kind of app? SwiftPM only directly supports macOS/Linux executables.
If you want an iOS/tvOS/watchOS one you will need an Xcode project that links using a relative path to find your package. This is done by creating the example app and dragging the Package's containing directory into the Xcode project. You can then link the library. If you put this example app in the repo with the package then it will be distributed along with anyone who clones the package.
One important thing I have noticed (which is almost certainly a bug in Xcode) when doing this myself is that the Package.swift and Example.xcodeproj cannot be in the same directory. You have to nest the Xcode project into another directory or it will have issues building/linking a lot of the time. So if you run into any issues With what I have suggested also try this bandaid.
I have created an example on GitHub here that works for Xcode 11.3.
The best approach is to use "Xcode workspace".
How does it work?
1. Create your artifacts
Create a directory.
Create an Xcode workspace inside there (Xcode: File: New: Workspace). Then create your library (package), and create your app (whatever type) inside the same directory.
In this step, just place the package and the app in the above directory, and close them immediately.
It should look like this.
2. Link your artifacts
Open your .xcworkspace file.
In the left bottom corner, click "+" sign.
Select "Add files to your_workspace_name", then:
a) for the Demo app
Navigate to the app's directory, select Your_app.xcodeproj and hit Add.
b) for the package
Navigate to the package's directory, and select the package directory only. Before hitting Add button, make sure "Create folder references for any added folders" is selected.
Your entire workspace should look like this now. You can immediately import the library inside your app and you are ready to go.

Nuget does not update Package References in ASP.NET website

About: I have a ASP.NET website (not Web project) with 3 class library projects in the solution. Earlier I was using SVN but now Git is used source management. I have installed the git locally on a computer (used as server) and using it for merging the source code from other developers. Also, I am using Visual studio 2015 community edition which provides the tools to work with git.
Problem: After cloning the project from the master repository, I build the project to run it. Building the project shows a dialog box saying "Package Restore is in progress". This process creates a folder named "Packages" and that folder includes every package listed in the packages.config file. But after restoration completes, the project throws the following exception:
This exception shows for each package (Autofac here).
The type or namespace name 'Autofac' could not be found (are you missing a using directive or an assembly reference?)
Work around To Solve this problem, I need to uninstall each package and installed it again and problem is solved. This thing I need to do again and again for each developer machine, which is frustrating and time consuming too.
Does anyone has faced the same problem working with Nuget, git and website in ASP.NET.
I faced a problem like this before. In my case, the reason was that I changed the project path (moved the project to another directory), and the path of the packages directory (that contains the NuGet packages) was stored in the csproj file for the old path, that is VS cannot restore NuGet packages. The solution for this was to edit the csproj manually and make it referring to the correct new packages path.
If this doesn't work for you, you can still use your workaround, but using the following PowerShell command (in NuGet Console) for simplicity:
Update-Package -reinstall -Project Your.Project.Name
Note: project name doesn't contain csproj extension, just the project name
It is good practice to not put third party packages into source control. It bloats your repository (even on a large web application, the size of the external packages will massively out weigh your code).
If NuGet package restore is slow, you could look at using a local cache (this can be as simple as a shared folder) or a better internet connection.
That said, you should only have this problem once per machine. While the packages are downloading you could be giving the new team member an overview of the design…
Make sure that all of your projects are using the same target framework, when this isn't done you can often get the
type or namespace [name] could not be found
warning.
To do this, right click each of the projects in the solution explorer > Properties > Application tab > Target Framework. They should all be the same or there will be incompatibilities between the references in your projects. Here's a question regarding this, hopefully this helps.
The main reason is there no Autofac reference in packages.config file.
When you see on the screen Restore packages message box this mean that nuget package manager is trying to install all the packages which are missing in the package folder.
Try to do this step:
In Visual Studio Solution Explorer pick the project and via context menu pick Unload project
After project was unloaded via context menu pick Edit your project
Go to section group and find you Autofac Reference section
If HintPath doesnt looks like ..\packages\Autofac.4.1.1\lib\net45\Autofac.dll (Actual for 4.1.1 version) remove the Autofac reference Item
Save csproj file and reload project
Install Autofac via NuGet Package manager
Commit and push changes to git repository
Had the same problem with VS 2019. In ASP.NET, packages are updated via the .refresh files that appear in your bin folders. If these aren't checked into Git, they wouldn't be copied down to your cloned repro.
I added the .refresh files for all of my package dlls, made sure the versions and paths in the .refresh files were correct, and now everything updates as expected.

Build fails due to different package location

I'm new to realm db and tried to integrate it in our mobile app (Xamarin.Ios using Visual studio).
I've added the nuget to the PCL project as well as to the executable project
when building, the build fails with the following message:
1>C:\Projects\CoachApp-Fork\ExternalPackages\Realm.0.74.1\build\Realm.targets(6,5): error MSB3030: Could not copy the file "C:\Projects\CoachApp-Fork\Build\Solutions\packages/Realm.0.74.1/tools/RealmWeaver.Fody.dll" because it was not found.
There seems to be a build task in realm that tries to copy the dll, however in our solution, the nuget packages are located at a different location (configured via Nuget.Config).
any ideas how to resolve this? i cannot simply change the nuget package location, as the build server relies on this...
That was my clever trick you broke!
The problem is the location of the DLL for Fody is quite restrictive and yet the Xamarin templates have two different relative package directory locations depending on you having a multi-target project. (This was an improvement to cache packages per-solution, I think sometime in 2015.)
Can you put your own manual copy step into your build, at least for now, to put it there in advance? I realise this isn't an optimal solution but might work as a band-aid.

Include header files in a built shared library

I'm using Qt Creator 3.5.1 (opensource) with Qt 5.5.1. There I've created a shared library, and I can successfully build it. In the resulting build folder I find the so-file as expected, but I can't find the header files which are needed to use the library. Are the header files not expected to turn up here? I though the build folder would contain everything needed in order to make use of the library. Is that not the case?
I don't think they should be placed there. That is the build directory, not the installation directory. You can however also define the installation behaviour; see Advanced Usage - Installing Files. You choose which headers to distribute and which are private and place the public ones in the proper position.

android studio adding extern jar library

I would like to add the extern jar library commons-jexl-2.1.1.jar. I copied the jar into the libs/ folder and performed the Add as library... menu point. I don't receive any errors in the code and everything seems to work but when compiling and starting the application I receive the error java.lang.NoClassDefFoundError: org.apache.commons.jexl2.JexlEngineon this line private JexlEngine jexl = new JexlEngine();
Does anyone know what I've missed?
Unfortunately, that menu command is doing the wrong thing for Gradle-based projects, which I assume yours is. (Gradle-based projects are what you get when you create new projects in Android Studio). I've filed bug https://code.google.com/p/android/issues/detail?id=62249 to request implementing this menu command properly for these projects, or at a minimum disabling it until it's implemented to prevent confusion.
In the meantime, you can add external JAR dependencies by going through the Project Structure dialog, which will add the appropriate entries to your build.gradle build file. Choose File menu > Project Structure, and click on the "Modules" entry on the left. Choose your module from the middle list, and click on the Dependencies tab on the right. Then click on the + button at the bottom to add a new dependency. Screen shot here:
The + menu has an option for "File dependency" (pictured). You will get a file chooser that will let you select the jar file.
If your dependency is one that can be found in Maven, you may find it more convenient to specify the Maven coordinates; that way, the build system will automatically download the dependency, and you won't have to download and store the JAR manually. To set that up, choose "Maven dependency" from the + menu. You'll get a dialog where you can search to find the proper Maven coordinates for your library. In your case, those coordinates will be "org.apache.commons:commons-jexl:2.1.1#jar"
If you prefer to edit build files by hand, check out your build.gradle file after completing the Project Structure dialog changes to see what it did.
The docs for using Gradle in Android are at http://tools.android.com/tech-docs/new-build-system
courtesy The App Chaps
I've been struggling with the same thing for many hours, trying to get the Gson jar to work no less. I finally cracked it – here are the steps I took:
Put the Gson jar (in my case, gson-2.2.4.jar) into the libs folder
Right click it and hit 'Add as library'
Ensure that compile files('libs/gson-2.2.4.jar') is in your build.gradle file
Do a clean build (you can probably do this fine in Android Studio, but to make sure I navigated in a terminal to the root folder of my app and typed gradlew clean. I'm on Mac OS X, the command might be different on your system
After I did the above three, it started working fine. I think the 'Add as library' step was the one I'd previously missed, and it didn't work until I cleaned it either.
[Edit - added the build.gradle step which is also necessary as others have pointed out]

Resources