Forum Xamarin Cross Platform with Xamarin

Using Xamarin only to build a portable library


Our task is to develop an app for iOS and Android. The GUI has to be native on both platforms, but there is common background code of course e.g. to access a SQLite DB and return results as list of objects.
By "native" i also mean writing the iOS app in Swift/ObjectiveC and the Android app in Java. Cause we want to use the native programming style and also want the whole possibilities of each platform.

So we are searching for a solution to write only the shared code with Xamarin and than drop that portable native library into XCode and Andorid Studio, call its methods and use the output in native app code.

Is that possible?



  • adamkempadamkemp USInsider, Developer Group Leader mod

    You can't use C# code in an app written in Objective-C or Java. You would at least need the top level application project to be Xamarin application. Technically you could make C# code callable from Objective-C or Java code, but it would be a lot of work, and very difficult to maintain.

    Speaking of difficult to maintain, writing the app twice in two different languages is a pain. I'm curious why you think you need to do that in order to "use the native programming style and also want the whole possibilities of each platform". Xamarin applications are native applications. They use the native SDKs. There is nothing you can't do in Xamarin that you can do in an Objective-C or Java app. Do you have any specific concerns about something you want to do?

  • ChristophMllerChristophMller USMember

    I know that is does not "simply" work. I know what steps are involved to get a C# or .NET or Java library or app running on iOS. There is a AOT step to produce a native executable so there is no need for Mono anymore. Mono provides some possibilities for that but this is not enough. So i thought Xamarin already does everything needed to have that working.

    Xamarin seams to do what i want, but with a "whole application" approach written in C#. It forces me to develop the Xamarin way, and that is not what i want. There is knowledge in the company in how to write native iOS applications, and native Android applications and with Xamarin we would write the UI separately for iOS and Android anyway. So why not use our knowledge and write the app GUI in Swift and Java? I am not happy with Xamarin recreating the iOS programming style in C# with different method names, that do sometimes the same and sometimes not.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    There is a AOT step to produce a native executable so there is no need for Mono anymore.

    That's not true. The runtime is more than just a JITer. The runtime includes all of libraries to support managed code. That includes the garbage collector, reflection, etc. Any .Net code requires a .Net runtime whether it's AOT compiled or JIT compiled. All Xamarin.iOS applications, even though they are AOT compiled, include a mono runtime. The compiled executable initializes that runtime and then asks it to start the managed code. From then on the mono runtime is in control.

    In order to call C# code from a non-Xamarin application you would have to include a mono runtime in your application as well, and you would have to initialize it and then invoke managed code through it. This is not a use case supported by Xamarin, and I'm not even sure you could get access to all the bits you would need to make it work.

    As a former Objective-C iOS developer I can say that transitioning to Xamarin.iOS is actually not difficult. Obviously you have to learn a new language, but programmers should be used to that. You would probably agree that C# is a more productive language than Objective-C, and even (IMO) Java. That means even though it takes some effort to learn it and adapt you and your teammates will very likely end up being more productive.

    As for the existing knowledge, the important pieces of knowledge are about APIs. All of those APIs are accessible in Xamarin.iOS and Xamarin.Android, and the majority of them look identical except for syntax and capitalization. There are a few C#isms for sure, but again those can be learned pretty quickly. I don't think that's a reason to avoid the platform or to do a whole lot of work to keep the status quo.

    If it helps I did two relevant presentations that you can watch online: Introduction to Xamarin.iOS and Xamarin.Android and Xamarin.iOS Deep Dive. The last presentation is especially useful to someone with an Objective-C background because it explains how Xamarin.iOS builds on top of Objective-C, how the common Objective-C patterns are mapped into C#, and why that was done. I think if your teammates watched that video they would have no problem adapting.

    I don't work for Xamarin so I have no stake in your decision, but I can say that our company went from separate codebases using Objective-C and Java to one codebase with Xamarin.iOS and Xamarin.Android, and we have seen a huge improvement in productivity and code reuse. When you reduce the barriers to code reuse (like having multiple languages) you'll find that a lot more code is reusable than you might have expected.

Sign In or Register to comment.