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.
Table of Contents
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!
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());
<?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>
<?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>
To connect an Android Emulator to localhost, you can use the following steps:
- Start the Android Emulator.
- In the emulator, open a terminal window by typing
terminalin the search bar.
- In the terminal window, type
ifconfigand press Enter. This will display the network configuration for the emulator.
- Look for the entry for
wlan0. This is the network interface for the emulator.
- Find the IP address for the network interface. It will be in the format
- On your local machine (the one running the emulator), open a web browser and enter the following URL:
XXX.XXX.XXX.XXXis the IP address you obtained in the previous step and
3000is 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.
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.
The following table presents the Android Studio ssystem requirements:
- 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!
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?