2
0

はじめに

ローカル環境を汚したくないので、Azure VM上でAndroid Studioのコーディング環境を整えられないかなぁと思いました。
デバッグの時に普段Androidエミュレータを使用しており、Azure VM上でエミュレータを起動するために少し工夫が必要だったので、ポイントをここで展開します。

Azure VMのサイズ

以下のようにエミュレータ技術が想定しているプロセッサの種類でないと動作しないので注意が必要です。今回HAXMを使用するので、プロセッサがIntelのVMを選択します。
Intel:HAXM(Intel Hardware Accelerated Execution Manager)
AMD:SVM

参考

また、今回検証した限りだと8コア、メモリ32GBくらいのサイズでないと以下のようなメッセージが出て、アプリのビルドとAndroidエミュレータへのインストール完了後にアプリが落ちました。

エラーメッセージ
Force removing ActivityRecord{a7aa8c9 u0 com.example.helloworldapp/.MainActivity t20}: app died, no saved state
2023-03-26 10:34:25.073   467-486   WindowManager           system_process                       W  Failed looking up window
java.lang.IllegalArgumentException: Requested window android.view.ViewRootImpl$W@c68d9e8 does not exist
at com.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:8733)
at com.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:8724)
at com.android.server.wm.WindowManagerService.removeWindow(WindowManagerService.java:2697)
at com.android.server.wm.Session.remove(Session.java:187)
at android.view.ViewRootImpl.dispatchDetachedFromWindow(ViewRootImpl.java:3099)
at android.view.ViewRootImpl.doDie(ViewRootImpl.java:5606)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3409)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.os.HandlerThread.run(HandlerThread.java:61)
at com.android.server.ServiceThread.run(ServiceThread.java:46)
2023-03-26 10:34:25.139 26270-26270 System                  com.android.phone                    W  ClassLoader referenced unknown path: /system/priv-app/TeleService/lib/x86
2023-03-26 10:34:25.176 26270-26270 System                  com.android.phone                    W  ClassLoader referenced unknown path: /system/priv-app/TelephonyProvider/lib/x86
2023-03-26 10:34:25.188   895-1003  EGL_emulation           com.android.launcher3                W  eglSurfaceAttrib not implemented
2023-03-26 10:34:25.188   895-1003  OpenGLRenderer          com.android.launcher3                W  Failed to set EGL_SWAP_BEHAVIOR on surface 0xaaf5d8c0, error=EGL_SUCCESS

よって、今回はD8s_v3を選びました。
ちなみに、D8s_v3はざっくり言うと「割りとコスパいいDシリーズの、8コアの、プレミアムSSD使用可能な、ハイパースレッド構成の第 3 世代 Intel® Xeon® Platinum 8370C (Ice Lake) プロセッサ、Intel® Xeon® Platinum 8272CL (Cascade Lake) プロセッサ、Intel® Xeon® 8171M 2.1 GHz (Skylake) プロセッサ、Intel® Xeon® E5-2673 v4 2.3 GHz (Broadwell) プロセッサ、または Intel® Xeon® E5-2673 v3 2.4 GHz (Haswell) プロセッサ上で実行されるVM」です。

image.png

参考

エミュレータの種類について

Androidエミュレータには2種類あり、Google製のAndroid EmulatorとMS製のVS Emulator for Androidがあります。
Android Studioに同梱されているのはGoogle製のもので、MS製のものは以下です。

上記のMSサイトの上部には以下のように書いてあります。
image.png

要約すると、MSがVS Emulator for Androidをリリースした後にGoogle側でEmulatorがでたよ。だからそっちが推奨だよ。でもGoogle製のものは物理サーバでしか動かないよ(重要) ということです。
つまり、Azure VM上で動かす場合はHyper-Vに対応しているVS Emulatorの方を使う必要があります。

なお、Azure VMの裏で動作しているHyper-VとHAXMは排他的であるため併存できません。よって、Android Studioで標準でインストールされるHAXMのインストールは確実にこけます。

image.png

AndroidエミュレータのAPIレベルについて

AndroidエミュレータのAPIのバージョンにも注意が必要でAPI23 Marshmallowの”5.2”を選ぶ必要があります。
image.png

なので以下のように新しいAPIが使用できません。
例えば画像の格納先の指定に「app:srcCompat」が使えないので代わりに「src」を使います。
使用不可
app:srcCompat="@drawable/background”

代わり
android:src="@drawable/background”

なお、これには参考先のサイトにも以下のようにあります。正式には非推奨だけれどもリモートVM上でAndroidコーディングしたい要件がある場合はこうやるしかないよ、みたいなことですね。

Also, on top of this page - https://visualstudio.microsoft.com/vs/msft-msandroid-emulator/, it explains you that current version of Android Emulator uses hardware acceleration. VS Emulator for Android is older version of it and not recommended.
However I chose this solution to my customers as of now; as this unblocks the customer immediately with the power of Azure cloud.
Also, right now my customer was not able to perform any android specific development task due to Corona virus situation. So even if older version, at least customer was able to continue the business and that was more important.
I repeat, the above approach of using older version of VS emulator is obsolete. For best experience you should switch to Hardware based emulator either using HAXM or WHPX. The above stated approach is a workaround for now. May be in future Microsoft Azure may release “absolutely non restricted Hyper V” supporting VMs and it may work.

参考

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0