Visual Studio 2019 で Android アプリのプロジェクトを作成し(Xamarin.Forms でも Xamarin.Android でも)、エミュレータで実行すると、次のエラーが出ました。
アプリケーションの開始エラー: ポートの転送に失敗しました。
デバッガーに接続できませんでした。詳細については Xamarin 診断出力を、
例外の詳細についてはログをご覧ください。
プロジェクトのビルドは成功しており、エミュレータの起動も完了しているにも関わらず、です。
このような場合、日本語のエラーメッセージで検索しても解決できる可能性は低いので、まずは英語のエラーメッセージを表示するために、Visual Studio の言語設定を英語にしましょう。
VS2019 を英語にするには、まずは英語の「言語パック」をインストールする必要があります。
- Visual Studio Installer を起動します
- 言語パック → 英語 にチェックを入れてインストールします
次に Visual Studio 2019 の設定で言語を変えます。
- Visual Studio 2019 のメニュー → ツール → オプション
- 環境 → 国際対応の設定 → 「English」を選択
これで同じ手順を辿ると、エラーメッセージは次のような英語になります。
Error Starting Application: Failed to forward ports.
Couldn't connect debugger. You can see more details in Xamarin Diagnostic output and the full exception on logs.
英語でググるとなんとか解決策に辿り着けるでしょう。
I've solved the problem! I deleted platform-tools in the SDK folder. And replaced it with an older version - platform-tools_r28.0.2-windows.zip. It all worked! Both from VS 2017 15.9.7 and after upgrading to 15.9.16. VS 2019 16.2.5 also successfully works with platform-tools_r28.0.2. Conclusion! - It is not the studio's fault, but the update of platform tools from android sdk to version 29 (29.0.1, 29.0.2 - does not work with VS 2017/2019, 28.0.2 - successfully works!)
Android SDK 内の platform-tools のバージョンが新しすぎると発生するから、古いバージョンにダウングレードすると回避できる、とのこと。
古いバージョンとはこちら → platform-tools 28.0.2
28.0.2 の platform-tools に置き換えて、Visual Studio 2019 を再起動、プロジェクトとエミュレータを再ビルド&実行すると、正常にエミュレータにアプリが転送できました。
ちなみにこのエラーが出たとき、私の platform-tools は 28.0.3 でした。
おそらく、Android SDK を Android Studio と共有していて、Android Studio 側で SDK をアップデートしたために、Visual Studio が対応していない 新しすぎる バージョンに更新されたためと思われます。
ディスク容量節約のために SDK を共有していますが、このようなリスクもある事を頭に入れておきましょう。