LoginSignup
4
7

More than 3 years have passed since last update.

mac で Android エミュレーター の hosts を変更する方法

Last updated at Posted at 2019-10-04

エミュレーターのリストを表示

※ Android 11 だと hosts ファイルの転送ができませんでした。

$ ~/Library/Android/sdk/emulator/emulator -list-avds
Pixel_3a_XL_API_28_9.0_

エミュレーターを書き込みモードで起動

$ ~/Library/Android/sdk/emulator/emulator -avd Pixel_3a_XL_API_28_9.0_ -writable-system

上記コマンドを実行するとエミュレーターが起動する。

既にエミュレーターが起動している場合は

emulator: WARNING: System image is writable
emulator: ERROR: Running multiple emulators with the same AVD is an experimental feature.
Please use -read-only flag to enable this feature.

こんなエラーが表示されるので終了してからコマンドを実行する。

注意

ここで起動するエミュレーターは 「Play Store」ではない ものでないとダメらしい。

root を取得

もうひとつターミナルを起動して以下実行する。

$ ~/Library/Android/sdk/platform-tools/adb root
restarting adbd as root

もし「Play Store」のエミュレーターを起動していた場合は

adbd cannot run as root in production builds

こんなエラーが出る。

再マウント

$ ~/Library/Android/sdk/platform-tools/adb remount
remount succeeded

エミュレーターの hosts ファイルを取得

$ ~/Library/Android/sdk/platform-tools/adb pull /system/etc/hosts ~/Desktop/
/system/etc/hosts: 1 file pulled. 0.0 MB/s (79 bytes in 0.002s)

Android Studio の Device File Explorer でエミュレーター内のファイルを確認すると
/etc/hosts も存在するが /system/etc/hosts へのリンクが貼ってあるだけ。

hosts ファイルを編集

127.0.0.1       localhost
::1             ip6-localhost

10.0.2.2 変更するドメイン ← 追記

ローカルへ接続する場合、 10.0.2.2 でないとダメらしい。

hosts ファイルを転送

$ ~/Library/Android/sdk/platform-tools/adb push ~/Desktop/hosts /system/etc/hosts
/Users/*****/Desktop/hosts: 1 file pushed. 0.0 MB/s (79 bytes in 0.011s)

もし書き込みモードでエミュレーターを起動していない場合は

adb: error: failed to copy '/Users/*****/Desktop/hosts' to '/system/etc/hosts': remote couldn't create file: Read-only file system

こんなエラーが出る。

root を解除する

$ ~/Library/Android/sdk/platform-tools/adb unroot
restarting adbd as non root

再起動する

$ ~/Library/Android/sdk/platform-tools/adb reboot

表示されるか確認する

以上で hosts が変更されたハズ。

Chrome などでちゃんと表示されるか確認する。

参考

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