Creating android binding library in xamarin with multiple binaries
In this blog, we will explore how to create android binding library in xamarin with multiple aar/ jar
Xamarin.Forms is an open source mobile UI framework from Microsoft for building iOS, Android, & Windows apps with .NET from a single shared codebase.
We as mobile developer tends to use multiple third party libraries in our application to reduce boilerplate code, to use a particular library functionality etc. Its not always the case that all the libraries are available on Nuget Gallery.
Thus, come the requirement of creating binding library which we can use in xamarin project. If you are new to xamarin please refer to this link to understand the basics of Binding an .AAR
What we will cover in this blog-
- Understanding POM file
- Downloading all the interdependent artifacts of android dependency
- Merging all aar/ jar in one single library
- Integrating .so files
Let’s begin,
Download a gradle script from here. It will download all transitive aar/jars files in accordance with the POM files.
To run this script you will need gradle v4.10.1, you can find it here.
Make appropriate changes as per your dependency, in this example we are creating biometric binding library manually hence the script has following dependency.
dependencies {
implementation 'androidx.biometric:biometric:1.1.0' // Replace it with your depenency
}dependencies {
pom (group: 'androidx.biometric', name: 'biometric', version: '1.1.0', ext: 'pom')// Replace group, name and version values
}
Incase if your dependency is hosted on private maven, make changes in following sections of the script
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
// Add your private maven credentials here
}Once you download the gradle on you machine, extract it and navigate to gradle-4.10.1\bin. Copy the earlier downloaded build.gradle file in bin folder.
For windows, use “.\gradle .\build.gradle” command in PowerShell to download all the artefacts.
Get Abhay Naik’s stories in your inbox
Join Medium for free to get updates from this writer.
After successful download your local folder structure should look like this
Navigate to dependency folder, it should have libs and pom folder each containing its respective entities. Lib folder should look like this
The dependency folder will also have a POM file under pom folder. To understand in detail please refer this. In a nutshell, this file has all the information/ artefacts or consider as helping hands which are required to run your main library. It’s internal dependency will be mentioned as below
<dependency>
<groupId>androidx.annotation</groupId>
<artifactId>annotation</artifactId>
<version>1.1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>androidx.core</groupId>
<artifactId>core</artifactId>
<version>1.3.2</version>
<scope>compile</scope>
<type>aar</type>
</dependency>Now that we have all the ingredients ready, let’s start creating a binding library.
Assuming you have a basic knowledge about creating binding library, create a new android binding project “SampleAndroidBiometricLibrary”.
Add biometric-1.1.0.aar in jar folder of the library project and change Build Action to LibraryProjectZip in properties section.
Once this is doe, copy all jar files from your gradle-4.10.1\bin\dependency\libs folder and set the BuildAction to EmbeddedReferenceJar
Now is the interesting part, since the binding library can not have more than one aar we can not add all the other interdependent aar in the project and build the solution. We have to extract all the aar files and add them as jar.
To do so, refer to this link and extract all aar files. Extract all the files and add all the jar files one by one in the project’s jar folder and set the BuildAction to EmbeddedReferenceJar
Now in case any of the libraries are dependent on native .so files, create a lib folder in library project and all the supported ABI sub-folders in it. The .so files of any aar file can be found in extracted aar folder’s in jni folder under respective ABI sub-folders. Add all these .so files to respective ABI folders of the library project and set BuildAction as EmbeddedNativeLibrary
Once all this is done, clean your solution and rebuild your project again. The out folder should have .dll file ready to consume in your xamarin android solution.
In case there are any errors while building the binding library solution you might have to add rules in Metadata.xml of binding library project. To understand more about it refer this or this
Hope this helps, incase you run into any issue please put it in comment.
P.s. Don’t forget to hit the recommend button if you enjoyed this article :)





