dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.89k stars 525 forks source link

Migration from Xamarin to Net8.0 : Cleartext communication not enabled #9063

Open vsfeedback opened 1 week ago

vsfeedback commented 1 week ago

This issue has been moved from a ticket on Developer Community.


[severity:I'm unable to use this version] I have to migrate a Xamarin/C# application to net8.0 because of Xamarin end of support. I am not a specialist of C#, .NET technologies, and I have just made a few modifications in this application and I have no colleagues to help me. I am using Visual Studio 17.9.7

When I call var response = await httpClient.GetAsync(resource); I have the error :

CLEARTEXT communication not enabled for client

I have done like other subject say : Here is the manifest file :

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.mci.mvision" android:versionCode="1" android:versionName="1">
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/MyTheme" android:usesCleartextTraffic="true" android:networkSecurityConfig="@xml/network_security_config"></application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk android:minSdkVersion="30" android:targetSdkVersion="34" />

Here is the main class :

[Activity(Label = "@string/app_name", MainLauncher = true, Icon = "@mipmap/ic_launcher", LaunchMode = LaunchMode.SingleInstance, ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize, WindowSoftInputMode = SoftInput.AdjustPan | SoftInput.StateHidden)]
public class MVision : AppCompatActivity

Here is the security config file :

<network-security-config>
    <base-config cleartextTrafficPermitted="true">
    <trust-anchors>
      <certificates src="system" />
      <certificates src="user" />
    </trust-anchors>
  </base-config>
</network-security-config>

To conclude, this project worked very well when it was in Xamarin, but the error occurs in . Net8.0 . After testing, this also works fine in MAUI. I conclude that the problem comes from . Net8.0 Can you fix this ?


Original Comments

Feedback Bot on 5/19/2024, 06:37 PM:

(private comment, text removed)


Original Solutions

(no solutions)

drasticactions commented 1 week ago

@jonathanpeppers Would you know where this issue should go?

jfversluis commented 1 week ago

Where in your project is the security config file?

Do you see any entries in your csproj file regarding this file?

dotnet-policy-service[bot] commented 1 week ago

Hi @vsfeedback. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

jpobst commented 6 days ago

the error occurs in .Net8.0. After testing, this also works fine in MAUI. I conclude that the problem comes from . Net8.0

Can you elaborate on this a little more? MAUI and .NET 8 are basically the same thing (assuming you mean net8.0-android for both).

Do you have two different projects for your two tests? I am guessing one of them is missing something the other has.

Can you provide a project that reproduces your issue?