在释放模式下连接到 TCP 连接时,Flutter 应用程序关闭且没有任何错误

问题描述 投票:0回答:1

我开发了一个 Flutter 应用程序,它连接到 TCP 服务器并使用基本的 dart:io 包发送数据:

_socket = await Socket.connect(MyConst.remoteHost, MyConst.remotePort);

在调试模式下运行完美,但在发布模式下应用程序崩溃。

我正在开发一个连接到 IoT 设备 Wi-Fi 的 IoT 应用程序,但该网络无法访问互联网。

Future<void> connect() async {
    try {
      _socket = await Socket.connect(MyConst.remoteHost, MyConst.remotePort);
      print('Connected to remote host');

      _socket!.listen(
            (data) {
          print('Received data: ${utf8.decode(data)}');
        },
        onError: (error) {
          print('Error: $error');
        },
        onDone: () {
          print('Connection closed');
          _reconnect();
        }
      );

      sendData({'cmd_id': '4016'});
      _startSendingDataPeriodically();

      await setDeviceTime();
      await setDeviceDate();
    } catch (e) {
      print('Failed to connect: $e');
      _reconnect();
    }
  }

这是我的连接功能。 它在调试模式下完美运行,但应用程序在发布模式下崩溃。 它也不会在控制台中引发任何错误,而是将其关闭。

flutter dart sockets tcp
1个回答
0
投票
  • 首先我们需要配置
    android/app/build.gradle
    文件
defaultConfig {
        // ... other default config ...
        ndk {
            abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
        }
        multiDexEnabled true
    }

    buildTypes {
        release {
            shrinkResources false
            minifyEnabled false
            // ... other release configurations ...
        }
    }

    packagingOptions {
        pickFirst 'lib/x86/libc++_shared.so'
        pickFirst 'lib/x86_64/libc++_shared.so'
        pickFirst 'lib/armeabi-v7a/libc++_shared.so'
        pickFirst 'lib/arm64-v8a/libc++_shared.so'
    }
    
  • ABIFilters
    指定您的应用程序支持哪些应用程序二进制接口(ABI)。它确保您的应用程序包含这些特定架构的本机库,这对于 VLC 跨不同设备类型工作至关重要。
  • shrinkResources false
    - 禁用资源收缩,这有时会删除本机库所需的资源。
  • minifyEnabled false
    - 禁用代码混淆和最小化,这可能会干扰本机库。
  • packagingOptions
    - 它解决了多个库包含同一文件时的冲突。它告诉 Gradle 如何处理重复的 .so 文件,这可能发生在 VLC 等本机库中。
  • 这允许您的应用将 DEX 文件(包含已编译的代码)拆分为多个文件,克服 65,536 个方法的限制。

我遵循了这些步骤,即使在发布模式下它也开始工作。

© www.soinside.com 2019 - 2024. All rights reserved.