2

我有一个需要使用特定 IP 地址启动 Android Emulator 的场景?我可以这样启动模拟器吗?

我不想做 IP 转发或其他事情,因为模拟器中运行着一个 Android 程序,它在启动时会使用启动时存在的 Android 模拟器 IP 地址配置自己。

默认情况下,模拟器的 IP 地址是 10.0.2.15,我需要更改它。此外,此 Ip 设置在 init.goldfish.rc 和 init.goldfish.sh 中。如果我更改它们,android 的 ip 仍然是 10.0.2.15。

4

1 回答 1

2

我是这样创作的:

在 Android Emulator 中,如果您执行 ifconfig,那么您将看到网桥、eth0 和 eth1 作为网络设备。桥将 10.0.2.15 作为 ip 并且 eth0 启动但没有任何 ip 并且 eth1 关闭而没有任何 ip。

现在在您的主机上创建一个 TAP 和桥接设备,并将您的 TAP 设备与您主机上的任何工作以太网卡桥接。

可以按照以下步骤创建 TAP 设备 (tap1) 和桥接 (br1) 与 eth0:

$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 服务器后,使用以下命令运行模拟器:

$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 中运行以下命令:

$netcfg

您应该能够看到 ip 0.0.0.0 下的 eth1,现在运行以下命令以启动 eth1:

$netcfg eth1 dhcp

瞧!你有 eth1 的 IP 地址是从 dhcp 服务器分配的。

让我知道它是否有效!

现在,如果您在 android 模拟器中有任何程序打开外部服务器 ip 地址上的端口,它将通过主机的 eth0 到外部服务器。

于 2013-03-14T17:10:41.263 回答