碰到什么错?
在我们进行Flutter Native混合开发的过程中,vscode连真机/模拟器调试时总是会出现一些莫名其妙的错误。
例如:
<pre data-language="bash" id="FR5I9" class="ne-codeblock language-bash" style="border: 1px solid #e8e8e8; border-radius: 2px; background: #f9f9f9; padding: 16px; font-size: 13px; color: #595959">➜ flutter attach
There are multiple observatory ports available.
Rerun this command with one of the following passed in as the appId:
flutter attach --app-id com.xxx.xxx (2)
flutter attach --app-id com.xxx.xxx</pre>
导致attach失败,混合调试变的异常麻烦。
在网上查阅了各种资料,大致都是说网络有问题,可能是本地的设置开了代理或者让重启设备、重启应用等。
但是尝试了许多方法都不能解决问题。
在尝试的过程中,通过终端之前去调用attach时,发现了一些可能存在问题的地方。
<pre data-language="basic" id="RXvZf" class="ne-codeblock language-basic" style="border: 1px solid #e8e8e8; border-radius: 2px; background: #f9f9f9; padding: 16px; font-size: 13px; color: #595959">➜ flutter_business git:(feature/S_2108) flutter attach -v
[ +138 ms] executing: [/Users/dangkun/flutter_sdk/flutter/] git -c log.showSignature=false log -n 1 --pretty=format:%H
[ +54 ms] Exit code 0 from: git -c log.showSignature=false log -n 1 --pretty=format:%H
[ ] 1aafb3a8b9b0c36241c5f5b34ee914770f015818
[ ] executing: [/Users/dangkun/flutter_sdk/flutter/] git tag --points-at HEAD
[ +40 ms] Exit code 0 from: git tag --points-at HEAD
[ ] 1.22.4
[ +9 ms] executing: [/Users/dangkun/flutter_sdk/flutter/] git rev-parse --abbrev-ref --symbolic @{u}
[ +19 ms] Exit code 0 from: git rev-parse --abbrev-ref --symbolic @{u}
[ ] origin/stable
[ ] executing: [/Users/dangkun/flutter_sdk/flutter/] git ls-remote --get-url origin
[ +18 ms] Exit code 0 from: git ls-remote --get-url origin
[ ] https://github.com/flutter/flutter.git
[ +61 ms] Unable to locate an Android SDK.
[ +8 ms] executing: [/Users/dangkun/flutter_sdk/flutter/] git rev-parse --abbrev-ref HEAD
[ +46 ms] Exit code 0 from: git rev-parse --abbrev-ref HEAD
[ ] stable
[ +8 ms] executing: sw_vers -productName
[ +19 ms] Exit code 0 from: sw_vers -productName
[ ] macOS
[ ] executing: sw_vers -productVersion
[ +17 ms] Exit code 0 from: sw_vers -productVersion
[ ] 11.0.1
[ ] executing: sw_vers -buildVersion
[ +16 ms] Exit code 0 from: sw_vers -buildVersion
[ ] 20B29
[ +1 ms] executing: sysctl hw.optional.arm64
[ +5 ms] Exit code 1 from: sysctl hw.optional.arm64
[ ] sysctl: unknown oid 'hw.optional.arm64'
[ +52 ms] Artifact Instance of 'AndroidMavenArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterWebSdk' is not required, skipping update.
[ +4 ms] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[ +10 ms] Artifact Instance of 'MaterialFonts' is not required, skipping update.
[ ] Artifact Instance of 'GradleWrapper' is not required, skipping update.
[ ] Artifact Instance of 'AndroidMavenArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterWebSdk' is not required, skipping update.
[ ] Artifact Instance of 'FlutterSdk' is not required, skipping update.
[ ] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[ ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FontSubsetArtifacts' is not required, skipping update.
[ +7 ms] executing: /usr/bin/xcode-select --print-path
[ +7 ms] Exit code 0 from: /usr/bin/xcode-select --print-path
[ ] /Applications/Xcode.app/Contents/Developer
[ +1 ms] executing: /usr/bin/xcodebuild -version
[+1389 ms] Exit code 0 from: /usr/bin/xcodebuild -version
[ ] Xcode 12.5
Build version 12E262
[ +61 ms] executing: sysctl hw.optional.arm64
[ +6 ms] Exit code 1 from: sysctl hw.optional.arm64
[ ] sysctl: unknown oid 'hw.optional.arm64'
[ +2 ms] executing: xcrun xcdevice list --timeout 2
[ +9 ms] xcrun simctl list --json devices
[ ] executing: xcrun simctl list --json devices
[ +194 ms] {
"devices" : {
"com.apple.CoreSimulator.SimRuntime.iOS-14-2" : [
{
"availabilityError" : "runtime profile not found",
"dataPath" :
"/Users/dangkun/Library/Developer/CoreSimulator/Devices/861973AE-1F07-4CD3-80E7-5F1F34BDDC7C/data",
"logPath" : "/Users/dangkun/Library/Logs/CoreSimulator/861973AE-1F07-4CD3-80E7-5F1F34BDDC7C",
"udid" : "861973AE-1F07-4CD3-80E7-5F1F34BDDC7C",
"isAvailable" : false,
"deviceTypeIdentifier" : "com.apple.CoreSimulator.SimDeviceType.iPhone-12",
"state" : "Shutdown",
"name" : "iPhone 12"
}
],
"com.apple.CoreSimulator.SimRuntime.tvOS-14-5" : [
],
"com.apple.CoreSimulator.SimRuntime.watchOS-7-4" : [
{
"dataPath" :
"\/Users\/dangkun\/Library\/Developer\/CoreSimulator\/Devices\/42E113B4-0210-4184-8B40-659A8E89D1F6\/data",
"logPath" : "\/Users\/dangkun\/Library\/Logs\/CoreSimulator\/42E113B4-0210-4184-8B40-659A8E89D1F6",
"udid" : "42E113B4-0210-4184-8B40-659A8E89D1F6",
"isAvailable" : true,
"deviceTypeIdentifier" : "com.apple.CoreSimulator.SimDeviceType.Apple-Watch-Series-5-44mm",
"state" : "Shutdown",
"name" : "Apple Watch Series 5 - 44mm"
}
],
"com.apple.CoreSimulator.SimRuntime.iOS-14-5" : [
{
"dataPath" :
"\/Users\/dangkun\/Library\/Developer\/CoreSimulator\/Devices\/78F214DF-0D5A-43E2-B918-02FD381E84E1\/data",
"logPath" : "\/Users\/dangkun\/Library\/Logs\/CoreSimulator\/78F214DF-0D5A-43E2-B918-02FD381E84E1",
"udid" : "78F214DF-0D5A-43E2-B918-02FD381E84E1",
"isAvailable" : true,
"deviceTypeIdentifier" : "com.apple.CoreSimulator.SimDeviceType.iPhone-7",
"state" : "Shutdown",
"name" : "iPhone 7"
},
{
"dataPath" :
"\/Users\/dangkun\/Library\/Developer\/CoreSimulator\/Devices\/9A072928-6F36-4345-B115-AD20956D481C\/data",
"logPath" : "\/Users\/dangkun\/Library\/Logs\/CoreSimulator\/9A072928-6F36-4345-B115-AD20956D481C",
"udid" : "9A072928-6F36-4345-B115-AD20956D481C",
"isAvailable" : true,
"deviceTypeIdentifier" : "com.apple.CoreSimulator.SimDeviceType.iPhone-XS-Max",
"state" : "Shutdown",
"name" : "iPhone Xs Max"
},
{
"dataPath" :
"\/Users\/dangkun\/Library\/Developer\/CoreSimulator\/Devices\/0622A1DE-DD97-458C-BF53-87D4B0522AE0\/data",
"logPath" : "\/Users\/dangkun\/Library\/Logs\/CoreSimulator\/0622A1DE-DD97-458C-BF53-87D4B0522AE0",
"udid" : "0622A1DE-DD97-458C-BF53-87D4B0522AE0",
"isAvailable" : true,
"deviceTypeIdentifier" : "com.apple.CoreSimulator.SimDeviceType.iPhone-11",
"state" : "Shutdown",
"name" : "iPhone 11"
},
{
"dataPath" :
"\/Users\/dangkun\/Library\/Developer\/CoreSimulator\/Devices\/C20591D4-F8CD-4EAB-8746-BD872021EF65\/data",
"logPath" : "\/Users\/dangkun\/Library\/Logs\/CoreSimulator\/C20591D4-F8CD-4EAB-8746-BD872021EF65",
"udid" : "C20591D4-F8CD-4EAB-8746-BD872021EF65",
"isAvailable" : true,
"deviceTypeIdentifier" : "com.apple.CoreSimulator.SimDeviceType.iPhone-12",
"state" : "Shutdown",
"name" : "iPhone 12"
},
{
"dataPath" :
"\/Users\/dangkun\/Library\/Developer\/CoreSimulator\/Devices\/95529135-D442-48C3-BCCA-AE7652876915\/data",
"logPath" : "\/Users\/dangkun\/Library\/Logs\/CoreSimulator\/95529135-D442-48C3-BCCA-AE7652876915",
"udid" : "95529135-D442-48C3-BCCA-AE7652876915",
"isAvailable" : true,
"deviceTypeIdentifier" : "com.apple.CoreSimulator.SimDeviceType.iPhone-12-Pro-Max",
"state" : "Shutdown",
"name" : "iPhone 12 Pro Max"
}
],
"com.apple.CoreSimulator.SimRuntime.iOS-10-3" : [
]
}
}
[+4251 ms] [
{
"simulator" : true,
"operatingSystemVersion" : "14.5 (18E182)",
"available" : true,
"platform" : "com.apple.platform.iphonesimulator",
"modelCode" : "iPhone13,2",
"identifier" : "C20591D4-F8CD-4EAB-8746-BD872021EF65",
"architecture" : "x86_64",
"modelUTI" : "com.apple.iphone-12-1",
"modelName" : "iPhone 12",
"name" : "iPhone 12"
},
{
"simulator" : true,
"operatingSystemVersion" : "14.5 (18E182)",
"available" : true,
"platform" : "com.apple.platform.iphonesimulator",
"modelCode" : "iPhone12,1",
"identifier" : "0622A1DE-DD97-458C-BF53-87D4B0522AE0",
"architecture" : "x86_64",
"modelUTI" : "com.apple.iphone-11-1",
"modelName" : "iPhone 11",
"name" : "iPhone 11"
},
{
"simulator" : false,
"operatingSystemVersion" : "14.7.1 (18G82)",
"interface" : "usb",
"available" : true,
"platform" : "com.apple.platform.iphoneos",
"modelCode" : "iPhone11,8",
"identifier" : "00008020-0003454C26B8003A",
"architecture" : "arm64e",
"modelUTI" : "com.apple.iphone-xr-2",
"modelName" : "iPhone XR",
"name" : "iPhone (2)"
},
{
"simulator" : true,
"operatingSystemVersion" : "14.5 (18E182)",
"available" : true,
"platform" : "com.apple.platform.iphonesimulator",
"modelCode" : "iPhone13,4",
"identifier" : "95529135-D442-48C3-BCCA-AE7652876915",
"architecture" : "x86_64",
"modelUTI" : "com.apple.iphone-12-pro-max-1",
"modelName" : "iPhone 12 Pro Max",
"name" : "iPhone 12 Pro Max"
},
{
"simulator" : true,
"operatingSystemVersion" : "14.5 (18E182)",
"available" : true,
"platform" : "com.apple.platform.iphonesimulator",
"modelCode" : "iPhone11,4",
"identifier" : "9A072928-6F36-4345-B115-AD20956D481C",
"architecture" : "x86_64",
"modelUTI" : "com.apple.iphone-xs-max-1",
"modelName" : "iPhone Xs Max",
"name" : "iPhone Xs Max"
},
{
"simulator" : true,
"operatingSystemVersion" : "7.4 (18T187)",
"available" : true,
"platform" : "com.apple.platform.watchsimulator",
"modelCode" : "Watch5,4",
"identifier" : "42E113B4-0210-4184-8B40-659A8E89D1F6",
"architecture" : "x86_64",
"modelUTI" : "com.apple.watch-series5-1",
"modelName" : "Apple Watch Series 5 - 44mm",
"name" : "Apple Watch Series 5 - 44mm"
},
{
"simulator" : true,
"operatingSystemVersion" : "14.5 (18E182)",
"available" : true,
"platform" : "com.apple.platform.iphonesimulator",
"modelCode" : "iPhone9,1",
"identifier" : "78F214DF-0D5A-43E2-B918-02FD381E84E1",
"architecture" : "x86_64",
"modelUTI" : "com.apple.iphone-7-1",
"modelName" : "iPhone 7",
"name" : "iPhone 7"
}
]
[ +8 ms] Error: 党坤的iPhone is not connected. Xcode will continue when 党坤的iPhone is connected. (code -13)
[ +50 ms] Checking for advertised Dart observatories...
[+5024 ms] No pointer records found.
[ +2 ms] mDNS query failed. Checking for an interface with a ipv4 link local address.
[ +4 ms] Found interface "en0":
[ ] Bound address: "172.27.101.213"
[ ] Found interface "en7":
[ ] Bound address: "169.254.248.197" link local
[ ] An interface with an ipv4 link local address was found.
[ +6 ms] Waiting for a connection from Flutter on iPhone (2)...</pre>
通过看日志,我们发现,该命令会读取到一个设备list,同时,能看到的是mDNS有一个报错提示。
mDNS是什么呢?mDNS
mDNS协议适用于局域网内没有DNS服务器时的域名解析,设备通过组播的方式交互DNS记录来完成域名解析,约定的组播地址是:224.0.0.251,端口号是5353,mdns协议使用DNS协议一样的数据包,由头部和数据段两部分
从上面的链接中可以了解到,flutter attach通过mDNS来查询调试设备的地址和端口,之后通过http进行通信。
发现问题!
那么有没有可能是因为vscode有缓存信息,导致attach时的目标对象不正确,导致无法attach呢。下面我们来验证一下。
直接使用vscode的attach功能,报错日志如下:
同时,原生工程在Flutter的Engine启动后log中会输出Observatory listening url
通过对比,我们发现,vscode链接的地址跟原生打印的地址不是同一个,那么问题可以确定是在出在这里了。
解决问题!
原生工程中打印的地址是否可以正确attach呢,我们可以通过命令 Flutter attach --debug-uri=""来验证一下
很快就attach成功,就此我们可以愉快的通过命令行进行Hot reload和 Hot restart了
优化方案
然而十分钟后,发现了不对的地方,虽然我们可以attach成功后,可以使用hot reload了,但是因为是通过终端执行的,vscode的断点功能不能使用了。
既然可以终端指定链接的目标,那么能不能通过vscode的配置文件实现呢?
我们看一下vscode的配置文件。
我们可以通过设置vmServiceUri来指定attach目标。这样我们可以完美的使用attach功能,再也不担心attach报错了。