6
2

More than 5 years have passed since last update.

Androidのエミュレータでhostsを書き換える

Posted at

概要

Androidのエミュレータでhostsを書いていないと見れない環境にアクセスする場合の対処方法です。

準備

adbコマンドを使えるようにします。恐らく、Androidを開発している人であればSDKが入っているので、下記のような方法でPATHを通しておきます。

.zshrcなど
export PATH=$PATH:/Users/musclemikiya/Library/Android/sdk/platform-tools

エミュレータがGooglePlayベースではないことを確認します。(adb rootになれない)
https://stackoverflow.com/questions/43923996/adb-root-is-not-working-on-emulator

手順

ローカルの/etc/hostsファイルを同期します。

adb root && adb remount && adb push /etc/hosts /system/etc/

トラブルシュート

エラーが出る場合

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

上記のエラーが出る場合は、一旦エミュレータを落とし、下記コマンドで立ち上げます。(端末名のところは適宜書き換え)

$ emulator -writable-system -netdelay none -netspeed full -avd Pixel_2_XL_API_28

emulatorはadbコマンドと同じくsdkフォルダの中にあります。

127.0.0.1に接続したい

PC上のローカル環境で開発していて、そこに接続したい場合、エミュレータからはlocalhostでは接続できません。そのかわり、ローカルPCには10.0.2.2というIPが割り当てられているので、一時的にhostsを書き換えると接続できます。

6
2
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
6
2