[SOLVED] How to connect Android Emulator to localhost application- 1 simple solution?

[SOLVED] How to connect Android Emulator to localhost application- 1 simple solution?
Photo by Masakaze Kawakami on Unsplash
Share this post and Earn Free Points!

To connect from the Emulator to the application that we have running on [ Connect Android Emulator to localhost application ] your current machine, you must give the appropriate IP address, because Android internally recognizes localhost / 127.0.0.1 as the internal address of the loopback website.

Why We Need It?

When we develop Android application which will communicate with other application we want to have such possibility without application deployment of these application to target server. Just have everything on localhost machine and integrate it together.

Using this approach we will save a lot of time, due to fact that application during development are constantly changing and the test loop should be as quicker as possible. We will save a lot of time and we will be calmer people. It is terrible and frustrating for the developer to waste time running tests and adding them.

Let’s move to the solution about how to Android Emulator connect to localhost!

Solution -> Connect Android Emulator to localhost application

Instead of localhost/127.0.0.1 use 10.0.2.2 IP address.

Android Emulator Localhost

    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("https://10.0.2.2:8080/")
            .addConverterFactory(GsonConverterFactory.create());

File /src/main/res/xml/network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">10.0.2.2</domain>
    </domain-config>
</network-security-config>

File AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bigdataetl.cards" >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:networkSecurityConfig="@xml/network_security_config"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Alternative Solution

To connect an Android Emulator to localhost, you can use the following steps:

  1. Start the Android Emulator.
  2. In the emulator, open a terminal window by typing terminal in the search bar.
  3. In the terminal window, type ifconfig and press Enter. This will display the network configuration for the emulator.
  4. Look for the entry for eth0 or wlan0. This is the network interface for the emulator.
  5. Find the IP address for the network interface. It will be in the format XXX.XXX.XXX.XXX.
  6. On your local machine (the one running the emulator), open a web browser and enter the following URL: http://XXX.XXX.XXX.XXX:3000, where XXX.XXX.XXX.XXX is the IP address you obtained in the previous step and 3000 is the port number of the localhost server.

Note: If you are using a physical Android device rather than an emulator, you can use the same steps to connect to localhost, but you will need to use the IP address of your computer rather than the emulator’s IP address. You can find your computer’s IP address by typing ipconfig in a terminal window on your computer.


Android Emulator

Each instance of the Android Emulator uses an Android virtual device (AVD) to specify the Android version and hardware characteristics of the simulated device. To effectively test your app, you should create an AVD that models each device on which your app is designed to run. To create and manage AVDs, use the Device Manager.

Each AVD functions as an independent device, with its own private storage for user data, SD card, and so on. By default, the emulator stores the user data, SD card data, and cache in a directory specific to that AVD. When you launch the emulator, it loads the user data and SD card data from the AVD directory.

https://developer.android.com/studio/run/emulator

Android Application Development

During Android application development I use the Android Studio in combination with Android Emulator. It allows me to quickly do development tasks and I can deploy this application using one click to Android environment.

Android Studio

Android Studio is the best tool which can be used to develop Android application. It’s based on the Intellij IDEA, but develop and adapted for writing Android application.

Android Studio can be installed on Windows, Mac and Linux platforms.

System Requirements

The following table presents the Android Studio ssystem requirements:

Windows

  • 64-bit Microsoft® Windows® 8/10
  • x86_64 CPU architecture; 2nd generation Intel Core or newer, or AMD CPU with support for a Windows Hypervisor
  • 8 GB RAM or more
  • 8 GB of available disk space minimum (IDE + Android SDK + Android Emulator)
  • 1280 x 800 minimum screen resolution

Linux

  • MacOS® 10.14 (Mojave) or higher
  • ARM-based chips, or 2nd generation Intel Core or newer with support for Hypervisor.Framework
  • 8 GB RAM or more
  • 8 GB of available disk space minimum (IDE + Android SDK + Android Emulator)
  • 1280 x 800 minimum screen resolution

Mac

  • Any 64-bit Linux distribution that supports Gnome, KDE, or Unity DE; GNU C Library (glibc) 2.31 or later.
  • x86_64 CPU architecture; 2nd generation Intel Core or newer, or AMD processor with support for AMD Virtualization (AMD-V) and SSSE3
  • 8 GB RAM or more
  • 8 GB of available disk space minimum (IDE + Android SDK + Android Emulator)
  • 1280 x 800 minimum screen resolution

I hope this article was helpful for you and helped you to connect Android Emulator to localhost. If yes please share my post with your friends!

Could You Please Share This Post? 
I appreciate It And Thank YOU! :)
Have A Nice Day!

How useful was this post?

Click on a star to rate it!

Average rating 4.9 / 5. Vote count: 756

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?