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.
解决方案
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 -qemu -net nic,vlan=1 -net user,vlan=1,hostname= -net nic,vlan=2,macaddr= -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.