No internet connection in Flutter release build

I’ve stumbled across the following issue several times: I’m finished with the new feature I built, have a clean code, a neat UI, a user-friendly UX and tests. Everything seems ready to be shipped. Now, just to be safe, let’s test it in release mode (app-release.apk) to see a better performance and how it behaves under more realistic conditions.
So I trigger the build, waiting for it to be finished which feels like forever when you’re used to the outstanding performance of Hot Reload.
Then, after a few minutes, the build is finished and I am faced with a Toast in the Snackbar telling you there’s no connection to the server.

There are basically two reasons for me that occur every now and then.

Reason 1: No appropriate permission (Android)

For Android build there are different AndroidManifest.xml: for debugging, for profiling and for releases.
The ones for debugging and profiling are meant to be used during development. They communicate via network with your IDE. Once you save the current project, the IDE transmits the source code. On the phone it gets injected into the running Dart VM.
In order to ensure for this to work from the beginning, the XMLs for debugging and profiling do have the internet permission by default.
This is, however, not the case for the XML for the release. Let’s say you are developing an app that only works an local stored data or not any stored data at all. For this reason your will not find the permission in the respective XML. We fix this like that:

<manifest xmlns:android=""
    <!-- is an that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <uses-permission android:name="android.permission.INTERNET" /> <!-- THIS LINE WAS ADDED -->

That should be it!

Reason 2: API addresses are set to test

If you manage your own backend and store the respective address / URI in a file (config file, Dart file, …) that is under version control, you might have uncommited changes where the address to your production backend is overridden with something pointing to a local machine or a test server. If they are (temporarily) not reachable, it will result in 404. Just revert all local changes before you build your app for release (testing).

If you like what you’ve read, feel free to support me:

🥗Buy me a salad

Leave a Comment