Press "Enter" to skip to content

Creating a Xamarin Binding Library for iOS And Android (Part. 1)

As a Xamarin Developer, there are situations where we want to use a native (Objective C, Swift, Java, Kotlin, etc) library. To do this, we have two options: translate the library code to C# which is good if the code is small but in cases where we want to migrate libraries with a lot of code the best approach is to create a binding library.

The following are the steps needed to do a Xamarin Binding:

  • Create a Binding library project for each platform you want to support
  • Create the binding mappings
  • Add the binding mappings and native library into the created Binding library project
  • Use it by importing the Binding library project in your project

This article will be divided into two parts:

  • Creating Binding Libraries in iOS and Android
  • Consuming the binding library in Xamarin Forms.

Let’s start

I will be migrating the Pay.Cards library which is a free credit card recognizer by using the built-in camera in Android/iOS devices.

The code for this open-source library can be found in these repositories: Swift and Java.

Creating an iOS Binding Library

1.Create a Binding Library iOS project

2. Download the Native SDK

In this specific case it was downloaded directly from the repository.

3. After downloading it, find the .framework folder

4. Check your iOS SDKs available versions

xcodebuild -showsdks

5. Using Terminal run the following command specifying your current iOS SDK in the file path that contains the framework folder:

sharpie bind \      
-sdk iphoneos13.6 \
./PayCardsRecognizer.framework/Headers/PayCardsRecognizer.h \
-scope PayCardsRecognizer.framework/Headers \
-c -F .

Note: If you get the following error in the terminal “sharpie: command not found”. Is because you don’t have Objective Sharpie install it, so make sure to install it (You can download it from here).

6. After running it you should get the following result if no errors occurred:

As a result, two files will be generated.

7. Go to the binding library project, click on Native Reference/Add Native Reference to the Framework folder

8. Also, add the generated code in the ApiDefinition.cs and Structs.cs files of your binding. project.

9. Click on Build

We got 3 errors. Let’s fix them!

10. Fix errors

To fix these errors I recommend you check this blog post about typical errors that can occur and how to fix them.

This specific issue was related to a delegate called PayCardsRecognizerPlatformDelegate. This is an objective C protocol so I added the following interface in ApiDefinitions.cs to map the methods using it to this interface instead.

	interface IPayCardsRecognizerPlatformDelegate
	{
	}

Basically I mapped this protocol:

id<PayCardsRecognizerPlatformDelegate> 

To:

IPayCardsRecognizerPlatformDelegate instead of PayCardsRecognizerPlatformDelegate.

Before

After

Now build again and we are done!!

Creating an Android binding library

1.Create an Android Binding library project

2. Download the native library

It was downloaded from here.

3. Add the .jar/.aar to the project

4. Click on Build

Got the following issue.

There is an awesome gist recompilation of common Android Binding issues that you can find it here.

For my specific case, I found this exact error in the following gist: https://gist.github.com/brendanzagaeski/69f490e31ca6a71136ff

So I just adapted the solution here with the specific classes and fields that were having the issue by adding the following to Metadata.xml.

<attr path="/api/package[@name='cards.pay.paycardsrecognizer.sdk.utils']/class[@name='Size']/method[@name='compareTo' and count(parameter)=1 and parameter[1][@type='cards.pay.paycardsrecognizer.sdk.utils.Size']]/parameter[1]" name="managedType">Java.Lang.Object</attr>

Now build once again and done!!

Make sure to check out the second part about consuming this binding library in a Xamarin Forms project (Coming soon!).

Check the full source code here.

Happy Binding!