I am trying to build Xamarin Android binding libraries for two different RFID hand held scanner SDKs (from two different companies), and then reference them in a Xamarin.Android project. So the end goal is to have one application that can run on Device A or Device B and depending on the device manufacturer it will use a different implementation of a "scanner" interface. If I try to reference both of the resulting dlls from the Xamarin.Android project, then I get the following error:
Program type already present: com.hsm.barcode.DecodeOptions
Looking in the jars using JD-GUI as suggested in the Microsoft Docs I can see the problem is that both of the jars have a com.hsm.barcode
package:
What is the best way to workaround this issue?
Note that if I use only one of the dlls then I have no issues.
What I have tried:
- Using a single Xamarin Android binding library project for both jars - this gave exactly the same result
- Renaming all of the classes in the jar to eliminate duplicate class names like this:
<attr path="/api/package[@name='com.hsm.barcode']/class[@name='DecodeOptions']" name="name">DecodeOptions2</attr>
. When I do this, in reflector I can see that the class name has indeed changed, but I still get the build error when building the Xamarin.Android project - Renaming the namespace in one of the projects:
<attr path="/api/package[@name='com.hsm.barcode']" name="name">com.hsm.barcode2</attr>
. Again, I can see the updated namespace in reflector but still I get the same "Program type already present" error when building the Xamarin.Android project - Similarly I have tried removing both the namespace and the duplicate classes using
remove-node
but seen similar results
This leads me to believe that this isn't necessarily an issue with the binding process but rather a more Android related problem. I have found some similar Android questions where people mention that you can use exclude module
in gradle to remove dependencies [1] [2], but a) there seems to be no concrete answer that this is the right approach in this case and b) as I have been able to find gradle is not a tool that is used as part of the Xamarin/Visual Studio process.
My final desperate attempt to get something working was to unzip one of the jar files, remove all the .class files that are causing issues, then rezip and use this in the Android binding library and then reference this dll in my Xamarin.Android project. This seems to work (the project builds and runs) but I'm not sure it is the correct/safest/most stable solution.
To summarize, the question is: if you have two jars with duplicated namespaces/packages and you want to use both of these Jars in a Xamarin.Android project how can you avoid the resulting Program type already present: com.hsm.barcode.DecodeOptions
error message.