Android模擬器不能上網的解決方法


我原來一直不用Android的模擬器,因為這東西的多年前的印象真的是很糟糕——啟動半個小時,不支持OpenGL。即使后來有了x86鏡像,在HAXM的支持下快的飛起,也不想用,因為NDK還要編譯x86的。那個時候我一直用網易的MuMu模擬器——只需要編譯Armv7的就行了。再后來就是網易的全部Mac App不知道抽了什么風,瘋狂寫log,直到寫滿硬盤為止,我就把MuMu給刪了,同時也擺脫了這玩意動不動就啟動不起來的煩惱。

現在的Android模擬器還是不錯的,那就用一下吧。

在Mac上HAXM是不需要裝的,即使裝了也沒用。

https://docs.microsoft.com/zh-cn/xamarin/android/get-started/installation/android-emulator/hardware-acceleration?pivots=macos

先裝了一個Oreo(Google Play),什么鬼,用了10G的磁盤空間,即使禁止熱啟動也沒小多少,然后居然不能上網!網上一查,都是說要改DNS,還要root。

好吧,我知道Google的套件就是費空間,裝個干凈的吧,Q(Android 9.+),反正我也不用Google API,這回好些了,占用4G磁盤。

然后我又裝了一個Android 6.0的鏡像,占用5.2G磁盤,Android果然是垃圾堆——你會發現隨着Android版本的增加,占用磁盤越來越少——或者說Google把原來的福利套件都移除了?

關鍵是,這個能直接上網!

比較一下就能發現,Android 6.0的鏡像沒有虛擬WiFi,直接用宿主機來上網

而從Android 7.0開始,虛擬了一個NAT網絡,以下截圖是Android 9.+的

 

所謂的改DNS的原因就是如下:

 

理由就是,這是個什么見鬼的DNS,反正改一下就能上網了……

但是,即使我認為Android再垃圾,我也不認為測試團隊能讓這種事情發生——不能上網,並且從7持續到了9。並且,即使改DNS能解決問題,也不是個好方法——如果指向局域網的DNS(基本上就是網關或者路由器的地址),當你換一個環境難道再改一次DNS?

那么這個奇怪的DNS是什么,因為Android的官網不好打開,我復制了一下相關內容:

 

At startup, the emulator reads the list of DNS servers that your system is currently using. It then stores the IP addresses of up to four servers on this list and sets up aliases to them on the emulated addresses 10.0.2.3, 10.0.2.4, 10.0.2.5 and 10.0.2.6 as needed. 

On Linux and OS X, the emulator obtains the DNS server addresses by parsing the file /etc/resolv.conf. On Windows, the emulator obtains the addresses by calling the GetNetworkParams() API. Note that this usually means that the emulator ignores the content of your "hosts" file (/etc/hosts on Linux/OS X, %WINDOWS%/system32/HOSTS on Windows).

When starting the emulator at the command line, you can also use the -dns-server <serverList> option to manually specify the addresses of DNS servers to use, where <serverList> is a comma-separated list of server names or IP addresses. You might find this option useful if you encounter DNS resolution problems in the emulated network (for example, an "Unknown Host error" message that appears when using the web browser).

 

意思就是,模擬器會把宿主機的DNS依次復制到10.0.2.3,10.0.2.4,10.0.2.5,10.0.2.6,按理說這么做沒問題啊。於是我打開了系統的DNS

 

但是我的/etc/resolv.conf是這個樣子的,難道IPv6優先?系統信息里明明IPv4的地址在前面。

 

原因就很明顯了,那個IPv6的DNS是什么鬼,然后我把路由器的IPv6的功能一關,重連WIFI,重啟模擬器,問題解決了。

總結:現在國內的IPv6一團麻,根本不知道是個什么情況,干脆先禁用一陣子。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM