Set IP Address for Android Emulator

久未见 提交于 2019-12-18 05:21:22

问题


I have a scenario where I need to start Android Emulator with a specific IP Address? Can I start the emulator like that?

I do not want to do IP forwarding or other stuff, because there is an Android program running in the Emulator which on boot will configure itself with Android Emulators IP address present during boot time.

As by default the ip address of emulator is 10.0.2.15 which I need to change. Also, this Ip is set in init.goldfish.rc and init.goldfish.sh. If I change them, still the ip of android is 10.0.2.15.


回答1:


I made work by this way:

In Android Emulator if you do ifconfig then you will see bridge, eth0 & eth1 as network devices. Where bridge has the 10.0.2.15 as ip and eth0 is up but without any ip and eth1 is down without any ip.

Now create a TAP and Bridge devices on you host machine and bridge your TAP device with any of the working ethernet cards on you host machine.

The TAP device (tap1) and bridging (br1) it with eth0 can be created following below steps:

$sudo ip tuntap add dev tap1 mode tap
$sudo ip link show dev tap1
$sudo brctl addbr br1
$sudo brctl addif br1 tap1
$sudo brctl addif br1 eth0
$sudo ip link set eth0 up
$sudo ip link set br1 up
$sudo brctl show

So now once your TAP is up and if your eth1 is connect to any dhcp server, start the dhcp server. Once the dhcp server is started, run the emulator with below command:

$sudo ./emulator -avd <avd_name> -qemu -net nic,vlan=1 -net user,vlan=1,hostname=<hostname_you_want> -net nic,vlan=2,macaddr=<mac_id_of_eth1_of_android_emulator> -net tap,ifname=tap1,script=no,vlan=2

In your android emulator shell run below command:

$netcfg

You should be able to see eth1 down with ip 0.0.0.0, now run below command to bring up the eth1:

$netcfg eth1 dhcp

And voila!!! you have the eth1 with ip address assigned from dhcp server.

Let me know if it works!!!

Now if you have any program in android emulator opening port on external server ip address it would go through eth0 of host machine to the external server.



来源:https://stackoverflow.com/questions/15350555/set-ip-address-for-android-emulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!