Xamarin Android - txgz999/Mobile GitHub Wiki

Native Android vs Xamarin Android

Native Android project and Xamarin Android project have similar project file structure. The resources, including layout file, value files and image files, have identical content. The main difference is class definition, one in Java, the other in C#. Most of them can be transformed relatively straightforwardly, e.g.

  • Activity class
  • Firebase messaging service class
  • RecyclerView and it’s adapter and view holder
  • SQLite class
  • Preference manager
  • Local Broadcast Manager

The class that makes http communication to server is different. In Native Android project we use the Volley liberty, but in Xamarin Android project we use Microsoft’s System.Net.Http package.

Add FCM Support to Android Xamarin Project

I have problem to install Xamarin.GooglePlayServices.Base in a Xamarin Android project (have no problem in the Android project in Xamarin Forms projects). Got errors like the following:

Version conflict detected for Xamarin.Android.Support.Compat. Install/reference Xamarin.Android.Support.Compat 28.0.0.3 directly to project xaapp1 to resolve this issue. xaapp1 -> Xamarin.Essentials 1.3.1 -> Xamarin.Android.Support.Compat (>= 28.0.0.3) xaapp1 -> Xamarin.GooglePlayServices.Base 71.1610.0 -> Xamarin.GooglePlayServices.Basement 71.1620.0 -> Xamarin.Android.Support.v4 28.0.0.1 -> Xamarin.Android.Support.Compat (= 28.0.0.1). xaapp1 C:\Notes\200327\xaapp1\xaapp1.csproj The reason is this project installs the Xamarin.Essentials 1.3.1 package, which depends on Xamarin.Android.Support.Compat 28.0.0.3 or above, but the Xamarin.GooglePlayServices.Base 71.1610.0 package depends on Xamarin.Android.Support.Compat 28.0.0.1.

The solution is uninstall Xamarin.Essentials 1.3.1 package, then install Xamarin.Essentials 1.3.0 package, then I am able to install the additional packages to support FCM.

From the package dependency tree, I can explain the reason I need to downgrade dependency of Xamarin.Essentials from 1.3.1 to 1.3.0:

Xamarin.GooglePlayServices.Base 71.1610.0 -> Xamarin.GooglePlayServices.Basement (>= 71.1620.0)

Xamarin.GooglePlayServices.Basement 71.1620.0 -> Xamarin.Android.Support.v4 28.0.0.1 -> Xamarin.Android.Support.Compat 28.0.0.1

Android App (Xamarin) project -> Xamarin.Essentials 1.3.1
-> Xamarin.Android.Support.Compat (>= 28.0.0.3)

Xamarin.Essentials 1.3.0 -> Xamarin.Android.Support.Compat (>= 28.0.0.1)

But I cannot explain the same issue does not appear in Mobile App (Xamarin.Forms) Android Project, since it also uses Xamarin.Essentials 1.3.1:

Mobile App (Xamarin.Forms) Android Project -> Mobile App (Xamarin.Forms) Shared project -> Xamarin.Essentials 1.3.1
-> Xamarin.Android.Support.Compat (>= 28.0.0.3)

Installation

On my work laptop, I installed Android Studio and then Visual Studio 2019 with Mobile development. After I install Android SDK and create Android emulation device, and no problem in build but when I try to run my Xamarin Android app, got the following error

Device error: PANIC: Broken AVD system path. Check your ANDROID_SDK_ROOT value [C:\Program Files (x86)\Android\android-sdk]!

It seems that Android Studio and Visual Studio need to use the save Android SDK path and JDK path. Modified the Android SDK path in Tools->Options->Xamarin->Android Settings (the JDK path is the same already), then the app runs correctly.