Android Studio Emulator No Internet Mac



Recently, while developing an Android Application, I was faced with a situation that wasted 2 hours of my time. The issue was simple, my app was not able to access internet from the Android Emulator.

@Yksh.Leo - I had tried that route initially but it had no effect. I would expect switching over to a bridged adapter would have a similar 'narrowing' effect on the nic used by the emulator. FYI - I am using Virtualbox 5x and the latest Android Player as of today's date. Android - Fixing the no internet connection issue on emulator Recently, while developing an Android Application, I was faced with a situation that wasted 2 hours of my time. The issue was simple, my app was not able to access internet from the Android Emulator. Android Studio’s emulator is the default Android console which comes with a bunch of additional tools. The tools are quite useful for the developer to test the Android app and games. As the name suggests it’s an Emulator to run Android apps on PC as well. This tool is available for Windows 7/8/10.


Initially I thought fixing the issue should have been straight forward, but life is not always that simple.
So what was the real issue because of which my app was not able to access internet? There are multiple reasons because of which this issue could occur. Hence, I decided to document my findings so that other could benefit from it.
There are two main symptoms of no internet connection on android emulator
  • Only your app is not able to access the internet
  • None of the apps are able to access the internet
Android Studio Emulator No Internet Mac
Lets look at what are the reasons behind each of them.
Only your app is not able to access the internet
If only your app is not able to access the internet on the emulator, check if any other apps are able to access the internet or not. For e.g. you could open up the browser application, visit http://news.google.com/. If the page opens up correctly then problem lies in your app itself and its simple to fix.
Basically, your app needs the Permission to access the internet. This can be done by adding the following line just before the end of <manifest> tag in AndroidManifest.xml file of your application.

A sample AndroidManifest.xml file with the added permission would look like
Compile and re-install the app in the emulator and try to access the internet from your app. It should work!
What is the use of uses-permissiontag:


Android application can request certain permissions so that they can function properly. Some examples of permissions are, get users location, make a call, access the internet etc. App has to explicitly specify this in the AndroidManifest.xml.
When end users install such an app on their device, the android OS will notify the user that, app is requesting certain permissions. If users are fine with that, then only the app will be installed. Else users can deny the permission and the app will not be installed.
This mechanism is Androids way of implementing security and users privacy!
None of the apps are able to access the internet
Now this situation is tricky. There are two reasons because of which this could happen
  • Proxy server is not configured on the emulator
  • Incorrect DNS used by the emulator
Emulator

If you access internet via a proxy server, then you will need to configure the proxy server on the emulator as well. This is done via the following steps.
Open up the Settings application on the Emulator
Settings
Select the Wireless & Network settings
Wireless and Networks
Mobile Networks

Emulator For Mac

Select the Access Point Names
Access Point Names
Select the Access Point listed there. In my case its Telkila
Select the access point
In the Edit access point section edit the value of Proxy and Port. These should point to your Proxy server and Port that you use.
Set the proxy and port values
Thats it! After doing these settings try to access the internet from the browser application on the emulator. It should work!
If you are not behind a proxy (like me) then the problem is because of an incorrect dns server. Lets look at how to fix the dns issue.
Incorrect DNS Used by the Emulator:
In my case none of the above things fixed the issue. The real issue was that the DNS used by the Emulator was incorrect. Fortunately there is a way in which we can specify which DNS server to use.
Android Emulator takes a command line parameter -dns-server using which you can specify which DNS server to use. The sample command to specify DNS server would look like

Android Emulator For Mac Air


-dns-server takes a comma separated list of IP address to use as DNS server.
That did the trick for me! I was finally able to access internet from my app on the android emulator!

Normally I'm developing on a mac with Xamarin Studio. Debugging an Android project on the Android emulator running on your mac is easy, because it just works. But now and the I need to do some work from my windows machine. My windows is running as a VM with Parallels Desktop.

Visual Studio isn't recognising the Android emulator running on my mac. I'm ok with that because the mac and windows are two different machines. Now, there is a way to debug your Android app on the emulator running on mac from your visual studio running on windows (if you don't lost me now, keep reading because doing so isn't that hard).

Find the IP address of your mac

Open up a terminal application and search for your ip address:

Connect the emulator from Windows

Normally it should be possible to connect to an emulator via the adb command.
On your windows machine open up an Android Adb Command Prompt and try to connect with the command:

I was a bit surprised to see the following error:

Even with the firewall on the mac disabled I got the same result. After some googling I wasn't surprised to find a solution on the Xamarin website itself. The full article can be found on https://developer.xamarin.com/guides/android/troubleshooting/questions/connect-android-emulator-mac-windows/

To make it easy, I'll give you the short version:

Step 1

Kill the adb server on your mac

Step 2

forward inbound TCP packets to the loopback interface and outbound packages back the other way

That's it, as long as you leave that command open, you can now connect from your Windows machine to your Android emulator running on the mac. And when the firewall of your mac is enabled you'll get a pop-up to ask if it's ok to change its settings.