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

碰到什么錯旬昭?

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

例如:

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時塔粒,發(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)卒茬,該命令會讀取到一個設(shè)備list船老,同時,能看到的是mDNS有一個報錯提示圃酵。

mDNS是什么呢柳畔?mDNS

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

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

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

那么有沒有可能是因為vscode有緩存信息,導(dǎo)致attach時的目標對象不正確坎匿,導(dǎo)致無法attach呢盾剩。下面我們來驗證一下。

直接使用vscode的attach功能替蔬,報錯日志如下:

image

同時告私,原生工程在Flutter的Engine啟動后log中會輸出Observatory listening url

image

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

解決問題凶异!

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

image

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

優(yōu)化方案

然而十分鐘后剩彬,發(fā)現(xiàn)了不對的地方酷麦,雖然我們可以attach成功后,可以使用hot reload了喉恋,但是因為是通過終端執(zhí)行的沃饶,vscode的斷點功能不能使用了。

既然可以終端指定鏈接的目標轻黑,那么能不能通過vscode的配置文件實現(xiàn)呢糊肤?

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

image
image

我們可以通過設(shè)置vmServiceUri來指定attach目標氓鄙。這樣我們可以完美的使用attach功能馆揉,再也不擔心attach報錯了。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末抖拦,一起剝皮案震驚了整個濱河市把介,隨后出現(xiàn)的幾起案子勤讽,更是在濱河造成了極大的恐慌,老刑警劉巖拗踢,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件脚牍,死亡現(xiàn)場離奇詭異,居然都是意外死亡巢墅,警方通過查閱死者的電腦和手機诸狭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來君纫,“玉大人驯遇,你說我怎么就攤上這事⌒钏瑁” “怎么了叉庐?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長会喝。 經(jīng)常有香客問我陡叠,道長,這世上最難降的妖魔是什么肢执? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任枉阵,我火速辦了婚禮,結(jié)果婚禮上预茄,老公的妹妹穿的比我還像新娘兴溜。我一直安慰自己,他們只是感情好耻陕,可當我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布拙徽。 她就那樣靜靜地躺著,像睡著了一般诗宣。 火紅的嫁衣襯著肌膚如雪膘怕。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天梧田,我揣著相機與錄音淳蔼,去河邊找鬼侧蘸。 笑死裁眯,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的讳癌。 我是一名探鬼主播穿稳,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼晌坤!你這毒婦竟也來了逢艘?” 一聲冷哼從身側(cè)響起旦袋,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎它改,沒想到半個月后疤孕,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡央拖,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年祭阀,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鲜戒。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡专控,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出遏餐,到底是詐尸還是另有隱情伦腐,我是刑警寧澤,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布失都,位于F島的核電站柏蘑,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏嗅剖。R本人自食惡果不足惜辩越,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望信粮。 院中可真熱鬧黔攒,春花似錦、人聲如沸强缘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽旅掂。三九已至赏胚,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間商虐,已是汗流浹背觉阅。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留秘车,地道東北人典勇。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像叮趴,于是被迫代替她去往敵國和親割笙。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容