Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate Xamarin binding for AAR with many dependencies?

I'm trying to generate a Xamarin Binding for Android com.adobe.creativesdk.image library

The problem is that this library contains like 20 references and these references for sure contains much more. I'd have to explore the entire tree and extract all references to add them to the binding project.

For sure there must be an easier way to do this. I was told that I could create a bundle AAR that includes all references using Android Studio but I'm not certain about steps to make it work. Some of the references are AAR too.

Could you please put me in the correct path to get this binding working?

like image 521
Claudio Redi Avatar asked May 04 '16 17:05

Claudio Redi


3 Answers

Binding libraries always have their own Caveats, but I'm going to do some generalization to help keep you organized.

  1. Check the dependencies involved and if any of them are already on Nuget USE THOSE. Things like Android Support Libraries, Google Play APIs, IBM SDKs, etc. Most of them already have bindings and are in Nuget.

  2. Don't forget to check your dependencies version numbers against the ones in Nuget. Maybe your .aar lib is actually more up to date than the Binding on Nuget.

  3. Separate big libraries. Find the rest of your dependencies that are not in Nuget that are relatively large, then create separate Android Binding Library Projects for each of them. Commonly the Binding APIs will fall apart with large libraries as dependencies, so it is far easier (and cleaner IMO) to create separate projects for them, then add project references to your end library.

  4. Separate your jars from aars. Usually your .aar files are the big ones that get their own bindings anyway and loop into #3, but jar dependencies should use ReferenceJar Build Actions.

  5. Play with your MetaData.xml. This can take a long time, but the Binding APIs aren't perfect. Use the MetaData and other files to get it cleaned to your liking.

Some other things that might help:

  • https://marketplace.visualstudio.com/items?itemName=EgorBogatov.XamarinGradleBindings (VS plugin to create binding libraries from gradle dependencies. Imperfect, but a great tool for getting started. Don't forget to move your .jar and .aar files into your Jars folder since this adds them to the project root for some reason)
  • https://developer.xamarin.com/guides/android/advanced_topics/binding-a-java-library/customizing-bindings/java-bindings-metadata/ (understanding some good ways to update your MetaData.xml file)
  • I would also look around GitHub for some existing Bindings projects for Android libraries of similar size for thinks like AWS services or other Apache libraries
like image 193
SuavePirate Avatar answered Nov 07 '22 18:11

SuavePirate


I had the same issue, and i finally solved it by simply adding the dependencies. Many can be added via the NuGet packages (rightclick on bindingproject and click add nuget packages...), the most common ones like play services, gson okhttp3 etc. can be found here. If you, like me, had some more uncommon dependencies, you might need to add them as bindingprojects themselves.

I read that you could add the aar/jar dependency to the bindingproject directly, and set build action to referenceJar or embeddedReferenceJar, but this did not work for me. Im relatively new to xamarin so im not sure if its just not the way its intended to be used or if it was because they could not automatically be bound due to execution error (got some errors and warnings that i needed to fix in the metafile when i later added the dependency jar/aar into its own bindingsproject).

Regardless, the way i got it to work was by adding a new bindingsproject to the solution, in which i added the dependency jar/aar and then built it and added the needed rows/nodes in the metafile to make it compile. Then i simply added the "dependency-binding" project as a reference in my actual bindingsproject.

This feels more like a workaround than an actual solution, but atleast i got it to work, so hopefully it can help you.

like image 2
Joachim Haglund Avatar answered Nov 07 '22 18:11

Joachim Haglund


Heads up for anyone which needs to create a wrapper around an aar/jar java dependency to make it play nice with C#. You are best referencing the aar dependency through a binding dll than trying to add it into the same binding, xamarin cannot use more than one aar per binding. Write the wrapper in it's own aar/binding, and have the wrapper binding/dll reference the dependency also as a binding/dll. Also - you're better using an aar file than a jar, jar seems to be kind of deprecated these days.

like image 1
Pellet Avatar answered Nov 07 '22 18:11

Pellet