androidflutterinstallationflutter-build

Flutter UDP-Function in release mode apk doesnt work, but works in debug mode


The App i created works fine on emulator or when debugging it via usb cabel in debug mode.

To share the apk with my friends i run flutter build apk

i upload the apk in build/app/outputs/apk/release/app-release.apk to google drive.

then i download the apk on my android and install it.

I send data via UDP to some device in my local network.

How ever the apk installed manually doesnt work. It will not send the data.


class UdpService {
  RawDatagramSocket? sender;

  static Future<UdpService> create() async {
    var udpService = UdpService();
    print("Creating UDP Service");
    udpService.sender =
        await RawDatagramSocket.bind(InternetAddress.anyIPv4, 31103)
            .catchError(() => print("============= Error"));
    print("Created UDP Service");

    return udpService;
  }

  Future<void> sendIntegerViaUDP(
      String value, String port, String ipadr) async {
    if (sender == null) {
      print("Sender is null");
      return;
    }

    var data = int.parse(value);
    var dataLength =
        sender!.send([data], InternetAddress(ipadr), int.parse(port));

    print("$dataLength bytes sent via port $port to host $ipadr");
  }

  void dispose() {
    sender?.close();
  }
}



So what i think, the code works only on debug mode apk.

What is the reason for that and can i make it work?


Solution

  • make sure that you add the internet permission on <your_project>/android/app/src/main\AndroidManifest.xml

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