Leveraging PDF417.mobi Android component to Xamarin.Android
As we can see here Xamarin.Android brings us the possibility to bind a Java library and use it in our Xamarin app.
This feature allow us to use Java libraries in our Xamarin projects, and reuse our self libs or components of thirds parts.
In one of our lasts projects we needed to integrate a specific bar code scanner component in a Xamarin.Android app.
Here we will talk about, how we have created a Xamarin.Android control using http://pdf417.mobi/ component in a Xamarin.Android app.
Downloading the component sample project, we can see that it is compound by an Android library (LibRecognizer.aar). When creating the binding library we should add this file to the project with the build action “LibraryProjectZip”.
When we start creating the binding library, we get a lot of binding errors due to the use some Java features that cannot be mapped by Xamarin.
At this point, we could fix these issues modifying the binding behaviour as explains the link at the top of the article.
Or the chosen option, that is to create a wrapper Android library in Java that exposes only the required functionality.
With Android Studio we can generate an Android library (.aar extension), and implement a class that extends a layout control, including the needed methods and an specific result listener.
When we build this project we obtain an aar file, that we can use in our Visual Studio Binding project. When we replace the LibRecognizer.aar file with the generated aar file, we get a lot of errors. The cause is that Visual Studio doesn’t found classes from LibRecognizer.aar, to fix this, we need to extract the content from LibRecognizer.aar (it is a zip file), and include the classes.jar file with the build action “EmbeddedReferenceJar”. Now the project must build successfully.
Now we can add this project as reference to a Xamarin.Android project.
The last thing to do, is include the native files of PDF417.mobi component to our Xamarin.Android project. We need to create a lib folder in the root of the project, and add the files we can find in the folder created when the LibRecognizer.aar was extracted inside /jni/ folder.
We can finally use the control in our Xamarin.Android app.
You can download the code from: https://github.com/danielcaceresm/Xamarin.PDF417.mobi