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

You are currently viewing [SOLVED] How to connect Android Emulator to localhost application- 1 simple solution?
Photo by Masakaze Kawakami on Unsplash
Share This Post, Help Others, And Earn My Heartfelt Appreciation! :)
4.9
(756)

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.

Solution -> Connect Android Emulator to localhost application

Instead of localhost/127.0.0.1 use 10.0.2.2 IP address.

Example

    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>

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

If you enjoyed this post please add the comment below and share this post on your Facebook, Twitter, LinkedIn or another social media webpage.
Thanks in advanced!

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.

Subscribe
Notify of
guest
5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Hussain

File /src/main/res/xml/network_security_config.xml This path is not my App
Anymore source to resolve the error

Bineesh

This really helped me, no you saved me.