问题描述
我有一个需要使用特定 IP 地址启动 Android Emulator 的场景?我可以这样启动模拟器吗?
I have a scenario where I need to start Android Emulator with a specific IP Address? Can I start the emulator like that?
我不想进行 IP 转发或其他事情,因为模拟器中运行着一个 Android 程序,它在启动时会使用启动时存在的 Android 模拟器 IP 地址进行自我配置.
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.
默认情况下,模拟器的 IP 地址是 10.0.2.15,我需要更改它.此外,此 Ip 设置在 init.goldfish.rc 和 init.goldfish.sh 中.如果我更改它们,android的ip仍然是10.0.2.15.
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.
推荐答案
我是这样创作的:
在 Android Emulator 中,如果您执行 ifconfig,您将看到 bridge、eth0 ð1 作为网络设备.桥有 10.0.2.15 作为 ip 并且 eth0 启动但没有任何 ip 并且 eth1 关闭没有任何 ip.
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.
现在在您的主机上创建一个 TAP 和桥接设备,并将您的 TAP 设备与您主机上的任何工作以太网卡桥接.
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.
可以按照以下步骤创建 TAP 设备 (tap1) 和桥接 (br1) 与 eth0:
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
现在,一旦您的 TAP 启动并且您的 eth1 连接到任何 dhcp 服务器,请启动 dhcp 服务器.启动 dhcp 服务器后,使用以下命令运行模拟器:
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
在您的 android 模拟器 shell 中运行以下命令:
In your android emulator shell run below command:
$netcfg
你应该可以看到 eth1 的 ip 为 0.0.0.0,现在运行下面的命令来启动 eth1:
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
然后瞧!!!你有 eth1 的 IP 地址是从 dhcp 服务器分配的.
And voila!!! you have the eth1 with ip address assigned from dhcp server.
让我知道它是否有效!!!
Let me know if it works!!!
现在,如果您在 android 模拟器中有任何程序打开外部服务器 IP 地址上的端口,它将通过主机的 eth0 到外部服务器.
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.
这篇关于为 Android 模拟器设置 IP 地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!