Null reference exception on startup in Xamarin app with Android network-security-config

437 Views Asked by At

I'm trying to add a network security config file to an Android app built with Xamarin tools. (Xamarin forms version 2.5.1.527436).

I followed the steps in this article: https://devblogs.microsoft.com/xamarin/cleartext-http-android-network-security/

My network_security.config.xml file just contain two TLS overrides for sites accessed by an in-app browser, and where https/tls can't be enabled server-side at this point:

<?xml version="1.0" encoding="utf-8" ?>
<network-security-config>
  <domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">xxx.somedomain.com</domain>
    <domain includeSubdomains="true">yyy.somedomain.com</domain>
  </domain-config>
</network-security-config>

In the AndroidManifest.xml file I just added android:networkSecurityConfig="@xml/network_security_config":

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.somecompany.someapp" android:versionName="1.2.3 (October 2019)" android:versionCode="3" android:installLocation="auto">
  <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="28" />
...
  <application android:largeHeap="true"
    android:label="Some App Name"
    android:icon="@drawable/icon"
    android:theme="@style/AppTheme"
    android:networkSecurityConfig="@xml/network_security_config">
...
  </application>
</manifest>

However, on startup, the call to Xamarin.Forms.Platform.Android.FormsAppCompatActivity.LoadApplication throws a null reference exception without any further details.

[0:] System.NullReferenceException: Object reference not set to an instance of an object.
at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.InternalSetPage (Xamarin.Forms.Page page) [0x0005e] in D:\agent_work\1\s\Xamarin.Forms.Platform.Android\AppCompat\FormsAppCompatActivity.cs:315
at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.SetMainPage () [0x00000] in D:\agent_work\1\s\Xamarin.Forms.Platform.Android\AppCompat\FormsAppCompatActivity.cs:343
at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.LoadApplication (Xamarin.Forms.Application application) [0x0025c] in D:\agent_work\1\s\Xamarin.Forms.Platform.Android\AppCompat\FormsAppCompatActivity.cs:139
at SomeApp.MainActivity.OnCreate (Android.OS.Bundle savedInstanceState) [0x001db] in D:\work\xxx\MainActivity.cs:173

I'm not sure if I am missing something, or if there is some Xamarin version dependency involved when adding a network security config file..? Any ideas?

1

There are 1 best solutions below

0
On BEST ANSWER

It seems that the version of xamarin.forms in your app is still 2.5.x ,and the newest version is 4.2.x . So firstly I suggest you can update it .Otherwise ,there will maybe have some unknown issue because of compatibility .