Select to view content in your preferred language

Issues regarding the basemaps (Xamarin.Android)

1560
8
06-14-2022 06:56 AM
Ant0nin
Emerging Contributor


Hello Esri Community,

I using Xamarin.Android (not Xamarin.Forms) combined with the .NET ArcGIS Runtime nuGet package in the context of a mobile app development in C#. I actually have a Visual Studio solution containing 2 items:
- An Android project (Xamarin.Android) using nuGet package Esri.ArcGISRuntime.Xamarin.Android
- A C# class library using nuGet package Esri.ArcGISRuntime (this library is used by the Android Project)

I encounter issues regarding the basemaps.
First, in the context of the Debug build of my Android app, the basemaps are not displayed at all in the case I do not specify my ArcGIS licence key (Lite licence) in the code (note that they are well displayed when I specify my licence key). I expected a "For debug use only" watermark on top of the map view instead but maybe it's the normal behavior, is it?
Secondly, the basemaps are not displayed neither in the Release build of my mobile app whether I enter my ArcGIS licence key or not.

Here are 2 attached screenshots (with and without basemaps).

I do the testing both on a real smartphone and a virtual smartphone.
Real smartphone: Xiaomi 9T Pro (Android version 11)
Virtual smartphone (Android emulator): Pixel 5 (Android version 11)
In the case of the virtual smartphone, the Release version of my app doesn't even respond (white screen) whereas in the case of the real smartphone, the app starts correctly and only the basemaps are not displayed.

And regarding the output logs, I did not noticed any related error messages unfortunately. Any ideas?

More details about my current configuration:
IDE: Microsoft Visual Studio 2022
.NET Standard 2.1 (for my C# class library)
Minimum Android version: 4.4.87
Target Android version: 12.0
nuGet package Xamarin.Essentials 1.7.2
nuGet package Esri.ArcGISRuntime 100.14.0 (for my C# class library)
nuGet package Esri.ArcGISRuntime.Xamarin.Android 100.14.0

Any help would be greatly appreciated. Thanks a lot.

0 Kudos
8 Replies
dotMorten_esri
Esri Notable Contributor

When using the BasemapStyle enums to create basemaps, you must specify an ArcGIS Online API key (not license key, which is for licensing the SDK itself). You can create those in the developer portal and set it

Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.ApiKey= "my_key_here";

Alternatively you can sign into an ArcGISPortal first, and the basemapstyles will "just work".

I don't fully understand why the license key makes your basemaps work though - that shouldn't be the case.

See https://developers.arcgis.com/net/reference/migrate-to-arcgis-location-services/for more info

0 Kudos
Ant0nin
Emerging Contributor

Thank you @dotMorten_esri for your answer. 

As a matter of fact, I specify both the licence key and the API key in my code (sorry to have omitted this important detail in my first message). It may explain why the basemaps work in the Debug build of my app. Here are the two related lines of code I wrote: 

 

Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.ApiKey = AppContextData.EsriApiKey;
Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.SetLicense(AppContextData.EsriLicense);

 

...knowing that AppContextData.EsriApiKey and AppContextData.EsriLicence are both constant string attributes containing my API key and my licence key respectively.

So if I understand correctly, it's normal to not be able to load basemaps when the API key is not specified. Therefore, the remaining mystery is why the basemaps work in the Debug build but not in the Release build of my app. Any idea?

0 Kudos
dotMorten_esri
Esri Notable Contributor

It's more likely this is because the optimizer is removing types you are only declaring in xaml, than an issue with the mapview. Do you have any code that directly references the MapView type in code-behind? Otherwise it could get optimized away. You could try and disable the linker to confirm.

https://docs.microsoft.com/en-us/xamarin/android/deploy-test/linker

 

0 Kudos
Ant0nin
Emerging Contributor

Hello @dotMorten_esri  and thanks a lot for your reply.

I indeed have references of the MapView type in my C# code (By the way I don't have XAML files because I don't use Xamarin.Forms. I using Xamarin.Android so my UI is defined through traditional Android XML files). I disabled the linker as you suggested but the basemaps still do not appear in the Release build of my app.

In order to push the test further, I created a new Xamarin.Android project in Visual Studio 2022 using the default code template generated by the IDE and added the Esri.ArcGISRuntime.Xamarin.Android nuGet package into it. I then edited the XML file "content_main.xml" (part of the main view activity_main.xml) and the "OnCreate" function from the MainActivity class as it follows.

content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main">

    <Esri.ArcGISRuntime.UI.Controls.MapView
        android:id="@+id/main_map_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

 

MainActivity.cs:

//...
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.UI.Controls;

namespace test_arcgis_api
{
	[Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        Map _map;
        MapView _mapView;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            SetContentView(Resource.Layout.activity_main);

            Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.ApiKey = "__my_API_key__";
            Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.SetLicense("__my_licence_key__");

            _map = new Map(BasemapStyle.ArcGISTopographic);

            _mapView = FindViewById<MapView>(Resource.Id.main_map_view);
            _mapView.Map = _map;
			
			//...
		}
		
		//...
	}
}

 

I disabled the linker and tested both the Debug build and the Release build of this new Android project on my smartphone (Xiaomi 9T Pro, Android 11). And I ended with the same issue, namely the world map displays itself correctly on the screen in the case of the Debug build (basemaps are displayed) but not in the Release build. I also tried to downgrade the version of the Esri.ArcGISRuntime.Xamarin.Android nuGet package but it did not solved the problem.

By any chance, does my issue could be related to a kind of "limitation" of my rights regarding my API key?

0 Kudos
MichaelBranscomb
Esri Frequent Contributor

The difference between debug and release could be related to a difference in your project configuration - for example the HTTP Handler you've selected might be different between debug/release.

Here's an example project from our samples: https://github.com/Esri/arcgis-runtime-samples-dotnet/blob/main/src/Android/Xamarin.Android/ArcGISRu....

0 Kudos
Ant0nin
Emerging Contributor

Thanks @MichaelBranscomb for your answer.

I checked my project configurations (Debug and Release) regarding the HTTP handler and they are identical. So the problem should be somewhere else.

I had a look at the example project that you linked. It's actually a Visual Studio solution which includes a "Xamarin.Forms.Android" project. I compared its configuration (Android options) to my project configuration  and they are the same.

But one key difference is that I don't use "Xamarin.Forms" in the context of my project but "Xamarin.Android" instead. So it gives me an idea: I have an old "Xamarin.Forms" project on my computer that uses the same ArcGIS API key and the same configuration (Android options) than my current "Xamarin.Android" project. So I tried to compile and deploy the Release version of this old project and this one works, namely the basemaps are well loaded and displayed. So my problem seems to be a very "Xamarin.Android"-specific problem.

I also tried the following: In my "Xamarin.Android" project, I went to the Android options of the Release build configuration (reminder: its the one that does not works, namely basemaps are not displayed) and slightly changed it by checking 2 checkboxes: "Enable developer instrumentation" and "Use Fast Deployment", as you can see in the attached screenshot. I know that by doing this, it's not a real Release build anymore. But when I do this and compile and deploy, the basemaps just work. Therefore, it means my other build parameters are apparently fine.

To sum up, I believe that either there is a bug in the "Esri.ArcGISRuntime.Xamarin.Android" nuGet package, or missing additional steps are necessary regarding the assemblies deployment in the context of the Release build of a "Xamarin.Android" project. I saying this because of the "Fast Deployment" option that prevents the basemaps to work when unchecked. What do you think guys?

Thanks in advance for your help.

 

0 Kudos
PreetiMaske
Esri Regular Contributor

I am sure you have already checked it, but I have seen sometimes manifest is different in debug and release build. I suggest select release build and ensure Internet is checked on for release builds in package manifest.

0 Kudos
dotMorten_esri
Esri Notable Contributor

The Forms nuget isn't really anything but a wrapper around Esri.ArcGISRuntime.Xamarin.Android - the MapView control as well as the Map and Basemap classes etc are all coming from the same assemblies. So if Xamarin.Forms works, it is likely there is a linker difference between your two android projects.

0 Kudos