Xamarin with native Android library

Today I want to show you how to consume custom Java library in Xamarin.Android application.

In Android Studio I created a new project. I chose location, application name and company domain. I also selected phone platform and added no activity.

Then I created new Android Library module.

My custom library can perform basic arithmetic operations on two numbers. Here is its interface.

package com.example.javacalculator;

public interface ICalculator {
    int Add(int a, int b);
    int Subtract(int a, int b);
    int Multiply(int a, int b);
    double Divide(double a, double b);
}

and implementation:

package com.example.javacalculator;

public class Calculator implements ICalculator {
    @Override
    public int Add(int a, int b) {
        return a + b;
    }

    @Override
    public int Subtract(int a, int b) {
        return a - b;
    }

    @Override
    public int Multiply(int a, int b) {
        return  a * b;
    }

    @Override
    public double Divide(double a, double b) {
        if(b == 0){
            throw  new IllegalArgumentException("Divide by 0 is illegal dude.");
        }

        return a / b;
    }
}

Then I built my new module so I got .jar file for further use in Xamarin application.

Except Xamarin.Android application I added a Binding Library to solution.

A Bindings Library (BL) is an assembly containing Managed Callable Wrappers for Java types so it is possible to call Java code via C# calls.

In BL project I added my .jar file into Jars directory and I added reference to Xamarin.Android application.

I also changed build action type for new .jar file for EmbeddedJar. With this option .jar file is automatically compiled and packaged into the BL.

Important thing is BL must targeting the same or higher Android API level as the Android Library.

Now it is possible to use Java library in C# code. Notice that I used ICalculator implementation in the following code of MainActivity in my application.

namespace AndroidNativeCalculator
{
    [Activity(Label = "AndroidNativeCalculator", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        ICalculator calc;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView (Resource.Layout.Main);
            calc = new Calculator();

            var firstNP = FindViewById<NumberPicker>(Resource.Id.firstNumber);
            var secondNP = FindViewById<NumberPicker>(Resource.Id.secondNumber);

            firstNP.MinValue = secondNP.MinValue = 0;
            firstNP.MaxValue = secondNP.MaxValue = 100;

            var result = FindViewById<TextView>(Resource.Id.result);

            FindViewById<Button>(Resource.Id.add).Click += (sender, args) => result.Text = calc.Add(firstNP.Value, secondNP.Value).ToString();
            FindViewById<Button>(Resource.Id.subtract).Click += (sender, args) => result.Text = calc.Subtract(firstNP.Value, secondNP.Value).ToString();
            FindViewById<Button>(Resource.Id.multiply).Click += (sender, args) => result.Text = calc.Multiply(firstNP.Value, secondNP.Value).ToString();
            FindViewById<Button>(Resource.Id.divide).Click += (sender, args) => result.Text = calc.Divide(firstNP.Value, secondNP.Value).ToString();
        }
    }
}

As you can see everything works as I expected:

Leave a Reply

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