Hey there 😊

If you like what you read, feel free to …

🥗Buy me a salad
Share this 💬

No internet connection in Flutter release build

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:

1<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2    package="com.flutterclutter">
3    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
4         calls FlutterMain.startInitialization(this); in its onCreate method.
5         In most cases you can leave this as-is, but you if you want to provide
6         additional functionality it is fine to subclass or reimplement
7         FlutterApplication and put your custom class here. -->
8    <uses-permission android:name="android.permission.INTERNET" /> <!-- THIS LINE WAS ADDED -->
9...

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).

Comments (1) ✍️

AntonioMew

When building APKs instead of app bundles, it is strongly recommended to build split APKs, as described in build an APK using the flag. What are the supported target architectures? When building your application in release mode, Flutter apps can be compiled for armeabi-v7a ARM 32-bit , arm64-v8a ARM 64-bit , and x86-64 x86 64-bit.
Reply to AntonioMew

Comment this 🤌

You are replying to 's commentRemove reference