Flutter 聯(lián)調(diào)問題

碰到什么錯(cuò)?

在我們進(jìn)行Flutter Native混合開發(fā)的過程中,vscode連真機(jī)/模擬器調(diào)試時(shí)總是會出現(xiàn)一些莫名其妙的錯(cuò)誤。

例如:

image

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

導(dǎo)致attach失敗,混合調(diào)試變的異常麻煩。

在網(wǎng)上查閱了各種資料,大致都是說網(wǎng)絡(luò)有問題,可能是本地的設(shè)置開了代理或者讓重啟設(shè)備、重啟應(yīng)用等。

但是嘗試了許多方法都不能解決問題。

在嘗試的過程中,通過終端之前去調(diào)用attach時(shí),發(fā)現(xiàn)了一些可能存在問題的地方。

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

通過看日志,我們發(fā)現(xiàn),該命令會讀取到一個(gè)設(shè)備list,同時(shí),能看到的是mDNS有一個(gè)報(bào)錯(cuò)提示。

mDNS是什么呢?mDNS

mDNS協(xié)議適用于局域網(wǎng)內(nèi)沒有DNS服務(wù)器時(shí)的域名解析,設(shè)備通過組播的方式交互DNS記錄來完成域名解析,約定的組播地址是:224.0.0.251,端口號是5353,mdns協(xié)議使用DNS協(xié)議一樣的數(shù)據(jù)包,由頭部和數(shù)據(jù)段兩部分

從上面的鏈接中可以了解到,flutter attach通過mDNS來查詢調(diào)試設(shè)備的地址和端口,之后通過http進(jìn)行通信。

發(fā)現(xiàn)問題!

那么有沒有可能是因?yàn)関scode有緩存信息,導(dǎo)致attach時(shí)的目標(biāo)對象不正確,導(dǎo)致無法attach呢。下面我們來驗(yàn)證一下。

直接使用vscode的attach功能,報(bào)錯(cuò)日志如下:

image

同時(shí),原生工程在Flutter的Engine啟動(dòng)后log中會輸出Observatory listening url

image

通過對比,我們發(fā)現(xiàn),vscode鏈接的地址跟原生打印的地址不是同一個(gè),那么問題可以確定是在出在這里了。

解決問題!

原生工程中打印的地址是否可以正確attach呢,我們可以通過命令 Flutter attach --debug-uri=""來驗(yàn)證一下

image

很快就attach成功,就此我們可以愉快的通過命令行進(jìn)行Hot reload和 Hot restart了

優(yōu)化方案

然而十分鐘后,發(fā)現(xiàn)了不對的地方,雖然我們可以attach成功后,可以使用hot reload了,但是因?yàn)槭峭ㄟ^終端執(zhí)行的,vscode的斷點(diǎn)功能不能使用了。

既然可以終端指定鏈接的目標(biāo),那么能不能通過vscode的配置文件實(shí)現(xiàn)呢?

我們看一下vscode的配置文件。

image
image

我們可以通過設(shè)置vmServiceUri來指定attach目標(biāo)。這樣我們可以完美的使用attach功能,再也不擔(dān)心attach報(bào)錯(cuò)了。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容