I’m quite new to Xamarin Forms; I’ve managed to get projects working before now, but I find myself stumbling through the same problems again and again. This is one such issue – so, hopefully, now I will come across my own article first!
You create a new Xamarin Forms app, press F5 and you get an error similar to this:
Xamarin.Android for Visual Studio requires Android SDK. Please click here to configure.
If you double click this, it should launch the Android SDK Manager.
The problem is, in fact, that there are many Android SDKs; so you need to install the correct ones for the systems that you’re targeting. Start with the SDK Manager; if it hasn’t already launched, then launch that here:
Next, have a look in your Xamarin output (that’s the standard output window, just select output from Xamarin in the drop-down):
Okay, so now you know what’s missing. We should now be able to find those versions of the Build Tools in the SDK Manager:
Once you’ve made the changes, select “Apply Changes” at the bottom.
It would be nice if that was it; however, you may have to restart Visual Studio before it realises what you’ve done.
Being relatively new to Xamarin, I naively expected any errors to just show up, you know, like when you run a console app after headbutting the keyboard, it gives you some vague indication that there’s a problem with your code.
My story starts with the default template of Xamarin, running just an Android application. I just want to mention again that this is the default template (admittedly I am running VS2019 and .Net Core 3.0 – at the time of writing, .Net Core 3.0 is still in preview).
Anyway, I start writing my app, and everything is running fine. I add a button, and it appears, I do something on button press: it does it – I’m on a roll! Then I add a chunk of code that calls an API… and suddenly the app just doesn’t run. It compiles and deploys fine, but it doesn’t run. At all.
It occurred to me that this does, potentially, make sense. The code that’s generated may now be complete garbage. In the same way as if you headbutted the keyboard in your console app, the C# compiler will simply run and JIT your C# into invalid IL… Except, that’s not what happens. No sane (statically typed) compiled system would compile a bunch of crap and deploy it… but hey ho.
So, why would my app not run?
Well, it was down to the following line:
var data = JsonConvert.DeserializeObject<List<MyData>>(contentString);
The reason being that, by default, JSON.Net is not installed in the default template; however, because it (or a version of it) is a dependency of one of the other libraries, it is accessible! Presumably there’s a conflict somewhere, or when this compiles it produces a big pile of steaming Java!
(I realise it doesn’t compile down to Java – but I think you’ll agree, steaming IL doesn’t have the same ring to it.)
Anyway, the moral of the story is: check your Newtonsoft dependencies, and liberally distribute try / catch blocks everywhere – it seems to be the only way to get a half sensible error from Xamarin.
Xamarin Forms is one of those technologies that I keep meaning to play with… but then don’t. Anyway, this is the story of the day that I first did… the intention was to spend a Sunday afternoon getting a quick prototype of a Xamarin Forms app running… but instead, I encountered error after error.
In the end,all I ended up producing was this post!
The first error after creating a new app:
This gave me the impression that I might have the wrong version of Android; so off I went to install the latest JDK; from here.
Thus began a process that took hours to complete…
Launch Android SDK Manager and install the latest (or a later) version:
Amazingly, this did seem to fix (or at least, change) the error.
The next problem was a build error from the Android project:
Error: No resource found that matches the given name: attr 'colorAccent'
There’s two things that were necessary here; the first was to make sure that this package was installed:
Suddenly, it decided there was an issue with some of the resources in the file ‘Tabbar.axml’:
1>C:\Code\MyApp\MyApp.Android\Resources\layout\Tabbar.axml : error APT0000: 1: error: No resource identifier found for attribute 'tabIndicatorColor' in package 'com.companyname.WizardGame'
1>C:\Code\MyApp\MyApp.Android\Resources\layout\Tabbar.axml : error APT0000: 1: error: No resource identifier found for attribute 'tabGravity' in package 'com.companyname.WizardGame'
1>C:\Code\MyApp\MyApp.Android\Resources\layout\Tabbar.axml : error APT0000: 1: error: No resource identifier found for attribute 'tabMode' in package 'com.companyname.WizardGame'
Taking inspiration from my Spectrum programming days, and having no clue what the hell was going on at this stage, I just deleted these lines… and got a different error:
error CS0012: The type 'AppCompatActivity' is defined in an assembly that is not referenced. You must add a reference to assembly 'Xamarin.Android.Support.v7.AppCompat, Version=22.214.171.124, Culture=neutral, PublicKeyToken=null'.
This didn’t make much sense, but while I was messing around in the Package Manager screen, I decided to update the Xamarin.Forms package; for some reason, the latest version seems to be (numerically) older that the older one:
Giddy with the thrill of the update working without a new, spurious error, I updated everything! This required VS to be restarted at least once.
At this stage, the project started compiling… so before Xamarin regained its footing and thought of a new error, I added back in the code I’d removed: