Android emulator: when there’s no connection to the internet
This happens from time to time on the Android emulator that lives on my computer — I launch an instance to work on and it just decides that it doesn’t want to connect to the internet. This is usually followed by the typical turn off and on the WiFi on the emulator, then on the computer and then either wipe all the data and relaunch or just creating a new instance to see if it would magically work.
It doesn’t.
The problem here seems to be related to the DNS picked by the Android emulator, which it cannot resolve and therefore connect to the internet.
Solution
Since the problem is related to the DNS used by the emulator (by default, it uses the same as your machine) the approach that I’ve taken is just to launch the emulator configured to connect to Google’s public DNS’ — 8.8.8.8.
emulator @<your_emulator_name> -dns-server 8.8.8.8
How to find your emulator name?
You can see all the emulators that you’ve created by entering the following command on the terminal/shell:
emulator -list-avds
How to use the emulator command?
Depending on the operating system that you’re using the emulator is under:
- On Mac
.../Library/Android/sdk/tools
- On Windows
...AppData\Local\Android\Sdk\tools
You can find these locations by using:
- On Android Studio by right + click on your project
- “Open Module Settings”
- Look on the sidebar for “SDK Location” and select this tab
- You should now have a couple of directories there, namely “Android SDK location” which is the one that we’re looking for
Now, let’s add it to your environment path:
- On Mac:
$ cd ~
$ open .bash_profile
And add the Android SDK locations copied from Android Studio:
export PATH=$PATH:/Users/{whois}/Library/Android/sdk/platform-tools
export PATH=$PATH:/Users/{whois}/Library/Android/sdk/tools
export ANDROID_HOME=/Users/{whois}/Library/Android/sdk
Close and execute the commands just added
$ source .bash_profile
Just to test that everything is working as expected try to enter this command:
$ adb version
- On Windows
You’ll need to add the ANDROID_HOME
to the “System variables”. You’ll need to:
- Go to System → Advanced system settings
- Select the “Advanced” tab
- Click on “Environment Variables…”
- And under “System variables” add
ANDROID_HOME
C:\Users\{whois}\AppData\Local\Android\Sdk\ANDROID_PLATFORM_TOOLS
C:\Users\{whois}\AppData\Local\Android\Sdk\platform_toolsANDROID_TOOLS
C:\Users\{whois}\AppData\Local\Android\Sdk\tools