Xamarin: Libraries support - xamarin

I want to use a C++ library (static/dynamic) on my Xamarin.Android and Xamarin.iOS project.
Should the C++ library be static or dynamic in order to be used on both Xamarin.Android and Xamarin.iOS projects, as i'm thinking of using C# DLLimport/SWIG tool to use the C++ library as C#.
(I heard that iOS forbids the usage of dynamic libraries in the apps.) What can be the best approach for me?
Thanks!

You are correct, iOS does not allow you to use dynamic libraries, so at least for iOS you must build the C++ library as a static library.
I believe both a static and a dynamic library work fine on Android, so there it doesn't matter which you choose.

As soon as iOS8 has been released in September, you will be able to add support for iOS Embedded frameworks to all devices with a minimum deployment target of iOS8. In fact you can now add dynamic libraries when you target is not less than iOS8.
Anyways there are some pitfalls, depicted here.

Related

Is it possible to create native Android and iOS library with Xamarin? [duplicate]

Currently, we are developing a .NET Standard class library that will be used by different Xamarin.* platforms.
I was wondering if there is a way to use the shared code in a native Android project (maybe produce an AAR/JAR file).
The reason I need this functionality is because our company provides an AAR to one of our customers (that is used by a native Android project), and we would like to keep a single source base instead of two.
Thanks in advance!
Update:
The name changed to ".NET Embedding" and it is at v0.4 at the time of this update.
https://developer.xamarin.com/releases/dotnetembedding/dotnetembedding_0/dotnetembedding_0.4/
New instructions:
Installing .NET Embedding
Original post:
You can use Mono's Embeddinator-4000
It supports various language consumers, so it surfaces .NET code as idiomatic code in the target language. This is the list of supported languages at present:
Objective-C: mapping .NET to idiomatic Objective-C APIs.
Java: mapping .NET to idiomatic Java APIs.
C: mapping .NET to an object-oriented like C APIs.
Embeddinator-4000 is a tool that allows your existing .NET Code (C#, F# and others) to be consumed from other programming languages and in various different environments.
Java is currently in a preview state for macOS, Windows, and Android.
The platform is selected by passing the --platform= command-line argument to the embeddinator. Currently macOS, Windows, and Android are supported.
re: https://mono.github.io/Embeddinator-4000/
As a part of the requires For Android you will need Xamarin.Android 7.4.99+ and Android Studio 3+ (Java 1.8) but if you are building Android apps with Xamarin and Java you would have those already.
Note: This tool is in Preview, so review the open issues and post new if needed.
re: https://github.com/mono/Embeddinator-4000/issues
Note: This is not trans-compiling, but embedding Mono's runtime in a native package that allow CIL code, AOT'd (i.e. iOS) or not to run within a different language/runtime.

What is your C# code doing within a Xamrin build?

I've read through a lot of documentation on Xamarin and my understanding is that it exposes the native APIs on each platform via C# and .NET, and this way, offers a sort of one-to-one binding. The native build produced contains some of the applicable .NET libraries (based on linker optimisation).
However, I can't seem to find any information about what actually happens within the native build itself.
Assuming my understanding is correct, my question is:
Within the native build, does the code you've written call the native API functions via a call to native .NET functions included in the build?
The short answer is: yes, it calls native API's through .NET code that runs on Mono, via the binding concept you already seem to be familiar with.
Xamarin uses these bindings to call the native code from your C# code. There are a few differences depending on the platform you are running on.
iOS
Your IPA-file will contain 2 things:
The executable of your .NET code (AOT-compiled), which is run through the Mono runtime
Native libraries, which use the objective-C runtime. The .NET code calls these libraries/API's via bindings.
Both the Mono runtime and the Objective-C runtime are running on top of a UNIX-like kernel.
Take a look at this Xamarin iOS architecture documentation for a more extensive explanation.
Android
Android is a little more complex to understand and explain. This is because some of the API's are only exposed through the Android Runtime API.
The bindings in Android are working through concepts such as:
"Android Callable Wrappers" (Android Runtime to Mono)
"Managed Callable Wrappers" (Bindings to Android Runtime API's)
The Xamarin Android architecture documentation explains these concepts and inner workings in great detail.

Xamarin.Forms PCL

i was just wondering what the benefits are in using PCL with Xamarin.Forms.
I mean you share the Code between each Platform anyway.
In Xamarins native approach it is quite clear.
So maybe anybody can tell me why to use PCL in Xamarin.Forms?
PCL allows the maximum in code sharing across platforms. PCL will by it's very nature limit you to using only those API's that can be shared from platform to platform.
While you can use Shared Projects, you will find yourself using compiler directives and making coding sacrifices that can lead to bad coding practices across anything but the smallest of projects.
Using the Xamarin Forms 2.3.3 or later you can of course embed Platform Specific Controls in your Xamarin Forms pages in your PCL and they will only render when run on the platform.
In addition of maximum code sharing, it allows you to follow best practices like SOLID.
Also, please remember that there are 3 choices (not only 2):
Portable Class Library
Shared Library
.NET Standard Library
.NET Standard Library is still an ongoing work in process, so it's not there yet. But, it will be the way to go!. So, PCLs today can give you an excelent migration path to the .NET Standard Library way tomorrow.
Hope this helps.

Do the Android and iOS versions of OpenTK have the same API?

I'm trying to get into mobile game development with Xamarin; I am using OpenTK for rendering.
My plan is the following:
1. Make a shared library that is actually the game (and has all openTK code).
2. Make a a runnable project for iOS and Android that handles platform specific stuff (e.g. popping up alerts) and uses my shared library.
Is this possible with openTK? Does it have exactly the same API on iOS and on Android, just the linked library is different? If yes, how to avoid code duplication?
Your approach sounds very reasonable and will work with a little bit of effort.
The reason is that Xamarin.Android and Xamarin.iOS were forked from an alpha version of the library and have evolved slightly different APIs. Much of the OpenGL ES binding is identical, but some functions have annoying (and unnecessary) differences that you will need to #ifdef in your shared library.
The good news is that the Xamarin fork of OpenTK has now been open-sourced, so we are actively working towards a solution. I already have a private build of library that exposes an identical OpenGL ES API between Android and desktop platforms[1] and I'm working on fixing the differences between the iOS and Android versions.
You can follow the development of OpenTK in the official github repository.
[1] You can execute OpenGL ES code on the desktop using ANGLE or the ARB_ES2_compatibility and ARB_ES3_compatibility extensions. This makes OpenGL ES the first graphics API to work across all major platforms (Windows, Linux, Mac, Android, iOS - and hopefully WinPhone in the future.)

Is it possible to write shared libraries between iPhone, Android, Blackberry, Mac and Windows?

I'm tasked with "porting" a few apps from a Windows environment to various mobile platforms and Mac as well.
I plan on writing MVC patterned apps in which I write as many controllers as I can in some sort of universal library, probably in C or C++. Then writing the views in various choice languages (Objective C, Java, .NET, whatever) for the target OSes.
I've never attempted anything like this before, so my questions are: Is it possible to write and compile one library that can be used on iPhone, Android, Blackberry, Windows and Mac? Is it even wise to try this?
I understand that certain native methods simply won't be available on each platform.
You can create a library whose source code is portable, assuming that you properly abstract away any platform-dependent calls. You can't, however, create such a library, compile it, then use it anywhere; you'll have to compile it for each platform.
Your should take a look at the Mono Project. http://www.mono-project.com
...more specifically at :
MonoTouch: To develop iPhone applications in .NET
MonoDroid (BETA): To develop Android applications in .NET
Mono plugin for MeeGo: To develop MeeGo applications in .NET
And you know that you can develop on Windows Mobile in .NET already.
I've haven't found anything related to BlackBerry yet.
There are services like Rhomobile and Appcelerator which will allow you to do this sort of cross compiling.
I've never used either however.

Resources