How do you reference a C++ dynamic Android library in Xamarin VS2015? - xamarin

I created a Xamarin project MyApp within VS2015. This ends up creating a bunch of projects including MyApp.Droid for Android. I can build and deploy this project just fine on my Android device.
Next, I created a shared C++ Dynamic Shared Library (Android) project MyShared. When I try to add this project as a reference in MyApp.Droid, it reports an error that only .dll or .exe file types can be added as a reference and not .so types.
Wondering if there is a different way to add a native library project as a dependency. Regards.

Xamarin.Android supports the use of native libraries via the standard PInvoke mechanism.
You will need to define the DllImport C# attributes/signatures, there are various tools such as SWIG, PInvoker Visual Studio Addin, Mono's CppSharp, etc.. that can assist in this step.
(Personally I use Mono's CppSharp, but it might be overkill depending upon the number of classes/methods/functions there are within your C++ library.)
Mono Interop with Native Libraries
Xamarin.Android : Using Native Libraries
To interop with native libraries in your Xamarin.Android project, all you need to do is create a DllImport function declaration for the existing code to invoke, and the runtime will handle the rest. Set the EntryPoint to specify the exact function to be called in the native code.
Build and Debug C++ Libraries in Xamarin.Android Apps with Visual Studio 2015

Related

Requiring unreferenced assemblies to be included in Xamarin App package

I have a Xamarin Forms app that requires me to dynamically load a single one of several possible assemblies at runtime (based on versioning). This should not be a problem as iOS can load assemblies using the mono interpreter. The problem I face is that the IPA and APK packages do not include the assemblies because they are not referenced directly in the app. I include them in the project references but they do not appear in the app package.
Is there a way to tell Xamarin to always include an assembly in the package regardless if it is being directly referenced in code?
==UPDATE==
I have a complete working POC with what I am trying to accomplish in terms of versioning in the application.
https://github.com/rlasker-b2w/XamVersioning
I specifically have to add references in the VersionSelector class otherwise the version projects do not get their assemblies added to the app package.
https://github.com/rlasker-b2w/XamVersioning/blob/main/VersionApp/VersionSelector.cs

Mwarray.dll for Unity - How can I use it?

I have a problem with the MWArray.dll which is the original dll library from mathworks compiled from Matlab. This dll works under visual studio but not under unity. The goal was to compile under matlab a simple function mycos which calculates a cosinus and uses it under unity with the second dll MWArray mathworks library.
I made a test under Visual Studio and it works well. I have no problem to read the dll and make a call to the mycos.dll class. I made a typical c# project with two references on MWArray and mycos.
But when I try under unity, by putting the two dll under a plugins folder and configure unity 2018 on .NET 4.0 because the dll are compiled with the .NET framework 4.0.
Unity can read the dll but when running I get an error:
NotImplementedException: The requested feature is not implemented.
System.Security.Principal.WindowsIdentity.GetCurrent (Boolean ifImpersonating)
(at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Security.Principal/WindowsIdentity.cs:166)
If I want to use matlab for a research project which was coded in matlab with advanced high level math functions which going to be difficult to recode under unity with c#. So the easy way is to use compiled package from matlab compiler. So is there someone who did this already?
The solution consists in NOT using MWArray.dll in Unity project because of incompatibility with Mono. Create a Visual Studio project (when creating new select the type "console applicatiion"), it MUST be separated from unity project directory, and use it as an application server; in fact in that app you can use MWArray.dll, remember that you have to configure your project configuration to x64!!! In the Unity project create a client app to communicate with the other server project.

How to develop a xamarin forms component control to be used as dll in a project

I'm developing a xamarin forms class library (Custom Component) that targets (Windows, Androi and IOS) platforms "this library should be added to an existing Xamarin forms Portable project as DLL reference" and want to add a UI control and use it's native functions from inside the library. Custom renderers can't be added in PCLs so Can anyone help on how to access the control's native functionality?
You can start from scratch on your by following this piece of documentation by Xamarin on how to build NuGet packages. Then just make sure you build them without too much references and test them in your sample projects.
But to make your life a lot easier have a look at this Visual Studio extension by James Montemagno. It installs some project templates for you so naming is consistent and you have the basics already setup to start building your reusable code.

Integrating C/C++ code in Xamarin for iOS

Under Visual Studio 2015, I have integrated Android C++ code into Xamarin and it works as expected. Now, I am trying to do the same for iOS and am running into some basic issues.
I am assuming the integration mechanism is the same for iOS as that of Android in the sense that a shared library (probably .so) is created that one can load using DllImport in C# code.
When I try to add a new C++ project for iOS to my solution, the only option that seems to make sense is Visual C++-->Cross Platform-->iOS-->Shared Library. I added this project type as MyTestShared. This actually ends up creating three projects - MyTestShared.Shared, MyTestShared.iOS, and MyTestShared.Android. Project MyTestShared.iOS already has an external method defined as char* iOSInfo(). However, when I try to add project MyTestShared.iOS as a reference to my MyMainApp.iOS project, I see an error "A reference to MyTestShared.iOS could not be added. An assembly must have a dll or exe extension."
Questions:
For Android C++, it generates exactly one project. Why does it create three projects for iOS C++?
How do I add a reference to MyTestShared.iOS? What is it that I am missing?
I see there is an option to create a static C++ library for iOS. Can I reference it somehow in my C# code?
Regards.

Using MinGW/GCC built DLL in a Visual Studio 2010 C++/CLI project

I have a communication library built on top of Qt and Google Protocol Buffers. It's currently being built with MinGW/GCC on Windows. My goal is to use the same library in C# on .NET, with the help of a thin wrapper on top using C++/CLI (bridging the unmanaged code with managed code).
I tried using the MinGW produced DLL directly in my C++/CLI project, but I keep getting linker errors (cant remember the error codes right now, but something about missing tokens and functions/signatures).
First question is: Should I be able to use the MinGW-produced DLL with the Visual Studio compiler/linker? Or do I need to compile the library again, using only VS compiler for all projects?
If I should be able to use the MinGW-produced DLL directly, how do I reference it in Visual Studio 2010? In project settings it seems to look for *.lib files, but I can't find any .lib files in the output of MinGW/GCC. It does produce *.a files, but it seems like Visual Studio don't handle this kind of file..
I should also mention that both Qt and protobuf are also compiled with MinGW. But I can of course recompile all the parts in VS 2010 if necessary.. Would have been nice to save the recompile time though, since our buildserver already has a working setup using MinGW.
The easiest way to use it would be by recompiling it with Visual Studio. This is when I am assuming C++ types and classes used in the interface you intend to use.
In case you have a C interface to this library you could dynamically load the library via LoadLibrary and use GetProcAddress to access those functions.
However it depends completly on the way how you intend to use the library.

Resources