Introduction and Background
This is my, well, 2nd post on Xamarin and this one might be a bit critical regarding its topic or subject matter. So, if you have been pulling your hair out these days, this post is for you. Basically, the purpose of this post is to provide you with a post that has the most commonly faced issues, and their solutions attached. I personally have faced quite a lot of issues with Xamarin. The problem was not that I was a beginner in Xamarin, but it was all working before and right after a reset, everything was giving me an error. Even worse was that there were no solutions at all.
Every solution was like, “Reinstall Xamarin”, “Reinstall Android SDK”, “Remove the space in the Android SDK path”. Even the most authentic of the resources were not providing the working or at least “sensible” solution to me. So, I thought of writing a complete post, sharing the reality based solutions, and not just, “install this, install that” sort of stuff that does not help anyone at all.
Installation of Xamarin
Installation of Xamarin itself is a bit confusing and problematic for beginners. If you have had ever worked with Xamarin previously, then you might know where all the plugs go, but for a beginner, the experience is painful and many leave in the beginning - let alone middle as the least.
The initial location where Xamarin installs the Android SDK is “C:\Program Files (x86)\Android\android-sdk“. A few things to consider here.
- There is no problem with the space in the path. Spaces are a problem only in the cases when you are going to program using NDK (Native Development Kit), otherwise they do not cause any problem. I am also using a path with space in it and it does not cause any trouble.
- Before anything at all, I would recommend that you run the Android SDK by running,
- Either the “bat” file as Administrator.
- Or by going to Visual Studio and running Android SDK from the Android Toolkit tray. It will request Admin access itself.
- Note that you need to run SDK Manager with Admin access. Otherwise, it will not work. Worse, it will give you errors of “Unable to move directory…” and later, it will delete the “android.bat” file and you will have to download and install Android SDK once again. Painful.
- It can be helpful to use the Android SDK installed by Android Studio in cases where you are lacking enough HDD space on your machine, but it is my own recommendation to not use that. The reason is that in case Xamarin messes up, Android Studio keeps running fine. Plus, you do not need all of the plethora that Android Studio installs just for Xamarin.
Figure 1: Android SDK launcher.
The installation typically installs Android SDK level 19, 21, and 23 (23rd one can be selected from installation options). You should not just go ahead and start installing everything, even if someone asks you to. There is no point in doing that.
Figure 2: Android SDKs and tools provided.
Later in this post, I will show you which of the frameworks are required as necessary for your project to work and which of these are not at all required.
Installation of Android SDK
Another most important point to consider here is that on various locations online, you will find people saying that you should install SDKs from the minimum one required, all the way up to the latest one. No, that is not the solution and is not required at all.
For example, have a look here,
Figure 3: Android target platforms.
In this case, which SDKs do I need to install? If you said: All the way from 16 to 23, then you are wrong. The only SDK that is required and compulsory for you to have is the “Latest Platform”. Now, the concept of latest platform is a bit different. You cannot expect to have the latest Android version released by Google, and expect that to be the latest one in Xamarin also. Xamarin is not working under Google and their APIs come a bit later than Google’s APIs.
I have only installed the SDK platform for 25 and 23. I installed 25 because everybody was asking me to install everything. Which did not work in any case. So, what you need to do is you need to install only the Latest Platform, and any platform that you need to test your application on; in the case of emulators etc.
One more thing, the latest platform will differ as you will be working at the time of writing this post, it was Android 6.0 Marshmallow, whereas Nougat was released quite months ago, even then the latest platform in Xamarin was 6.0. And in Android Studio I was already targeting API level 25. Which means, that Android Studio API level and Xamarin level do not meet each other and thus you need to check again which of them are you going to support.
Emulators in Xamarin
Xamarin, if installed with Visual Studio, comes shipped with Visual Studio Emulator for Android. It requires you to have Hyper-V installed and active… Meaning, you can only access it on a Pro edition of Windows. In other editions, such as Home, you cannot run that. In that case you either have to fall back on Genymotion, or other products such as the Android emulator provided by Xamarin itself. The benefits of using these emulators are:
- They come pre shipped and preconfigured.
- All you need is a Pro edition of Windows (in case of Visual Studio’s emulator), or you need a commercial account; such as for Genymotion unlimited account.
However, my choice is a bit different. I consider using the same Android emulators that I used with Android Studio. There are various benefits to this,
- You get the latest API levels beforehand. My latest platform (as seen above) is Android 6.0, however, I get to run the application in Android 7.0 using the emulators by Android Studio. Fun eh?
- You can use Intel HAXM, and it works even if you do not have a Pro edition of Windows. However, a CPU with virtualization technology is required — of course, an Intel CPU.
- Visual Studio automatically detects the running Android device, and you can push your application to the running emulator and run it in super-fast mode.
However, if you want to run your application on one of the Android Studio’s emulators, then you need to make only one thing certain: The platform level of the emulator and the platforms installed in Xamarin.Android SDK is similar. For example, if I have a device of Android 7.0, and I have not installed Android 7.0 as a platform level in Xamarin.Android SDK, then the application will not deploy; although it will build, it will try to deploy, but will not fail nor succeed. To overcome that, install the same SDK level in your Xamarin.Android as well as the Android Studio SDK. Then you can deploy your applications.
One thing and purpose of having Android 7.0 installed on Xamarin, was that I was testing my applications on Android Studio’s emulator, which had Android 7.0 installed, to have it accept the application, I needed to install Android 7.0 SDK on Xamarin as well. Otherwise, it won’t start debugging at all.
Figure 4: Android emulators shown in Android Studio AVD manager.
If you look close enough in the following, you will find a lot of Easter eggs; Android 6.0 as target, yet running on Android 7.0 and so on.
Figure 5: Android Studio emulator with Android 7.0 running a Xamarin application targeting Android 6.0 application.
You will also see, that the Android emulator being used is the Android Studio’s emulator and not any other emulator and it runs and works just perfectly.
View not loading
Most of the times you will get an error message saying, Android SDK is outdated. Or to be specific, the error message is,
The installed Android SDK is too old. Version {API_LEVEL} or newer is required.
Then it provides you with a link to open Android SDK and install it. The problem here is that you are trying to target your build to a version, that is not installed at the moment. Such as, in my above example the target was Android 6.0 and the only SDK installed was 19 (the default one). That caused the problem for my setup to target the views to the latest API level.
In most posts, it is shown to actually either update the paths, install everything, or move the directories from one location to another, or even reinstall Xamarin.
The solution to this problem is pretty much straight-forward. All you need to do is:
- Go to the Properties → Application.
- Check for the “Compile using Android version:” value. Also note that the “Target Android version” can be selected as the “Use Compile using SDK version” to make things a bit simpler.
- Finally, install the SDK for your target platform level.
One thing to note here, if your SDK shows that you have installed the platform however, you cannot run the application. Recheck the location of SDK being used.
Figure 6: Android SDK default paths in Visual Studio.
- For that, go to Tools → Options → Xamarin → Android Settings.
- Double check the Android SDK location property here. And make sure it is the location where your SDK is installed.
These will set up a few things in the system so that Xamarin works the way it should.
Java SDK required
In most cases, Java JDK 8 is recommended. By default you will be provided with JDK 7, and that works perfectly. But it is recommended to install JDK 8 and remove JDK 7. Reason?
- JDK 7 is old. Really.
- JDK 7 will cause your applications to target JDK 7, even if JDK 8 is installed because it overwrites the default JAVA_HOME variable. Since, JAVA_HOME variable needs to target JDK 8 location, there is no need to have JDK 7; since it will never be used.
- The latest Android tools will be using and supported by JDK 8. Soon Xamarin will also require you to have JDK 8, because while compiling Xamarin to Android, it uses the Android libraries as well; SDK etc. They require JDK 8.
A simple step to do this would be, to remove the JDK 7 completely. Go to control panel for that. Next, set the JAVA_HOME environment to point to JDK 8 location. That will be different on devices, based on the build or versions. So, check it against your own system.
Final words
Xamarin itself is a very powerful tool provided by Microsoft. Plus, the benefits of Xamarin, especially Xamarin.Forms, outweigh any disadvantages of it. The main disadvantage of this is, the learning slope is really very slippery - not just steep. Most of the beginners leave out learning the Xamarin framework, because learning a simple language such as Java and having maximum code already provided is an easy way to have your work done. Whereas, in Xamarin you need to not just learn the tools but also to understand which plug goes to which socket.
I tried my best to provide you with a post that has the solutions to the most widely faced problems. The problems talked about here, were all generic and not a specific case because I needed future readers to also get help from this post.
If you find any other error, do let me know by commenting and I will try to find a solution - a real solution - and then share it with you and the rest of the community.