Porting Android Libraries to Xamarin.Android28 Oct 2016
When it comes to Android, there is a vast open source community with thousands of libraries to use inside our Android application.
However not all of these awesome libraries have an equivalent library in the
Xamarin community. This of course makes it awkward for us as a developer for multiple reasons:
- Not enough time to write port the respective Java -> C# code
- Not familiar enough with Java/C# to know the language equivalents
- Just want to use the library and not care about the internals
Because of this, this gives us only a few options to use one of these libraries:
- Port over the Java code to C#
- Create a Xamarin.Android Binding Project
- Wait for somebody else to do it (Xamarin Community)
However there are pros/cons of each of these items:
Porting over the Java code to C#:
This is personally my favorite way to create a Xamarin.Android library. You will get to learn how the library was made simply by porting it over.
- Everything will be in C#/Xamarin.Android and easier to maintain in the future
- Easier to debug
- It is fairly easy to port Java to C# (Guide)
- It takes awhile to write in terms of keyboard strokes
- Certain language features can be hard to port over (Reflection, Anonymous Inner Classes, Enums, etc)
Create a Xamarin.Android Binding Project:
This scenario basically allows the Xamarin.Android Binding Generator do all the work for you. In most simple cases it works quite well, but in more complicated cases, you might end up pulling out your hair.
- Easy to generate a quality binding without writing much code
- Ability to transform the binding how you see fit to match C# conventions
- Does not take much time
- Hard to debug generated code
- Have to learn how to troubleshoot bindings / bindings guide
Wait for somebody else to do it (Xamarin Community)
Waiting on other people is typically a last resort when it comes to deadlines.
- All the work has been done for you and tested
- Plug and Play
- Officially maintained by somebody
- Have to wait and rely on other people
- Libraries can be abandoned by maintainers
Overall with these three scenarios laid out, it’s really up to you to make the decision on what might be best for your project’s needs.
Here’s a quick guide to help you find if a library already exists:
- Search NuGet and Xamarin Components for the library you’re after
- Search Google for the
Xamarin(Note: Some libraries might be hosted on other providers like CodePlex, GitLab, etc)
- Search Github for snippets of the
Javalibrary to see if there’s any