Resolving “Compile to dalvik: Unexpected top-level error” under Xamarin.Android

If you are using the following configuration under Xamarin.Android, you may experience an issue (Compile to dalvik: Unexpected top-level error) which prevents your build from succeeding:

  • Xamarin.Forms
  • Xamarin.Forms.Maps
  • Deploying to release

Resolving the issue

To resolve this issue, simply do the following:

  1. Right-click on your Xamarin.Android project
  2. Select “Properties”
  3. Open “Android Options” => “Advanced”
  4. Set “Java Max Heap Size” to 1G

Now, you should be able to build your Xamarin.Android project in release mode once more.

// Dave


Json.NET crashes when releasing an Xamarin.Android Application

So, after I published my last post, some users reported that the Android app I’m developing didn’t contain any content.

This seemed weird to me, as I knew for a fact, that the iOS version worked just fine.

Fortunately, I am using Xamarin.Insights, which allows me to monitor issues that may arise.

Apperently, the users whom had installed the Android app experienced a lack of content due to a JSON serialization issue which meant that no data was shown – ofcourse, this should be handled by presenting an error message in the UI as well.

The reason was, apparently, that Json.NET crashes in release mode under Xamarin.Android, but not when debugging, which is why I didn’t initially experience the issue myself.

The solution

What you need to do if you experience this issue is to follow these simple steps:

  1. In Visual Studio / Xamarin Studio, right click on your Android project and select “Properties”
  2. Go to “Android Options” => “Linker”
  3. In the field “Skip linking assemblies” enter “System.Core” (without the quotes)
    1. Note: if you have already omitted other assemblies, seperate them using “;” (quotes omitted)
  4. Save

Now, you can release your Android app once more without having Json.NET crashing.’

Testing the solution

If you want to be sure that the solution works before publishing your app to the Google Play Store, you can:

  1. Set your “Solution Configuration” to “Release”
  2. In Visual Studio, open the “Debug” menu
  3. Now click “Start Without Debugging”

Doing so allows the application to be installed in release-mode on your emulator/device.

// Dave


Publishing Xamarin.Forms.Android Application to Google Play

So, I recently had to publish a Xamarin.Forms.Android Application to Google Play. I experienced a few bumps along the way, but finally succeeded this very evening.

Preparing the .APK

First of all, you want to make sure that you disable debugging, specifiy an application icon, version the application and configure your linker.

Afterwards, you’ll need to build the package in Release mode. No problem, right?

Well, I personally experienced a “COMPILETODALVIK : UNEXPECTED TOP-LEVEL” error while doing so, which, apparently was related to Xamarin.Forms.Maps.

If you experience this issue, it can either be due to missing permissions in your Android Manifest or the Heap Size. This is all explained in the previous forum discussion.

After resolving the error, I was finally able to clean & rebuild the solution.

Signing the .APK

Now, for signing the .APK, instead of using the command line tools, you can simply use the tools provided in Visual Studio.

Simply navigate to “Tools” –> Android” –> “Publish Android App…” in the Visual Studio menu, and select “Create new keystore”.

After you have created your keystore, select where you want your signed .apk to be built to and click “Publish”.

Publishing the .APK

Now, in your “bin/Release” folder, you will have two .apk files generated.

So, here I was, thinking to myself, that I could simply upload the signed version of the .APK.

But alas, when I tried to do so, I was told (no matter what I did) that the .APK was, to my amazement, not zipaligned correctly.

I also did try to upload the aligned .APK which was generated from the publishing process, but to no avail.

So, before I was able to publish, I needed to zipalign the signed version of the .APK.

So, what I did was to grab a copy of the .APK which said “Signed” under “bin/Release”.

I then navigated to where my Androd SDK was located, and jumped into “android-sdk\build-tools\<version>” and for ease, pasted my .APK here.

Note: if you are unable to locate this folder, you probably havn’t insstalled the build tools. You can install the build tools in Visual Studio under the Android SDK Manager.

I then popped open a terminal and navigated to the build tools folder, and typed in the followng command (I, ofcourse, replaced “helloworld” with the name of my application):

Afterwards, I was finally able to upload my .APK to the Google Play Store.


// Dave


OxyPlot and Xamarin.Forms Unified API

I finally figured out what was wrong with my references in my iOS project!

Once I uninstalled OxyPlot.Xamarin.Forms, and installed the new 707-alpha PR, it was for “some reason” (actually a very good one, which you’ll see later) still referencing the Classic iOS OxyPlot.Xamarin.Forms API.

I was frustrated by this fact, since, when I created a new project, this was not an issue at all. This got me thinking.

Once upon a time, when I started my project, I was running Xamarin.Forms 1.2.3, and I have by now simply upgraded to the newest version ( I did some refactoring of my classes based on the post below, and thought that I was now running the Unified API:

Alas, upon further investigation, I found , that I had in fact NOT upgraded to the Unified API in my .csproj file:

I followed the steps, removed OxyPlot, re-installed the 707 OxyPlot.Xamarin.Forms PR from the NuGet Package Manager Console.

Hereafter, everything worked as expected, and I could finally include the OxyPlot.Xamarin.Forms package for the Unified iOS project as described in the OxyPlot documentation:

// David