C#DevelopmentXamarin

Using Android Java libraries in Xamarin Forms – a practical example.

xamarin cs java android

Android ecosystem has huge numbers of libraries which can be used in Android-based applications. Technically, these libraries are files with .jar or .aar extensions. When developing Android application with Java these libraries can be directly included/linked in the application and used. Of course, re-usage of these java-based libraries in Xamarin Forms is possible, but the process is not so straightforward.

In this blog post I will show how to import Android Java library into Xamarin Forms and use its functionality in C# application.

To make all more real-word, I will try to import one random jar library from public repository https://mvnrepository.com and try to use the logic from it.

Let’s get our hands dirty

As I sad before, for the purpose of this demo I randomly picked one library from mentioned repository. I picked HTML parser library located here https://mvnrepository.com/artifact/org.jsoup/jsoup. You can find more information and documentation about the library here https://jsoup.org.

First, In Visual Studio 2019 I create new Xamarin.Forms project with Blank Template. I select only Android platform. In one of the next blog posts I will show similar procedure for iOS – how to reuse Objective-C iOS code (and .a library) in Xamarin Forms and C#. But for now, let’s focus on Android.

new blank xamarin forms project

As usual, Visual Studio scaffolds two projects, one is Common Project (cross-platform shared code) and the other is Android specific project. Next, I add new “Android Binding Library (Xamarin)” project to handle bindings between imported Java library and C#/Xamarin runtime. This binding library will create proxies (or Managed Callable Wrappers or just wrappers for that mater) which will handle C# calls and delegate them to external Java library.

adding new Android Bindings Library project

My solution structure now consists of three projects:

New Xamairn Forms Bindings project on place.

I have everything ready and prepared so I can start putting external “logic” in my app.

Importing external Android Java library

I download latest version of jsoup library from https://mvnrepository.com/artifact/org.jsoup/jsoup/1.12.1. I also check maven definition for any dependencies. All library dependencies must be also embedded in the binding assembly . In my case, jsoup library has no dependencies, as you can see from maven definition below:

Mvnrepositry with Android Java library.

Procedure is simple, download jar file, put it into Jars folders in bindings project, mark file as “EmbeddedJar” and that’s it.

How to use EmbeddedJar in my project

Now, things start to get interesting. At project compile, EmbeddedJar build action/process starts to create endpoint/stubs between C# and Java. This are so called Managed Callable Wrappers.

If you know that your Java library is available on the target device, you can also use InputJar option for Build Action.

Almost always, when I do first compile I get a lot of compile errors, just like pictured below:

Errors after first compile

Hold on, no panic!

Due to differences between Java and C#, no all Java-to-C# relations can be translated 1:1. Normally, I observed each and every error and I just remove problematic code. For this purpose Transformation/Metadata.xml file with developer-specific directives how to handle code generation (mapping Java code to C# wrappers) is used. You can check awesome documentation about this here: Metadata.xml Transform file. In my case, I simply removed problematic code, so my binding transformation file looks like this:

I hit Rebuild and now my compiler report only some warnings. For now, I am good with this, but in production ready app, these warnings should be checked and fixed if possible.

Build fixed after applying Metadata.xml transformation.

If you are more interested in generated code (generated Managed Callable Wrappers a.k.a proxies between Java and C#) you can checked output in obj folder (code intermediate folder) as shown below:

Code generated for Xamarin Bindings library

Finally, I have build my binding project. Now I need to reference it to my Xamarin Android project and use the C# wrappers to call logic in Java library.

Integration into my mobile application

Binding library is ready to be used by my mobile app. I will use jsoup HTML parsing library wrapper to parse title (<title></title>) element from web pages.

Android Permissions

On start and not to forget, my Android application will use internet, so I need to put correct permissions in my Android Manifest:

Integration steps

As said, when binding library is referenced in Android project It can be used only on platform specific project. If I want to use it in shared code project (where all logic normally is) I do this by service Dependency Injection mechanism. I will just quickly summarize steps how this can be done.

I define interface in my shared project called “IGetWebSiteTitleService

Implement interface in Android specific project

Injected into shared project via App constructor and make it application visible

Here, for simplicity, I used App static field to enable my service availability in my application, but It can be also used by any dependency injection container and used across application.

Now, that my service is available in my application I can use it in my code-behind, e.i:

Gui

Last, but not the least important thing is GUI or user-interaction layer. My mobile app needs to communicate with service layer and present result to the end user. Just for illustration, my app use simple code-behind approach, but logic can be used also with MVVM pattern (by consuming IGetWebSiteTitleService service in some ViewModel and bind result to View).

My XAML is simple…

… with only one event handler for button click which calls underlying service to process the request.

Result

End user enters URL, hit “Get Title” button and gets web page title parsed! All this logic is executed by external Java library, in my case by jsoup.

My Xamarin Android application in action

Conclusion

Binding Android Java libraries into Xamarin Forms applications is very powerful mechanism because It enables Xamarin developers to use numerous Android Java libraries available.

In this blog post, I described practical example (with all problems and drawbacks) of importing Android Java library into Xamarin Forms application.

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.