In order to try to troubleshoot an issue I'm having trying to create an app targeting Android Pie using Google Maps, I've decided to start a very small project to target and isolate what may be the problem I'm having, here's what I got...
- Create a new Android project targeting Android Pie, updated the support design library (Xamarin.Android.Support.Design) to 28.0.0.1 because by default the template seems to set the target at 8.1. I then added the the nuget package "Xamarin.GooglePlayServices.Maps". Which generated a warning about a dependencie not beign updated (Xamarin.Android.Support.Media.Compat) and still at 26.0.2. So I manually update it at 28.0.0.1.
- Create a very simple layout named activity_main
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"
android:layout_marginTop="24dp" />
</LinearLayout>
Which is only a linear layout containing a single google maps fragment
- Create the activity class (MainActivity.cs)
MainActivity.cs
namespace SimpleMapTest
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
}
}
}
Which only set the content view defined in the layout file
- Adding the required lines in the manifest for my Google Maps API key
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="SimpleMapTest.SimpleMapTest">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="[MyKeyValue]" />
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
</application>
</manifest>
- Compile the app. The app deployed ok, started, but when the map is about to be shown, I got this error.
- Then, without changing a single line, went to the project property screen and changed to compile against Android Oreo (8.1)
- Compile the thing, deploy and run the app, the app worked successfully
- Got back to the project property, change the target back to Android Pie (9.0) and recompile and deploy. App compiles, deploy and start running, but this time the exceptionI got at runtime was this one:
- Searched online and found that I may need to work around this error by adding the following line to the manifest
Manifest
<uses-library android:name="org.apache.http.legacy" android:required="false" />
I guess it should be something about Android 9 changing the way it handles HTTP request
- I've added the line in the manifest, recompile, deploys and at runtime got back the same exception I got the first time, concerning a timeout
- If I check the Output, and got this after a Timeout Exceeded exception details
Output
02-10 10:50:48.352 W/t.SimpleMapTes(28585): Unsupported class loader
02-10 10:50:48.358 W/t.SimpleMapTes(28585): Skipping duplicate class check due to unsupported classloader
02-10 10:50:49.082 E/AndroidRuntime(28585): FATAL EXCEPTION: Thread-6
02-10 10:50:49.082 E/AndroidRuntime(28585): Process: SimpleMapTest.SimpleMapTest, PID: 28585
02-10 10:50:49.082 E/AndroidRuntime(28585): java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;
Which seems to indication that the workaround detailed in step 9 is no longer a way to go, which fits with this article (first response) :
So kinda back at square one!
So whay should be the proper way to build an app targeting Android Pie that uses Google Maps in a fragment ? I'm lost here! Anyone can share an explanation that may be usefull to all ?
I understand that the line I added to the manifest is now mandatory, but still got the timeout exceed exception. It seems that the nuget package for google play services are overdue... Is this the case ?