skip to Main Content

I am developing a Flutter app that works perfectly on iOS in both production and debug modes. However, I am encountering an issue with the Android version of the app. The problem is that HTTP calls to my REST API are blocked when the app is downloaded from the Play Store’s internal test plan. The same app works perfectly in debug mode and also when built directly on my phone.

Error Message:

ClientException with SocketException Connection failed (OS Error: Operation not permitted, errno = 1), address XXXXXXXX, port 5050,, uri http://XXXXXXXXXX:5050/Auth/SignUp

Steps Taken:

Added android:usesCleartextTraffic="true" to the AndroidManifest.xml:

I added the following line to the AndroidManifest.xml file to allow cleartext traffic:

<application
    android:usesCleartextTraffic="true">

Verified Network Configuration:

I checked my build.gradle files and ensured that there are no restrictions that could be blocking the HTTP traffic.

Tested Different Scenarios:

  • The app works correctly when built directly on the device in release mode.

  • The app works correctly when run in debug mode.

  • The issue only occurs when the app is installed from the Play Store internal test track.

It’s been 2 days that I’m struggeling with this and my app need to be in production very soon…

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application
        android:label="mobilitizzz_mobile"
        android:name="${applicationName}"
        android:usesCleartextTraffic="true"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:taskAffinity=""
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
       
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>

    <queries>
        <intent>
            <action android:name="android.intent.action.PROCESS_TEXT"/>
            <data android:mimeType="text/plain"/>
        </intent>
    </queries>
</manifest>

3

Answers


  1. Chosen as BEST ANSWER

    Well, after a lot of research and investigations I turns out that the issues was related to this line that was currently only in debug manifest:

    <uses-permission android:name="android.permission.INTERNET"/>
    

    I just added it in android/app/main/AndroidManifest.xml like so

    <manifest xmlns:android="http://schemas.android.com/apk/res/android">
        <uses-permission android:name="android.permission.INTERNET"/>
        <application
            android:label="mobilitizzz_mobile"
            android:name="${applicationName}"
            android:networkSecurityConfig="@xml/network_security_config"
            android:usesCleartextTraffic="true"
            android:icon="@mipmap/ic_launcher">
            <activity
                android:name=".MainActivity"
                android:exported="true"
                android:launchMode="singleTop"
                android:taskAffinity=""
                android:theme="@style/LaunchTheme"
                android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
                android:hardwareAccelerated="true"
                android:windowSoftInputMode="adjustResize">
    
                <meta-data
                  android:name="io.flutter.embedding.android.NormalTheme"
                  android:resource="@style/NormalTheme"
                  />
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
            </activity>
    
            <meta-data
                android:name="flutterEmbedding"
                android:value="2" />
                  <meta-data android:name="io.flutter.network-policy"
                 android:resource="@xml/network_security_config"/>
        </application>
    
        <queries>
            <intent>
                <action android:name="android.intent.action.PROCESS_TEXT"/>
                <data android:mimeType="text/plain"/>
            </intent>
        </queries>
    </manifest>
    
    

  2. Starting with Android API 28 and iOS 9, these platforms disable insecure HTTP connections by default.

    With this change Flutter also disables insecure connections on mobile platforms. Other platforms (desktop, web, etc.) are not affected.

    Try this:

    In your project’s android directory app/src/main/res/ create new directory called xml. And create network_security_config.xml in it.

    network_security_config.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <base-config cleartextTrafficPermitted="true">
            <trust-anchors>
                <!-- You can add trusted certificates here if needed -->
            </trust-anchors>
        </base-config>
    </network-security-config>
    

    Now add this in your AndroidManifest.xml file:

    <application
            android:networkSecurityConfig="@xml/network_security_config"
            android:usesCleartextTraffic="true">
    </application>
    

    I have recently face the same issue and this works for me. I hope it works for you.

    For more Information: Insecure HTTP connections are disabled by default on iOS and Android.

    Login or Signup to reply.
  3. Just Add One line in you AndroidManifest.xml

      <manifest xmlns:android="http://schemas.android.com/apk/res/android">
    
      <uses-permission android:name="android.permission.INTERNET" />
      <application
      ....
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search