リア充爆発日記

You don't even know what ria-ju really is.

Androidのエミュレータのhostsを編集する

RESTful APIとWebサイトを1つのアプリケーションで作るでクライアントからはapiサブドメインにアクセスさせよう!と決めて、rspecでroutingのテストでその縛りを入れてしまったので、エミュレータからのアクセスはサブドメインapiが入ったhost指定にしないといけなくなった。

エミュレータのhostsって変えられるんか?と思ったけどちょっと調べたらたくさん方法が載ってた。

コマンドラインからしかやり方がわからないけど

emulator -avd mapv2intel -partition-size 1024 -no-snapshot &

で、エミュレータを起動。-no-snapshotつかわないと手元の環境ではうまくいかなかったのでつけてある。パーティションサイズも、ぐぐると128とかが多いけど、なんかうまくいかないので1024くらいに適当にしておいた。

最後の&をつけないとバックグラウンドで動いてくれなくてコンソールが1つ縛られちゃうのでお好みで。

で、

adb remount
adb pull /system/etc/hosts /tmp/hosts
echo "192.168.10.25    api.example.com" >> /tmp/hosts
adb push /tmp/hosts /system/etc

とやる。

ちゃんと変更されているかは

$ adb shell cat /system/etc/hosts
127.0.0.1		    localhost
192.168.10.25    api.example.com

で確認できる。

これにAndroidでGoogle Maps v2 をエミュレータで動かしてしかも爆速の内容を加えるて、以下のようなスクリプトを使ってる。

#!/bin/bash

# run this manually first
# emulator -avd mapv2intel -partition-size 1024 -no-snapshot

adb -s emulator-5554 install ~/Documents/com.android.vending-1.apk
adb -s emulator-5554 install ~/Documents/com.google.android.gms-1.apk
adb -s emulator-5554 install ~/Documents/com.google.android.gsf.apk

adb remount
adb pull /system/etc/hosts /tmp/hosts
echo "192.168.10.25    api.example.com" >> /tmp/hosts
adb push /tmp/hosts /system/etc

もうちょっと工夫の余地がありそうかな。