XamarinアプリをAppium連携する方法について（Android編）

この記事は、[Xamarin Advent Calendar 2019](https://qiita.com/advent-calendar/2019/xamarin)の19日目の記事です。

先日、[Appiumの連携でiOSでハマった問題と解決方法をまとめた記事](https://qiita.com/gremito/items/b87bf035870b7a34d07b)をアップしました。

今回は、XamarinアプリをAppium連携する際にXamarin.Androidで絶賛ハマっている内容を紹介しようと思います。


## 環境

* MacBookPro Mojave 10.14
* Appium 1.15.1
* Visual Studio for Mac 8.3.7
    * Xamarin.Android 10.0.6.2
    * Xamarin.iOS 13.6.0.12

## Appium連携


### セットアップ

まず前提知識として、[ネイティブのAndroidアプリをAppiumで実行する方法](https://qiita.com/gremito/items/b87bf035870b7a34d07b#%E5%86%8D%E8%B5%B7%E5%8B%95)を理解している前提で話を進めていきます。

Xamarin.Androidの場合、`Droid/Properties/AndroidManifest.xml`には、最低限の設定しか行わず、各Activityクラスにプロパティを設定する必要があり、もし`Nameプロパティ`がActivityクラスに設定が無い場合は、ビルドされた`AndroidManifest.xml`の`android:name`タグにハッシュ付きのActivityクラス名が設定されます。

<img width="1340" alt="スクリーンショット 2019-12-22 16.44.50.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/82601/fbf22d8f-c60b-3e66-ccda-6a3e505b0eb6.png">
<img width="990" alt="スクリーンショット 2019-12-22 16.25.00.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/82601/7cb67d03-4b74-49e5-c56c-a9246abfb062.png">
ちなみにビルドされた`AndroidManifest.xml`は、`Droid/obj/Debug/android/AndroidManifest.xml`にあるので、一度`プロジェクト名.Droid`をビルドする必要があります。
<img width="1792" alt="スクリーンショット 2019-12-22 16.26.18.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/82601/8aa64ca6-8477-85a0-3e17-aea4980e308b.png">
<img width="1484" alt="スクリーンショット 2019-12-22 16.39.02.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/82601/67928cbf-0d3e-c653-0e80-f61959e7e302.png">
<img width="1792" alt="スクリーンショット 2019-12-22 16.40.00.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/82601/9007f8f6-1211-14b1-ae99-cd2209052b10.png">

詳細：[Android マニフェストの操作](https://docs.microsoft.com/ja-jp/xamarin/android/platform/android-manifest)

次に上記の情報を踏まえて[Appium側と連携する設定](http://appium.io/docs/en/writing-running-appium/caps/)を行なっていきます。設定の際に一度、実機/エミュレータにアプリをビルドしておく必要があります。

<img width="1192" alt="スクリーンショット 2019-12-22 16.28.07.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/82601/8df2f26e-9237-f76c-831a-5f15dedd2fa6.png">

`.apk`の場所ですが、`Droid/bin/Debug/`配下にビルドされています。

<img width="411" alt="スクリーンショット 2019-12-22 16.27.53.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/82601/b792751f-4b9b-3d15-7f57-97b0b8a0d66f.png">


## 問題

次の動画のようにアプリが異常動作し、Appiumは毎度アプリを入れ直すのでその後のアプリは壊れてしまします。

<blockquote class="twitter-tweet"><p lang="ja" dir="ltr">普通に<a href="https://t.co/AV3tOMaU1K">https://t.co/AV3tOMaU1K</a>のアプリとAppiumを連携させるとこうなる笑 <a href="https://t.co/lAN4ptqgum">pic.twitter.com/lAN4ptqgum</a></p>&mdash; gremito #ものラジ CSM® フリーランス (@grem_ito) <a href="https://twitter.com/grem_ito/status/1208661064428056576?ref_src=twsrc%5Etfw">December 22, 2019</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>


## 検証

次の検証を行なってみました。

1. Activityクラス名にNameプロパティを付けてハッシュを無くす。
2. 対象のAndroidバージョンをデバイスに合わせてポップアップを出さなくする。
3. MainActivityをスプラッシュ画面ではなく、メイン画面に変更する。

結果としては、全く改善できませんでした笑汗

1は動画と同じ状況で2は動画後半のスプラッシュ画面でフリーズしてしまう状態でAppiumもローディングから抜け出せない状態になってしまします。
3は、メイン画面を開けるもののAppiumがローディングから抜け出せない状況が続き、最終的にはタイムアウトのエラーで異常終了してしまう結果となります。

<blockquote class="twitter-tweet"><p lang="ja" dir="ltr">どうしたら正しく連携するのか...Xamarinの沼だwww <a href="https://t.co/050gSnqv6u">pic.twitter.com/050gSnqv6u</a></p>&mdash; gremito #ものラジ CSM® フリーランス (@grem_ito) <a href="https://twitter.com/grem_ito/status/1208666147505065984?ref_src=twsrc%5Etfw">December 22, 2019</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

Appiumのログを見てもタイムアウト以外のエラーは、`Got an error during apksigner execution ...`だけで、これが原因であれば署名をしていなかったネイティブのAndroidアプリでAppium連携できないはずだけども正常に連携できたので原因では無さそうで、、、打つ手無し状態です笑汗

```
[Appium] Welcome to Appium v1.15.1
[Appium] Non-default server args:
[Appium]   allowInsecure: {
[Appium]   }
[Appium]   denyInsecure: {
[Appium]   }
[Appium] Appium REST http interface listener started on 0.0.0.0:4723
[HTTP] --> GET /wd/hub/sessions
[HTTP] {}
[GENERIC] Calling AppiumDriver.getSessions() with args: []
[GENERIC] Responding to client with driver.getSessions() result: []
[HTTP] <-- GET /wd/hub/sessions 200 8 ms - 40
[HTTP] 
[HTTP] --> POST /wd/hub/session
[HTTP] {"desiredCapabilities":{"app":"/Users/gremito/Xamarin/AppiumTestForXamarin/Droid/bin/Debug/net.gremito.app.appiumtestforxamarin.apk","appActivity":"net.gremito.app.appiumtestforxamarin.MainActivity","appPackage":"net.gremito.app.appiumtestforxamarin","automationName":"UiAutomator2","deviceName":"Android Emulator","platformName":"Android","newCommandTimeout":0,"connectHardwareKeyboard":true}}
[MJSONWP] Calling AppiumDriver.createSession() with args: [{"app":"/Users/gremito/Xamarin/AppiumTestForXamarin/Droid/bin/Debug/net.gremito.app.appiumtestforxamarin.apk","appActivity":"net.gremito.app.appiumtestforxamarin.MainActivity","appPackage":"net.gremito.app.appiumtestforxamarin","automationName":"UiAutomator2","deviceName":"Android Emulator","platformName":"Android","newCommandTimeout":0,"connectHardwareKeyboard":true},null,null]
[BaseDriver] Event 'newSessionRequested' logged at 1577003532774 (17:32:12 GMT+0900 (JST))
[Appium] Appium v1.15.1 creating new AndroidUiautomator2Driver (v1.37.2) session
[BaseDriver] Creating session with MJSONWP desired capabilities: {
[BaseDriver]   "app": "/Users/gremito/Xamarin/AppiumTestForXamarin/Droid/bin/Debug/net.gremito.app.appiumtestforxamarin.apk",
[BaseDriver]   "appActivity": "net.gremito.app.appiumtestforxamarin.MainActivity",
[BaseDriver]   "appPackage": "net.gremito.app.appiumtestforxamarin",
[BaseDriver]   "automationName": "UiAutomator2",
[BaseDriver]   "deviceName": "Android Emulator",
[BaseDriver]   "platformName": "Android",
[BaseDriver]   "newCommandTimeout": 0,
[BaseDriver]   "connectHardwareKeyboard": true
[BaseDriver] }
[BaseDriver] The following capabilities were provided, but are not recognized by Appium:
[BaseDriver]   connectHardwareKeyboard
[BaseDriver] Session created with session id: 9a87fb97-0ded-404e-89c1-24158c5438b4
[BaseDriver] Using local app '/Users/gremito/Xamarin/AppiumTestForXamarin/Droid/bin/Debug/net.gremito.app.appiumtestforxamarin.apk'
[UiAutomator2] Checking whether app is actually present
[ADB] Found 6 'build-tools' folders under '/Users/gremito/Library/Android/sdk' (newest first):
[ADB]     /Users/gremito/Library/Android/sdk/build-tools/29.0.2
[ADB]     /Users/gremito/Library/Android/sdk/build-tools/29.0.1
[ADB]     /Users/gremito/Library/Android/sdk/build-tools/28.0.3
[ADB]     /Users/gremito/Library/Android/sdk/build-tools/28.0.2
[ADB]     /Users/gremito/Library/Android/sdk/build-tools/28.0.0
[ADB]     /Users/gremito/Library/Android/sdk/build-tools/27.0.3
[ADB] Using 'adb' from '/Users/gremito/Library/Android/sdk/platform-tools/adb'
[AndroidDriver] Retrieving device list
[ADB] Trying to find a connected android device
[ADB] Getting connected devices...
[ADB] Connected devices: [{"udid":"emulator-5554","state":"device"}]
[AndroidDriver] Using device: emulator-5554
[ADB] Using 'adb' from '/Users/gremito/Library/Android/sdk/platform-tools/adb'
[ADB] Setting device id to emulator-5554
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell getprop ro.build.version.sdk'
[ADB] Current device property 'ro.build.version.sdk': 28
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell getprop ro.build.version.release'
[ADB] Current device property 'ro.build.version.release': 9
[ADB] Device API level: 28
[UiAutomator2] Relaxing hidden api policy
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell settings put global hidden_api_policy_pre_p_apps 1'
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell settings put global hidden_api_policy_p_apps 1'
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell settings put global hidden_api_policy 1'
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 wait-for-device'
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell echo ping'
[AndroidDriver] Pushing settings apk to device...
[ADB] Getting install status for io.appium.settings
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell dumpsys package io.appium.settings'
[ADB] 'io.appium.settings' is installed
[ADB] Getting package info for 'io.appium.settings'
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell dumpsys package io.appium.settings'
[ADB] Using 'apkanalyzer' from '/Users/gremito/Library/Android/sdk/tools/bin/apkanalyzer'
[ADB] Starting '/Users/gremito/Library/Android/sdk/tools/bin/apkanalyzer' with args ["manifest","print","/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/io.appium.settings/apks/settings_apk-debug.apk"]
[ADB] The version name of the installed 'io.appium.settings' is greater or equal to the application version name ('2.14.2' >= '2.14.2')
[ADB] There is no need to install/upgrade '/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/io.appium.settings/apks/settings_apk-debug.apk'
[ADB] Getting IDs of all 'io.appium.settings' processes
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell 'pgrep --help; echo $?''
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell pgrep -f io\\.appium\\.settings'
[AndroidDriver] io.appium.settings is already running. There is no need to reset its permissions.
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell appops set io.appium.settings android\:mock_location allow'
[Logcat] Starting logcat capture
[ADB] Getting install status for io.appium.uiautomator2.server
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell dumpsys package io.appium.uiautomator2.server'
[ADB] 'io.appium.uiautomator2.server' is installed
[ADB] Getting package info for 'io.appium.uiautomator2.server'
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell dumpsys package io.appium.uiautomator2.server'
[ADB] Starting '/Users/gremito/Library/Android/sdk/tools/bin/apkanalyzer' with args ["manifest","print","/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-v4.3.0.apk"]
[ADB] The version name of the installed 'io.appium.uiautomator2.server' is greater or equal to the application version name ('4.3.0' >= '4.3.0')
[UiAutomator2] io.appium.uiautomator2.server installation state: sameVersionInstalled
[ADB] Checking app cert for /Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-v4.3.0.apk
[ADB] Using 'apksigner' from '/Users/gremito/Library/Android/sdk/build-tools/29.0.2/apksigner'
[ADB] Starting '/Users/gremito/Library/Android/sdk/build-tools/29.0.2/apksigner' with args '["verify","--print-certs","/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-v4.3.0.apk"]'
[ADB] apksigner stdout: Signer #1 certificate DN: EMAILADDRESS=android@android.com, CN=Android, OU=Android, O=Android, L=Mountain View, ST=California, C=US
[ADB] Signer #1 certificate SHA-256 digest: a40da80a59d170caa950cf15c18c454d47a39b26989d8b640ecd745ba71bf5dc
[ADB] Signer #1 certificate SHA-1 digest: 61ed377e85d386a8dfee6b864bd85b0bfaa5af81
[ADB] Signer #1 certificate MD5 digest: e89b158e4bcf988ebd09eb83f5378e87
[ADB] 
[ADB] '/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-v4.3.0.apk' is already signed.
[ADB] Getting install status for io.appium.uiautomator2.server.test
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell dumpsys package io.appium.uiautomator2.server.test'
[ADB] 'io.appium.uiautomator2.server.test' is installed
[ADB] Checking app cert for /Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-debug-androidTest.apk
[ADB] Starting '/Users/gremito/Library/Android/sdk/build-tools/29.0.2/apksigner' with args '["verify","--print-certs","/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-debug-androidTest.apk"]'
[ADB] apksigner stdout: Signer #1 certificate DN: EMAILADDRESS=android@android.com, CN=Android, OU=Android, O=Android, L=Mountain View, ST=California, C=US
[ADB] Signer #1 certificate SHA-256 digest: a40da80a59d170caa950cf15c18c454d47a39b26989d8b640ecd745ba71bf5dc
[ADB] Signer #1 certificate SHA-1 digest: 61ed377e85d386a8dfee6b864bd85b0bfaa5af81
[ADB] Signer #1 certificate MD5 digest: e89b158e4bcf988ebd09eb83f5378e87
[ADB] 
[ADB] '/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-debug-androidTest.apk' is already signed.
[UiAutomator2] Server packages are not going to be (re)installed
[UiAutomator2] Waiting up to 30000ms for services to be available
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell pm list instrumentation'
[UiAutomator2] Instrumentation target 'io.appium.uiautomator2.server.test/androidx.test.runner.AndroidJUnitRunner' is available
[UiAutomator2] Forwarding UiAutomator2 Server port 6790 to 8200
[ADB] Forwarding system: 8200 to device: 6790
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 forward tcp\:8200 tcp\:6790'
[ADB] Checking app cert for /Users/gremito/Xamarin/AppiumTestForXamarin/Droid/bin/Debug/net.gremito.app.appiumtestforxamarin.apk
[ADB] Starting '/Users/gremito/Library/Android/sdk/build-tools/29.0.2/apksigner' with args '["verify","--print-certs","/Users/gremito/Xamarin/AppiumTestForXamarin/Droid/bin/Debug/net.gremito.app.appiumtestforxamarin.apk"]'
[ADB] Got an error during apksigner execution: Command '/Users/gremito/Library/Android/sdk/build-tools/29.0.2/apksigner verify --print-certs /Users/gremito/Xamarin/AppiumTestForXamarin/Droid/bin/Debug/net.gremito.app.appiumtestforxamarin.apk' exited with code 1
[ADB] apksigner stderr: DOES NOT VERIFY
[ADB] ERROR: Missing META-INF/MANIFEST.MF
[ADB] 
[ADB] '/Users/gremito/Xamarin/AppiumTestForXamarin/Droid/bin/Debug/net.gremito.app.appiumtestforxamarin.apk' is not signed with debug cert
[ADB] Using 'zipalign' from '/Users/gremito/Library/Android/sdk/build-tools/29.0.2/zipalign'
[ADB] '/Users/gremito/Xamarin/AppiumTestForXamarin/Droid/bin/Debug/net.gremito.app.appiumtestforxamarin.apk' is not zip-aligned. Aligning
[ADB] Signing '/Users/gremito/Xamarin/AppiumTestForXamarin/Droid/bin/Debug/net.gremito.app.appiumtestforxamarin.apk' with default cert
[ADB] Starting '/Users/gremito/Library/Android/sdk/build-tools/29.0.2/apksigner' with args '["sign","--key","/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-adb/keys/testkey.pk8","--cert","/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-adb/keys/testkey.x509.pem","/Users/gremito/Xamarin/AppiumTestForXamarin/Droid/bin/Debug/net.gremito.app.appiumtestforxamarin.apk"]'
[ADB] Getting install status for net.gremito.app.appiumtestforxamarin
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell dumpsys package net.gremito.app.appiumtestforxamarin'
[ADB] 'net.gremito.app.appiumtestforxamarin' is installed
[ADB] Getting package info for 'net.gremito.app.appiumtestforxamarin'
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell dumpsys package net.gremito.app.appiumtestforxamarin'
[ADB] Starting '/Users/gremito/Library/Android/sdk/tools/bin/apkanalyzer' with args ["manifest","print","/Users/gremito/Xamarin/AppiumTestForXamarin/Droid/bin/Debug/net.gremito.app.appiumtestforxamarin.apk"]
[ADB] The version name of the installed 'net.gremito.app.appiumtestforxamarin' is greater or equal to the application version name ('1.0.0' >= '1.0.0')
[ADB] There is no need to install/upgrade '/Users/gremito/Xamarin/AppiumTestForXamarin/Droid/bin/Debug/net.gremito.app.appiumtestforxamarin.apk'
[AndroidDriver] Performing fast reset on 'net.gremito.app.appiumtestforxamarin'
[ADB] Getting install status for net.gremito.app.appiumtestforxamarin
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell dumpsys package net.gremito.app.appiumtestforxamarin'
[ADB] 'net.gremito.app.appiumtestforxamarin' is installed
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell am force-stop net.gremito.app.appiumtestforxamarin'
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell pm clear net.gremito.app.appiumtestforxamarin'
[AndroidDriver] Performed fast reset on the installed 'net.gremito.app.appiumtestforxamarin' application (stop and clear)
[UiAutomator2] Performing shallow cleanup of automation leftovers
[UiAutomator2] No obsolete sessions have been detected (Error: socket hang up)
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell am force-stop io.appium.uiautomator2.server.test'
[UiAutomator2] Starting UIAutomator2 server 4.3.0
[UiAutomator2] Using UIAutomator2 server from '/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-v4.3.0.apk' and test from '/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-debug-androidTest.apk'
[UiAutomator2] Waiting up to 30000ms for UiAutomator2 to be online...
[ADB] Creating ADB subprocess with args: ["-P",5037,"-s","emulator-5554","shell","am","instrument","-w","io.appium.uiautomator2.server.test/androidx.test.runner.AndroidJUnitRunner"]
[WD Proxy] Matched '/status' to command name 'getStatus'
[WD Proxy] Proxying [GET /status] to [GET http://localhost:8200/wd/hub/status] with no body
[WD Proxy] Got an unexpected response with status undefined: {"code":"ECONNRESET"}
[Instrumentation] io.appium.uiautomator2.server.test.AppiumUiAutomator2Server:
[WD Proxy] Matched '/status' to command name 'getStatus'
[WD Proxy] Proxying [GET /status] to [GET http://localhost:8200/wd/hub/status] with no body
[WD Proxy] Got an unexpected response with status undefined: {"code":"ECONNRESET"}
[WD Proxy] Matched '/status' to command name 'getStatus'
[WD Proxy] Proxying [GET /status] to [GET http://localhost:8200/wd/hub/status] with no body
[WD Proxy] Got response with status 200: {"sessionId":"None","value":{"ready":true,"message":"UiAutomator2 Server is ready to accept commands"}}
[UiAutomator2] The initialization of the instrumentation process took 3277ms
[WD Proxy] Matched '/session' to command name 'createSession'
[WD Proxy] Proxying [POST /session] to [POST http://localhost:8200/wd/hub/session] with body: {"capabilities":{"firstMatch":[{"platform":"LINUX","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"app":"/Users/gremito/Xamarin/AppiumTestForXamarin/Droid/bin/Debug/net.gremito.app.appiumtestforxamarin.apk","appActivity":"net.gremito.app.appiumtestforxamarin.MainActivity","appPackage":"net.gremito.app.appiumtestforxamarin","automationName":"UiAutomator2","deviceName":"Android Emulator","platformName":"Android","newCommandTimeout":0,"connectHardwareKeyboard":true},"app":"/Users/gremito/Xamarin/AppiumTestForXamarin/Droid/bin/Debug/net.gremito.app.appiumtestforxamarin.apk","appActivity":"net.gremito.app.appiumtestforxamarin.MainActivity","appPackage":"net.gremito.app.appiumtestforxamarin","automationName":"UiAutomator2","deviceName":"emulator-5554","platformName":"Android","newCommandTimeout":0,"connectHardwareKeyboard":true,"deviceUDID":"emulator-5554"}],"alwaysMatch":{}}}
[WD Proxy] Got response with status 200: {"sessionId":"6572217d-4279-4704-9475-7e2c95ba3153","value":{"sessionId":"6572217d-4279-4704-9475-7e2c95ba3153","capabilities":{"firstMatch":[{"platform":"LINUX","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"app":"/Users/gremito/Xamarin/AppiumTestForXamarin/Droid/bin/Debug/net.gremito.app.appiumtestforxamarin.apk","appActivity":"net.gremito.app.appiumtestforxamarin.MainActivity","appPackage":"net.gremito.app.appiumtestforxamarin","automationName":"UiAutomator2","deviceName":"Android Emulator","platformName":"Android","newCommandTimeout":0,"connectHardwareKeyboard":true},"app":"/Users/gremito/Xamarin/AppiumTestForXamarin/Droid/bin/Debug/net.gremito.app.appiumtestforxamarin.apk","appActivity":"net.gremito.app.appiumtestforxamarin.MainActivity","appPackage":"net.gremito.app.appiumtestforxamarin","automationName":"UiAutomator2","deviceName":"emulator-5554","platformName":...
[WD Proxy] Determined the downstream protocol as 'W3C'
[WD Proxy] Proxying [GET /appium/device/info] to [GET http://localhost:8200/wd/hub/session/6572217d-4279-4704-9475-7e2c95ba3153/appium/device/info] with no body
[WD Proxy] Got response with status 200: {"sessionId":"6572217d-4279-4704-9475-7e2c95ba3153","value":{"androidId":"f5a8d6d0364faa08","manufacturer":"Google","model":"Android SDK built for x86","brand":"google","apiVersion":"28","platformVersion":"9","carrierName":"Android","realDisplaySize":"1080x1920","displayDensity":420,"networks":[{"type":1,"typeName":"WIFI","subtype":0,"subtypeName":"","isConnected":true,"detailedState":"CONNECTED","state":"CONNECTED","extraInfo":null,"isAvailable":true,"isFailover":false,"isRoaming":false,"capabilities":{"transportTypes":"NET_CAPABILITY_SUPL","networkCapabilities":"","linkUpstreamBandwidthKbps":1048576,"linkDownBandwidthKbps":1048576,"signalStrength":-50,"networkSpecifier":null,"SSID":null}},{"type":0,"typeName":"MOBILE","subtype":13,"subtypeName":"LTE","isConnected":true,"detailedState":"CONNECTED","state":"CONNECTED","extraInfo":"epc.tmobile.com","isAvailable":true,"isFailover":false,"isRoaming":false,"capabilities":{"transportTypes":"NET_CAPABILITY_MMS","networkCapabilities":"","linkUpstreamBandwidthKbps...
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell dumpsys window'
[AndroidDriver] Screen already unlocked, doing nothing
[UiAutomator2] Starting 'net.gremito.app.appiumtestforxamarin/net.gremito.app.appiumtestforxamarin.MainActivity and waiting for 'net.gremito.app.appiumtestforxamarin/net.gremito.app.appiumtestforxamarin.MainActivity'
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell am start -W -n net.gremito.app.appiumtestforxamarin/net.gremito.app.appiumtestforxamarin.MainActivity -S'
[UiAutomator2] Deleting UiAutomator2 session
[UiAutomator2] Deleting UiAutomator2 server session
[WD Proxy] Matched '/' to command name 'deleteSession'
[WD Proxy] Proxying [DELETE /] to [DELETE http://localhost:8200/wd/hub/session/6572217d-4279-4704-9475-7e2c95ba3153] with no body
[WD Proxy] Got response with status 200: {"sessionId":"6572217d-4279-4704-9475-7e2c95ba3153","value":null}
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell am force-stop net.gremito.app.appiumtestforxamarin'
[Instrumentation] .
[Logcat] Stopping logcat capture
[ADB] Removing forwarded port socket connection: 8200 
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 forward --remove tcp\:8200'
[UiAutomator2] Restoring hidden api policy to the device default configuration
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell settings delete global hidden_api_policy_pre_p_apps'
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell settings delete global hidden_api_policy_p_apps'
[ADB] Running '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell settings delete global hidden_api_policy'
[BaseDriver] Event 'newSessionStarted' logged at 1577003572240 (17:32:52 GMT+0900 (JST))
[MJSONWP] Encountered internal error running command: Error: Cannot start the 'net.gremito.app.appiumtestforxamarin' application. Visit https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/android/activity-startup.md for troubleshooting. Original error: Error executing adbExec. Original error: 'Command '/Users/gremito/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell am start -W -n net.gremito.app.appiumtestforxamarin/net.gremito.app.appiumtestforxamarin.MainActivity -S' timed out after 20000ms'. Try to increase the 20000ms adb execution timeout represented by 'adbExecTimeout' capability
[MJSONWP]     at ADB.startApp (/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-adb/lib/tools/apk-utils.js:153:11)
[HTTP] <-- POST /wd/hub/session 500 39492 ms - 713
[HTTP] 
[HTTP] --> DELETE /wd/hub/session
[HTTP] {}
[HTTP] No route found. Setting content type to 'text/plain'
[HTTP] <-- DELETE /wd/hub/session 404 7 ms - 57
[HTTP] 
```


## さいごに

~~引き続き調査を進めているので、解決次第アップデートします🙏~~

[追記](https://qiita.com/gremito/items/8d321a0cd4823fc18d95#%E8%BF%BD%E8%A8%98)に解決方法をまとめました🙌


### 小ネタ①：Xcodeが古くXamarin.iOSが最新の場合

現在扱っているMBPは、Xcode10.1でiOSターゲットが12.1であり、その状態で最新のXamarin.iOSを扱うとiOSアプリをビルドすることができません。
次のようにターゲットを変えられるのですが、Xamarin.iOSが勝手にバージョン制御しているようでXcodeを最新にする必要があるようです。

<img width="1465" alt="スクリーンショット 2019-12-22 15.51.02.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/82601/d1cd2db0-de7c-e53b-4f5f-bd7b9195a81b.png">
<img width="250" alt="スクリーンショット 2019-12-22 16.06.38.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/82601/d9085cfa-10b5-7106-ecce-a3cc320a1a64.png"><img width="650" alt="スクリーンショット 2019-12-22 16.06.15.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/82601/e78829e9-c320-2aa0-7127-5e48d7f1a6d5.png">
<img width="373" alt="スクリーンショット 2019-12-22 16.10.30.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/82601/f1cd163f-5ea5-1b8d-8e41-35056205b25a.png">


### 小ネタ②：Xamarin.iOSは正常にAppium連携できる

別環境で試してみたところXamarin.iOSの方は正常にAppium連携できました笑

<img width="1877" alt="スクリーンショット 2019-12-23 11.36.33.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/82601/ea4487eb-22e2-a56a-d46d-f13b498a1f54.png">


## 追記

### Appium Desktop App ver 1.9.1

Appiumのissueを確認したところ、似た環境で同じ現象が起きている方がAppiumのバージョン1.9で動作したことを確認したと言われていたため、[Appiumのバージョン1.9.1を再インストールして試したところXamarinアプリとAppium連携できた](https://github.com/appium/appium-desktop/issues/849#issuecomment-579188831)ことを確認できましたw


### Appium Desktop App Newest Version

さらに[最新のAppiumアプリでXamarinアプリのAppium連携](https://github.com/appium/appium-desktop/issues/849#issuecomment-581789646)方法が分かりました！