parse-community / Parse-SDK-dotNET

Parse SDK for .NET, Xamarin, Unity.
http://parseplatform.org
Apache License 2.0
323 stars 260 forks source link

Compiling Xamarin in Andoid Exception #328

Closed diasleo98 closed 4 years ago

diasleo98 commented 4 years ago

I am trying to compile my Xamarin project with an Andoid device and it threw this exception: System.TypeInitializationException: 'The type initializer for 'VersionInformation' threw an exception.' The device that I'm using is an Samsung SM-J410G (Android 8,1 - API 27)

Exception

TobiasPott commented 4 years ago

Hi @diasleo98,

could you elaborate which version of the Parse SDK you are using? From your message I cannot tell whether or not you are using the NuGet package or a self-build version of the master branch of this repository or anything else.

Kind Regards

diasleo98 commented 4 years ago

Sure, its a self-build version of the master branch of this repository, first i was using the NuGet Package and I was getting this error A2F93A15-F226-4F53-B416-9F9EE1317A90, than i changed to the self-build version and the exception that i mentioned started popping up

TobiasPott commented 4 years ago

Hi @diasleo98

your problem is one often discussed in other issues aready and I may suggest you take a look at those to get a fast solution (unfortunately it's more of a temporary one as no direct solution is available for this repository). Please take a look at #316 and the discussion in #321 about a more permanent solution. The issue you have is related to the use of GetEntryAssembly() which does not provide any result on Xamarin for Android or iOS, due to the nature the managed .NET part of an app is nested inside an native context.

I hope you can solve your issue with this hints. The NuGet package is indeed outdated, as it hasn't been maintained for quite some time. We are in the process to reorganize access and update the package available via NuGet.

Best Regards Tobias

diasleo98 commented 4 years ago

Thank you @TobiasPott, just took a look in all off both discussions, I've seen that you uploaded a new branch: https://github.com/TobiasPott/Parse-SDK-dotNET/blob/feature/implement-target-platform-configurations/Platforms.md , but I've used it and the exception persists. What got me rid of it was the solution to change the GetEntryAssembly to GetExecutingAssembly, but as I see it just works for testing the app, it is not a permanent one. I saw that @Lunjio gave a suggestion aswell, to create that PlatformInitialize method and I was wondering if you have tested it and if it works. If so can you help me implement it? I didin't understand where exactly I'm supposed to create both methods, and call the PlatformInitialize one , if it is in the MainActivity.cs file in the Android folder or else. Thanks a lot for your attention and sorry for the lack of experience.

TobiasPott commented 4 years ago

@diasleo98 Yeah, the link to my feature branch only provides the base of a configurable solution to use the Parse SDK on different platforms and adjust to the requirements or differences of them. I didn't had the time to make a sample platform configuration for Xamarin on Android or iOS as I'm using xamarin in my hobby projects only.

You may take a look at https://github.com/parse-community/Parse-SDK-dotNET/issues/316#issuecomment-585383081 where @NadjibR mentiones the source files where to change the GetEntryAssembly calls to GetExecutingAssembly. I can't remember the usees of the method out of my head and would need to search for them too.

The suggestion made by @lunjio is only a step further to the fix mentioned by @NadjibR as he replaces the explicit use of a method with a wrapper property which would allow an easier change of the method used for a specific platform. Nonetheless the property would need to be put at all places in the original code where GetEntryAssembly was called. As this is a very specific fix for Xamarin builds on Android (and maybe iOS) I would prefer a more generic one but the idea behind it is very similar to the one my feature branch is following, which is wrapping all methods and calls which tend to be problematic on different platforms and allow the developer of an app override them with appropriate ones. Unfortunately I'm did not have the time to work out a proper configuration one for Xamarin and only had a primitive one used in a Xamarin app for Android Wear but I have it on my list to provide a working one for Xamarin.Android & iOS aside the one for NETStandard and Unity.

TL;DR: Look at NadjibR's answer.

Kind Regards

TobiasPott commented 4 years ago

@diasleo98 a quick update from my side. I've added an xamarin configuration to my feature branch (see one of the linked issues above) which does work for me, when using the Parse SDK in an Xamarin for Android build (using Xamarin.Forms). I have not updated the readme or any guide on this but if you want to try it out you may be able to do so by setting the target platform configuration class before you do anything else with Parse.

public static async void SomeMethod()
{
    // sets the target platform configuration to a Xamarin config object (it is required to set it before you call ParseClient.Initialize(...)
    ParseTargetPlatform.CurrentPlatform = new ParseXamarinPlatform();
    // registering your custom ParseObject classes if needed
    ParseObject.RegisterSubclass<CategoryCollection>();
    // initializing the Parse SDK to use your specific Parse server instance
    ParseClient.Initialize("[...]", "https://[...].herokuapp.com/parse/");
}

The above snippet should give you a glimpse on how to set the target platform config. It would help me if you can test it and give me feedback whether or not it works for you and if not what problems you are encountering. I did not test it with Xamarin.iOS yet, but I assume it should work as it does on Android.

Kind Regards

Edit: As a sidenote, I've tested it on with an app running on iOS Simulator and it worked without problems, I'll test on an actual device as soon as I have access to one in the next days.

diasleo98 commented 4 years ago

Thanks for your attention @TobiasPott! Just tested it and it works fine, as soon as I get a Mac i can test it on IOS aswell. So this means that we have the permament soluttion that was needed? If so, there are any other Parse resourses that do not work properly on Xamarin also?

TobiasPott commented 4 years ago

You're welcome =). It is not a permanent solution yet, as long as I didn't merge it with the official parse community repository (but that's only a question of time and when the NuGet access is available again).

Actually I cannot tell if there are other parts of the SDK not working properly in any Xamarin context. We will depend on developers using Xamarin to report issues and fix them as they occur. It is a unfortunate situation as most problems reported in a Xamarin context are similar or identical to your problem. Hopefully getting people beyond this issue will allow the SDK to be used more often and more bugs or issues may surface.

If your question refers to general Parse features not working on Xamarin target platforms, yes I think there are some, e.g. push notifications, which are not implemented, I believe LiveQuery is a newer feature available in other Parse SDKs but I'm not aware of the actual functionality and only heard about it from a request by another user.

diasleo98 commented 4 years ago

I see... So for mobile development the recommended is to use the native tools with Parse, isn't it? Xcode and Android Studio have everything already working @TobiasPott ? Thanks for your attention regarding the previous matters.

lunjio commented 4 years ago

I see... So for mobile development the recommended is to use the native tools with Parse, isn't it? Xcode and Android Studio have everything already working @TobiasPott ? Thanks for your attention regarding the previous matters. Hi, actually not, you can use any tool you find comfortable. Have you solved your problem with using the Parse SDK for net in the Xamarin project on Android ? If not i can send you working dll or actualize my own forked repository, so that you just need to download, compile and use compiled dll.

diasleo98 commented 4 years ago

Hey @lunjio, yes I have, thanks for the offer, @TobiasPott latests changes solves it! The reason that I`m asking this is because .Net SDK has some unimplemented functionalities, such as Push notifications, that are important for Apps. If I want to use them I will have to implement them by myself, in the native tools they are already implemented and ready.

TobiasPott commented 4 years ago

@diasleo98 It's not that easy to say and I cannot tell anything about the feature set provided by the Android or iOS native SDKs. You would need to check on those repositories. Regarding the push notification, yes they most likely will be different and you would need to figure out a way to provide them by yourself when you are using the Parse SDK for .NET (unless there is a solution for that, I'm not aware of, which is definitely possible).

But in general if you need access to the more basic Parse functions (storing, querying, login and such) The .NET SDK is as good to be used with Xamarin targeting Android or iOS as the native ones are, I believe.

lunjio commented 4 years ago

@diasleo98 I think you can look for a custom cloud code function, may be there are one, and you could just copy this function in your cloud code, and use it as custom function from your client and sdk. Implementing yourself it's hardest way, as i think.

lunjio commented 4 years ago

diasleo98 in your case to start fast and building MVP or just test youself in developing, you also can use any other sdk for push, for example Microsoft App center, so just follow easy way, if you need quick start and have not so much experience

TobiasPott commented 4 years ago

As this discussion starts drifting towards different topics I would assume it is better to open a new thread for discussions about push notifications and such. A quick search and look into the App Center online documentation states that the push functionality will be retired, thus it would be good to point out the alternative to migrate to Microsoft mentions in the migration guide https://docs.microsoft.com/en-us/appcenter/migration/push/index