Binding a Java Library

Consuming Java libraries from C#

PDF for offline use:
Related Articles:

Let us know how you feel about this.


0/250
Thanks for the feedback!

last updated: 2016-06

The Android community has many Java libraries that you may want to use in your app; this guide explains how to incorporate Java libraries into your Xamarin.Android application by creating a Bindings Library.

Overview

The third-party library ecosystem for Android is massive. Because of this, it frequently makes sense to use an existing Android library than to create a new one. Xamarin.Android offers two ways to use these libraries:

  • Create a Bindings Library that automatically wraps the library with C# wrappers so you can invoke Java code via C# calls.

  • Use the Java Native Interface (JNI) to invoke calls in Java library code directly. JNI is a programming framework that enables Java code to call and be called by native applications or libraries.

This guide explains the first option: how to create a Bindings Library that wraps one or more existing Java libraries into an assembly that you can link to in your application. For more information about using JNI, see Working with JNI.

Xamarin.Android implements bindings by using Managed Callable Wrappers (MCW). MCW is a JNI bridge that is used when managed code needs to invoke Java code. Managed callable wrappers also provide support for subclassing Java types and for overriding virtual methods on Java types. Likewise, whenever Android runtime (ART) code wishes to invoke managed code, it does so via another JNI bridge known as Android Callable Wrappers (ACW). This architecture is illustrated in the following diagram:

A Bindings Library is an assembly containing Managed Callable Wrappers for Java types. For example, here is a Java type, MyClass, that we want to wrap in a Bindings Library:

package com.xamarin.mycode;

public class MyClass
{
    public String myMethod (int i) { ... }
}

After we generate a Bindings Library for the .JAR that contains MyClass, we can instantiate it and call methods on it from C#:

var instance = new MyClass ();

string result = instance.MyMethod (42);

To create this Bindings Library, you use the Xamarin.Android Java Bindings Library template. The resulting binding project creates a .NET assembly with the MCW classes, .JAR file(s), and resources for Android Library projects embedded in it. You can also create Bindings Libraries for Android Archive (.AAR) files and Eclipse Android Library projects. By referencing the resulting Bindings Library DLL assembly, you can reuse an existing Java library in your Xamarin.Android project.

When you reference types in your Binding Library, you must use the namespace of your binding library. Typically, you add a using directive at the top of your C# source files that is the .NET namespace version of the Java package name. For example, if the Java package name for your bound .JAR is the following:

com.company.package

Then you would put the following using statement at the top of your C# source files in order to access types in the bound .JAR file:

using Com.Company.Package;

When binding an existing Android library, it is necessary to keep the following points in mind:

  • Are there any external dependencies for the library? – Any Java dependencies required by the Android library must be included in the Xamarin.Android project as a ReferenceJar or as an EmbeddedReferenceJar.
  • What version of the Android API is does the Android library target? – It is not possible to "downgrade" the Android API level; ensure that the Xamarin.Android binding project is targeting the same API level (or higher) as the Android library.
  • What version of the JDK was used to compile the library? – Binding errors may occur if the Android library was built with a different version of JDK than in use by Xamarin.Android. If possible, recompile the Android library using the same version of the JDK that is used by your installation of Xamarin.Android.

Build Actions

When you create a Bindings Library, you set build actions on the .JAR or .AAR files that you incorporate into your Bindings Library project – each build action determines how the .JAR or .AAR file will be embedded into (or referenced by) your Bindings Library. The following table summarizes these build actions:

Build Action Description Recommended Use
EmbeddedJar Embeds the .JAR into the resulting Bindings Library DLL as an embedded resource. This is the simplest and most commonly-used build action. Use this option when you want the .JAR automatically compiled into byte code and packaged into the Bindings Library.
InputJar Does not embed the .JAR into the resulting Bindings Library .DLL. Your Bindings Library .DLL will have a dependency on this .JAR at runtime. Use this option when you do not want to include the .JAR in your Bindings Library (for example, for licensing reasons). If you use this option, you must ensure that the input .JAR is available on the device that runs your app.
LibraryProjectZip Embeds an .AAR file into the resulting Bindings Library .DLL. This is similar to EmbeddedJar, except that you can access resources (as well as code) in the bound .AAR file. Use this option when you want to embed an .AAR into your Bindings Library.
ReferenceJar Specifies a reference .JAR: a reference .JAR is a .JAR that one of your bound .JAR or .AAR files depends on. This reference .JAR is used only to satisfy compile-time dependencies. When you use this build action, C# bindings are not created for the reference .JAR and it is not embedded in the resulting Bindings Library .DLL. Use this option when you will make a Bindings Library for the reference .JAR but have not done so yet. This build action is useful for packaging multiple .JARs (and/or .AARs) into multiple interdependent Bindings Libraries.
EmbeddedReferenceJar Embeds a reference .JAR into the resulting Bindings Library .DLL. Use this build action when you want to create C# bindings for both the input .JAR (or .AAR) and all of its reference .JAR(s) in your Bindings Library.

These build actions are explained in more detail in the following guides.

Adapting Java APIs to C

The Xamarin.Android Binding Generator will change some Java idioms and patterns in order to correspond to .NET patterns. The following table describes how Java is mapped to C#:

Java.NET
Setter/Getter methods Properties
Fields Properties
Listeners/Listener Interfaces Events. The parameters of methods in the callback interfaces will be represented by a EventArgs subclass.
Static nested class Nested class
Inner class Nest class with an instance constructor

Binding Scenarios

The following binding scenario guides can help you bind a Java library (or libraries) for incorporation into your app:

  • Binding a .JAR is a walkthrough for creating Bindings Libraries for .JAR files.

  • Binding an .AAR is a walkthrough for creating Bindings Libraries for .AAR files. Read this walkthrough to learn how to bind Android Studio libraries.

  • Binding an Eclipse Library Project is a walkthrough for creating binding libraries from Android Library Projects. Read this walkthrough to learn how to bind Eclipse Android Library Projects.

  • Customizing Bindings explains how to make manual modifications to the binding in order to resolve build errors and shape the resulting API so that it is more "C#-like".

  • Troubleshooting Bindings lists common binding error scenarios, explains possible causes, and offers suggestions for resolving these errors.