Xamarin Android binding duplicate packages causing "Program type already present" build error - xamarin

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.

Related

UnityPlayer_UAP_ARM64_release_il2cpp.pdb not loaded

I've installed several packages in a Unity (2020) app I'm building. Namely: World Locking Tools, MRTK, PUN2. World Locking Tools provides some examples that are built with assemblies defining scripting symbols that any derived files I want to build would need access to. I'd like to create my own version of certain files from one of these examples. To do this and have access to the scripting symbols in one of those examples, I created an assembly reference to reference the assembly for that example code within World Locking Tools package installed in my project space. In so doing, I kept running into the common "...not found, are you missing an assembly reference" issue, which ultimately led me to creating assembly references for many of the assemblies provided by the various packages I needed to use (not just the particular example code I initially wanted to modify). Once my project finally built successfully in unity, I then tried to upload to a Hololens 2 headset in Visual Studio. This provided the error in the title of this post. Coincidentally, the app also appears to fail to start on the headset. Before I tried to alter my codebase with modified versions of files from that World Locking Tools example and with assembly references, the code would successfully build in Unity and successfully deploy to the Hololens 2 headset.
Does the behavior and missing .pdb error I describe mean that I'm missing assembly references for yet other packages even though Unity successfully builds the project?
This is my first foray into using assemblies so be please be gentle :)
For the .pdb message, that one is ignorable since will not have that symbol available. However, the app not starting is definitely likely due to a missing component or loading issue. When ran in debugger, may get a better idea on what is missing or from a debugger log in Unity.

Xamarin Shared Library namespace cannot be found

My shared library cannot resolve using RestSharp; among other related namespaces. RestSharp is an installed package and is resolvable in the Droid/iOS projects but cannot be found in the Shared Library. I know this works because I'm using RestSharp in a MonoGame project in the same way. The shared library is referenced in the Droid/iOS projects but still fails during compilation.
Any advice? I tried clean all.
Update I created a new Cross Platform Native app solution and I was able to reference in my shared library without issue. That original project is a bit older and was created with PCL originally. I still want to know why it's not working in my original solution.
I continued to have lots of namespace and type not found issues. Once I showed warnings instead of just errors I found that the csproj was missing. After removing the reference in the native apps and re-adding the issue was resolved. Quite frusterating.

Xamarin binding for AWS SDK

Summary
When binding a JAR/AAR built on top of aws-java-sdk, do you have to EmbeddedReferenceJar all the libraries below your JAR, down to and including the aws-java-sdk, when down at that low level the AWS SDK for .NET has the functionality.
Are there any Xamarin packages (Xamarin.Forms or Xamarin.Android) that give an out-of-the box authentication UI with Sign-In/Sign-Up buttons for AWS user pools, Google+, Facebook authentication providers ?
Context
I would like to use SignInUI from aws-android-sdk-auth-ui in a Xamarin.Android app, which gives you an out-of-the-box UI for authentication to AWS. I've got the AWS UserPool and AWS IdentityPool setup already.
Unfortunately, the AWS SDK for .NET does not expose the equivalent aws-android-sdk-auth-ui a java package built on top of aws-android-sdk-auth-core (also unavailable in .NET), which is built on top of aws-android-sdk-core and finally aws-java-sdk.
I thought about binding a java library. I would create a Xamarin binding project for aws-android-sdk-auth-ui.aar downloaded from maven Central. I followed the Binding an .AAR tutorial. It builds, but the generated aws-android-sdk-auth-ui.dll file is missing some classes. I opened the dll with Ildasm.exe and in the Com.Amazonaws.Mobile.Auth.UI namespace I only found AuthUIConfiguration and BuildConfig, clearly missing the SignInUI class.
Is it expected that when you build a binding project for an .aar file that references other .jar, the build succeeds but silently does not produce wrapper classes for java classes who reference missing jars? I would have guessed building would succeed AND would include a wrapper for all classes, but a failure would occur at runtime in codepaths that require the missing jar.
Alright, so I added aws-android-sdk-auth-core.jar, aws-android-sdk-core.jar to the bindings project, as EmbeddedReferencedJar. That did not help, the generated dll is also missing the same classes. The build output pasted below does point in the direction of misssing reference jars. See below.
How far do I need to go though, do I also need to embed all the way down to the 80MB aws-java-sdk.jar?
I have the following build output:
1>JARTOXML : warning J2X9001: Couldn't load class com/amazonaws/mobile/auth/ui/SignInActivity : java.lang.NoClassDefFoundError: android/support/v7/app/AppCompatActivity
1>JARTOXML : warning J2X9001: Couldn't load class com/amazonaws/mobile/auth/ui/SignInUI$1 : java.lang.NoClassDefFoundError: com/amazonaws/mobile/auth/core/DefaultSignInResultHandler
1>JARTOXML : warning J2X9001: Couldn't load class com/amazonaws/mobile/auth/ui/SignInActivity$SignInProviderResultHandlerImpl : java.lang.NoClassDefFoundError: com/amazonaws/mobile/auth/core/signin/SignInProviderResultHandler
android/support/v7/app/AppCompatActivity indicates that com.android.support:appcompat isn't found. How are those supposed to bind? In Xamarin.Android these classes are in
Xamarin.Android.Support.v7.AppCompat Nuget package. But the jar does not know it's running in Xamarin.Android. Does that mean I also need to EmbedReferenceJar all the native Android stuff as well? That's a deep rabbit hole...
com/amazonaws/mobile/auth/core/DefaultSignInResultHandler: that one should not error, I did embed aws-android-sdk-auth-core.jar with EmbbededRefenceJar
Update:
Someone referred me to Xamarin.GradleBindings. I haven't tested this yet, but thought I'd share in hopes this can be useful to other readers.

Where to specify <RunCommand> tag in .Net Core .csproj file

Where to define this command in csproj file?
bin\Debug\net47\MyApp.exe
I m using .net core 2.0.0 SDK. I m not getting RunCommand in Intellisense. Also while building my solution, I have 7 projects and main project is of type console application and its Output Type is EXE. Solution gets built successfully. While I press F5 it gives me MessageBox containing message like "Unable to run your project. The RunCommand is not defined."
How to solve this?
This is typically set by the SDK. Most likely you are targeting netstandard2.0 (was it a Class Library project that you later added a Main() to?). Try targeting the specific runtime+version - in your case netcoreapp2.0 or net47 - instead.
If you want to build both Core and Full Framework you can specify multiple target frameworks by separating them with a semicolon and changing the TargetFramework tag name to plural (TargetFrameworks):
<TargetFrameworks>net47;netcoreapp2.0</TargetFrameworks>
Alternatively, you should be able to specify a <RunCommand> inside <PropertyGroup> in your .csproj file. FWIW I can't seem to get anything to show up in Intellisense except the generic <PropertyName> and anything I've already added to the list.
An example would be:
<RunCommand>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).exe</RunCommand>

Unable to convert classes into dex format Unity

Using the google play games services and Admob plugin I cannot seem to get my back to build into an APK and I am receiving the following error message. Any help would be appreciated as I have removed some duplicate files but I am still failing to find why I cannot build my app.
I am using Unity Unity 5.6.1f1 (64-bit).
game services: https://github.com/playgameservices/play-games-plugin-for-unity Ads: https://github.com/unity-plugins/Firebase-Admob-Unity
CommandInvokationFailure: Unable to convert classes into dex format.
C:/Program Files/Java/jdk1.8.0_121\bin\java.exe -Xmx2048M -Dcom.android.sdkmanager.toolsdir="C:/Users/Jack/AppData/Local/Android/sdk\tools" -Dfile.encoding=UTF8 -jar "C:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer/Tools\sdktools.jar" -
stderr[
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/android/gms/internal/zzbyb;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/android/gms/internal/zzbyc;
2 errors; aborting
]
Some Background
When Unity builds your project for Android, it invokes several tools from the Android SDK. As part of that process, it converts all of your native (Java) code for Android into a file format called DEX (Dalvik executable).
All of your Android plugins get built up into a single package using that tool. The problems start when a few plugins have the same compiled Java code (classes) in them. This will cause the DEX tool to fail with an error like the one you're seeing:
Uncaught translation error: java.lang.IllegalArgumentException:
already added: Lcom/google/android/gms/internal/zzbyb;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/android/gms/internal/zzbyc;
This means that classes with those names were already included in another library, and so they cannot be added again.
Possible Causes
As explained above, this error occurs when you have duplicate plugins in your project; this can happen in different scenarios, here are some examples:
The same plugin is included more than once, under different folders of the project.
The same plugin is included more than once with different versions.
A plugin contains other dependencies "embedded" inside it, but these dependencies are already included in the project in some form.
How To Fix
You should look up duplicate Android plugins in your project and eliminate them (keep only 1 copy). From the error message you posted, the issue here is related to Google play services libraries. You should look into that (libraries named play-services-xxxx.aar).
Paid Help (Shameless Plug)
I provide a professional service for fixing this exact kind of issue. In case you (or anyone else) are not able to resolve such an issue themselves, feel free to contact me and get it solved.
I was facing the same problem after add admob in my project, here are 2 possible solutions:
Go to the file >> build settings and change the option internal to gradle
Open libs folders separately of both Facebook SDK and Google Play services and delete the matching files
It would be something like (support v4)
Google ads 11.2 has errors. It reuses certain jar classes .
On its own, it isnot an issue
Once you add in another google class, ie firebase, your done for.
There is a fix though. Google put out firebase 4.2 and ads 11.4

Resources