20251226

0218睡到0930睡到1115。昨天flutter写的Android App无法使用网络可以总结成debug版+VPN能正常访问,debug版(无VPN)会因为被墙而显示空,release版则是在一直转圈加载,原因究竟是什么呢。

flutter build apk --release打了个release包试,也在一直转圈,看来果真是release的问题。为什么呢为什么呢。Flutter release 会开启 AOT 编译 和 Tree Shaking,如果你网络请求相关的类/方法被 Tree Shaking 去掉(比如反射、插件注册失败),也可能导致网络调用不生效。

编译有提示Font asset "MaterialIcons-Regular.otf" was tree-shaken, reducing it from 1645184 to 1772 bytes (99.9% reduction). Tree-shaking can be disabled by providing the --no-tree-shake-icons flag when building your app.但这只是字体,应该和网络没关系,而且用的是http库,不该会有问题的。

创建自定义的 HttpClient,尝试绕过 HTTPS 证书验证。打过一次包后第二次竟然只要41.9s,但很遗憾,仍然不行。

使用android:networkSecurityConfig="@xml/network_security_config"让它允许所有 HTTPS,结果也不行。

看着前面的标红去把ndkVersion = flutter.ndkVersion改成ndkVersion = "27.0.12077973"后也不行。

使用isMinifyEnabled = false isShrinkResources = false禁用混淆后也不行。

不过意识到测试时可以使用flutter run --release而不是flutter build apk --release

而且这样还有日志看,然后问题就出来了

1
2
I/flutter (23971): [IMPORTANT:flutter/shell/platform/android/android_context_vk_impeller.cc(61)] Using the Impeller rendering backend (Vulkan).
I/flutter (23971): NETWORK ERROR: ClientException with SocketException: Failed host lookup: 'find.5ch.net' (OS Error: No address associated with hostname, errno = 7), uri=https://find.5ch.net/search?q=%E3%82%AF%E3%83%AB%E3%82%B9%E3%82%BF
Licensed under CC BY-NC-SA 4.0
使用 Hugo 构建
主题 StackJimmy 设计