Autosana Logo
    Autosana
    • SandboxesBeta
    • Enterprise
    • Pricing
    Contact salesTry for free
    Resources

    Blog

    Insights on automated testing and QA best practices.

    BlogProduct updatesCase studies
    AllComparisonsBest ToolsMobile TestingAI TestingTesting Fundamentals

    Fix bugs, not tests.

    Talk to an expert.

    Book 30 minutes with our team. We'll show you Autosana running on your app.

    Contact sales

    Start building.

    Describe a user journey and watch an agent run it on iOS, Android, and web.

    Try for free
    Autosana

    Catch bugs before your users do.

    Try for free

    Product

    • Platform
    • Use cases
    • Enterprise
    • Pricing

    Resources

    • Docs
    • Case Studies
    • Product Updates
    • Blog

    Company

    • Contact sales
    • Careers
    • LinkedIn

    Trust

    • Status
    • Security
    • SOC 2 Type 1 · Type 2 in review
    • Backed by

    © 2026 Autosana. All rights reserved.

    Autosana Logo
    Autosana
    • SandboxesBeta
    • Enterprise
    • Pricing
    Contact salesTry for free
    Written by
    Read time
    Published
    Autosana

    Catch bugs before your users do.

    Try for free

    Product

    • Platform
    • Use cases
    • Enterprise
    • Pricing

    Resources

    • Docs
    • Case Studies
    • Product Updates
    • Blog

    Company

    • Contact sales
    • Careers
    • LinkedIn

    Trust

    • Status
    • Security
    • SOC 2 Type 1 · Type 2 in review
    • Backed by

    © 2026 Autosana. All rights reserved.

    Autosana Logo
    Autosana
    • SandboxesBeta
    • Enterprise
    • Pricing
    Contact salesTry for free
    Back to Blog
    Blog

    Android emulator localhost: one base URL for every test device

    Android emulator localhost is the emulator, not your machine. Use 10.0.2.2 or adb reverse, fix cleartext and ATS errors, and reach cloud and real devices.

    Written by
    Yuvan Sundrani
    Read time
    10 min read
    Published
    December 1, 2026
    Android emulator localhost: one base URL for every test device

    Key Takeaways

    • On an Android emulator, 127.0.0.1 is the emulator's own loopback; your machine is 10.0.2.2.
    • adb reverse tcp:3000 tcp:3000 lets the app keep calling localhost:3000 on an emulator or a USB phone.
    • Android 9 blocks plain HTTP by default, and <debug-overrides> can't turn it back on; a debug-only network_security_config.xml can.
    • From iOS 17, ATS no longer allows plain HTTP to IP addresses by default, so http://127.0.0.1 can fail where http://localhost works.
    • A cloud simulator sees neither your laptop nor 10.0.2.2; it needs a reverse tunnel, and a real device needs a public URL.

    On an Android emulator, localhost is the emulator itself, so reach your machine at 10.0.2.2 or map the port back with adb reverse.

    Localhost address by device type

    Where the app runslocalhost meansHow it reaches your machineTypical failure
     
    iOS Simulator on your MacYour MacDirectly, same network stackATS blocks plain HTTP to an IP
    Android emulator on your machineThe emulator10.0.2.2, or adb reverseConnection refused, then cleartext error
    Simulator or emulator in a cloudThe cloud hostA reverse tunnel to your machineConnection refused, no log line
    Real phoneThe phoneLAN IP, USB adb reverse, or a public URLTimeout on guest Wi-Fi

    Microsoft's .NET MAUI docs state that the iOS Simulator uses the host machine network, so http://localhost:<port> reaches the Mac with no extra setup.

    Each Android emulator instance runs behind a virtual router. Google's emulator networking address table lists 10.0.2.2 as the special alias to your host loopback and 127.0.0.1 as the emulated device's own loopback. So localhost:3000 inside the emulator asks the emulator for port 3000, and nothing is listening there.

    How localhost:3000 gets home from four devices: the iOS Simulator on a Mac uses the shared network stack, an Android emulator uses adb reverse tcp:3000 tcp:3000, a cloud simulator or emulator uses autosana sandbox forward 3000, and a real phone has no route to your loopback so it needs a public HTTPS URL
    Localhost bugs, caught in the cloud. Try Autosana for free.

    adb reverse setup for Android emulator localhost

    A per-platform base URL (10.0.2.2 on Android, localhost on iOS) needs a new branch for every new place the build runs: a USB phone, a second emulator, a cloud device. Keeping every debug build on http://localhost:<port> and moving the routing into the device connection avoids the branching.

    # device port 3000 -> host port 3000
    adb reverse tcp:3000 tcp:3000

    # Metro too, if this is React Native
    adb reverse tcp:8081 tcp:8081

    adb reverse --list
    adb reverse --remove tcp:3000
    adb reverse --remove-all

    The adb man page gives the form as reverse REMOTE LOCAL: the first port is on the device, the second is on your machine. adb forward is the opposite direction, host port to device port, and it's what you want for reaching a debug server inside the app.

    • Reverse rules live on the adb connection. After an emulator cold boot or an adb kill-server, the rule is gone. Re-run it in the script that launches the app, never once by hand.
    • It works on USB phones too. React Native's own device docs use adb reverse tcp:8081 tcp:8081 for a phone on USB, which is the same trick for Metro.
    • Several devices need -s. With two emulators attached, adb -s emulator-5556 reverse tcp:3000 tcp:3000 targets one; the bare command errors out.

    If you script devices often, the adb commands cheat sheet covers the rest of the device-selection flags.

    If you'd rather keep a per-device URL, our sandboxes can hand it to the app at launch: autosana sandbox launch --launch-env API_URL restarts the app with the value from the sandbox's environment, as an environment variable on iOS and an intent extra on Android, with no rebuild.

    CLEARTEXT communication not permitted fix

    Android's network security docs state that cleartext support is disabled by default starting with Android 9 (API level 28). With the port mapped, OkHttp fails the request with:

    CLEARTEXT communication to localhost not permitted by network security policy

    Allow exactly the host named in that message, and only in the debug source set, so a release build can't ship the exception. In an Autosana sandbox, the same line turns up with autosana sandbox logs --grep "CLEARTEXT", next to the screenshot of the blank screen.

    app/src/debug/res/xml/network_security_config.xml:

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

    app/src/debug/AndroidManifest.xml:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android">
        <application android:networkSecurityConfig="@xml/network_security_config" />
    </manifest>

    The manifest merger applies src/debug/AndroidManifest.xml to debug builds only.

    <debug-overrides> can't enable cleartext. It only accepts <trust-anchors>, for trusting a proxy's CA in debug builds.

    android:usesCleartextTraffic is on its way out. Android ignores it when a network security config is present, and the manifest reference says it will be ignored entirely for apps targeting API level 38 and above.

    If your main manifest already points at a config file (for certificate pinning, say), the merger reports a conflict. Copy that file into the debug source set, add the localhost domains there, and mark the debug manifest attribute with tools:replace="android:networkSecurityConfig".

    iOS Simulator localhost ATS fix

    Apple's NSAllowsLocalNetworking reference says the key controls connections to unqualified domains, .local domains and IP addresses. Through iOS 16, ATS allowed all three by default. In iOS 17, ATS no longer allows connections to IP addresses by default.

    http://localhost:3000 loads, because localhost is an unqualified name. http://127.0.0.1:3000 or http://192.168.1.20:3000 fails on an iOS 17 simulator runtime and passes on an iOS 16 one. Same code, different runtime.

    The fix for debug builds:

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsLocalNetworking</key>
        <true/>
    </dict>

    Keep it in a debug-only plist, set per configuration through the INFOPLIST_FILE build setting. On an Autosana iOS Simulator sandbox, autosana sandbox network --failed lists the failed requests, which is quicker than reading the console for an ATS refusal.

    From Apple's ATS key references:

    • NSAllowsArbitraryLoads is ignored once NSAllowsLocalNetworking is present. A plist with both blocks plain HTTP to a remote staging host, even though "arbitrary" sounds like it covers it.
    • NSExceptionDomains keys take no port number. localhost:3000 as a key matches nothing.
    • Arbitrary loads need a justification at App Store review, which is one more reason to keep the debug plist separate.
    The cleartext fix file by file: on Android 9 and later the symptom is CLEARTEXT communication to localhost not permitted, the fix is a debug network_security_config.xml allowing localhost and 10.0.2.2, and debug-overrides cannot enable cleartext; on iOS 17 and later http://127.0.0.1 is refused while localhost loads, the fix is NSAllowsLocalNetworking in a debug Info.plist, and NSAllowsArbitraryLoads is then ignored

    adb reverse port remapping

    The app's debug build has a port baked in. Your backend doesn't always run there. Two git worktrees can't both bind port 3000, so the second backend lands on 8000 while both builds still call localhost:3000.

    adb reverse remaps without a rebuild: the device keeps port 3000 and your machine serves 8000.

    # device 3000 -> this worktree's backend on 8000
    adb -s emulator-5556 reverse tcp:3000 tcp:8000

    A server bound to 127.0.0.1 is enough for the simulator and for adb reverse, which both arrive on loopback. A phone on Wi-Fi arrives on your LAN interface, so that server has to bind 0.0.0.0. A server bound only to IPv6 ::1 refuses a client that resolved localhost to 127.0.0.1.

    Metro's port 8081 collides the same way across worktrees, covered in one device per worktree for parallel coding agents.

    Real device access to a local backend

    A real phone has no route to your machine's loopback. The options:

    RoutePlatformsWhat you configureWhat breaks
     
    USB adb reverseAndroidSame rules as the emulatorOnly while the cable and adb session hold
    Same Wi-Fi, LAN IPBothServer on 0.0.0.0, cleartext entry for the IP, iOS local network promptGuest networks that isolate clients, DHCP changing the IP
    Public HTTPS tunnelBothNone on the device, since it's HTTPSAnyone with the URL reaches your laptop

    On iOS 14 and later, a debug build that talks to a LAN IP also needs an NSLocalNetworkUsageDescription string, because the system asks the user before allowing local network access. Expo's CLI documents a tunnel mode for Metro and notes that tunnel URLs are public.

    For real devices in a device cloud, a forward isn't on the table, ours included: point the app at a public HTTPS staging URL.

    If staging sits behind a firewall, our network allowlist publishes the source ranges for real-device runners. The broader question of when a real phone is worth that setup is covered in real device testing: when a simulator isn't enough.

    Autosana for cloud device localhost access

    Autosana is an AI end-to-end testing agent for iOS, Android and web apps. For this problem, our Autosana sandboxes give a coding agent a cloud iOS Simulator, Android Emulator or real device, and our CLI carries the device's ports back to your machine, so the debug build from the earlier sections runs unchanged.

    Autosana key features for local backends

    1. Port forwarding from the cloud device. autosana sandbox forward 8081 3000 connects the device's ports to the same ports on your machine, and 3000:8000 maps the device's 3000 to your 8000. It does the job of adb reverse over the internet, so the app keeps calling localhost:3000.
    2. One sandbox per git worktree. Commands act on the worktree's own sandbox, so two coding agents on two branches never share a device or a port map. The full setup is in sandboxing coding agents that drive mobile devices.
    3. Evidence when a request still fails. Every action saves a screenshot, and logs and network take --since <step>. The logs stay readable by sandbox ID after the device is returned, so a refusal from an overnight agent run can be traced the next morning.

    autosana sandbox start --upload app-debug.apk
    autosana sandbox forward 8081 3000:8000   # Metro on 8081, device 3000 to your 8000

    Our flow editor inserting an environment variable into a test, so one flow reads a different backend URL per environment
    Environment variables in our flow editor: the test references ${env:...} and each environment supplies its own value, such as the backend URL a cloud device should call. Source: Autosana Environments docs video.

    Environment variables in our flow editor: the test references ${env:...} and each environment supplies its own value, such as the backend URL a cloud device should call. Source: Autosana Environments docs video.

    Autosana customer results

    • Gobi (social maps app) replaced 13 brittle Maestro tests with 30+ Autosana tests, and nightly runs caught six real bugs in five days. Their CTO: "It's like a human is using your app." See the Gobi case study.

    Where Autosana fits less well

    • Real devices in our cloud have no forward, so the app has to call a public HTTPS URL there.
    • autosana sandbox network --failed covers the iOS Simulator only; on Android, request failures show up in logs.
    • Our Tailscale routing covers web tests. A private staging API for mobile runs goes through the IP allowlist.
    • Web testing runs on Chrome, Firefox, Edge and Chromium, with no Safari.

    Why use Autosana for localhost testing on cloud devices?

    • One debug URL on every device. The app calls localhost on an emulator, a simulator and a cloud device alike, so there's no per-platform base URL to maintain.
    • No tunnel to configure. Port forwarding is one command in the CLI the coding agent already uses.
    • Agents on different branches stay apart. Each worktree gets its own device and its own port map.
    • A refused request comes with proof. Step screenshots and kept logs show whether the request ever left the device.
    • Free to start. Pay as you go includes up to 1,000 free AI actions a month, with no credit card.
    Cloud devices, local backend. Try Autosana for free.

    Conclusion

    Per-platform base URLs grow a branch for every device type. One debug URL, http://localhost:<port>, with cleartext allowed for exactly localhost and 10.0.2.2 in debug source sets, leaves the routing to adb reverse on emulators and USB phones and to autosana sandbox forward on cloud simulators and emulators. That is the setup to standardize on once agents run the app.

    Real devices stay the exception. They need a public HTTPS URL, and in a device cloud, a staging host that allowlists the real-device runner ranges.

    FAQs

    1. What address reaches the host from an Android emulator?

    10.0.2.2 is the alias for your machine's loopback. 127.0.0.1 is the emulator's own.

    2. Does adb reverse work on a real Android phone?

    Yes, over a USB or wireless adb connection. The phone then reaches your port at localhost.

    3. Why does Android say CLEARTEXT communication not permitted?

    Android 9 disabled plain HTTP by default. Allow specific hosts in a debug network security config.

    4. Does the iOS Simulator need port forwarding for localhost?

    No. It uses the Mac's network, so localhost reaches the Mac directly.

    5. Why does http://127.0.0.1 fail on iOS 17 when localhost works?

    iOS 17 stopped allowing IP addresses under ATS by default. Hostnames like localhost still pass.

    6. Can an Autosana sandbox reach a backend on my laptop?

    Yes, through autosana sandbox forward on simulators and emulators. Real devices need public URLs.

    Sources

    • Android Developers: Emulator networking address
    • Apple Developer: NSAllowsLocalNetworking

    ‍

    Fix bugs, not tests.

    Talk to an expert.

    Book 30 minutes with our team. We'll show you Autosana running on your app.

    Contact sales

    Start building.

    Describe a user journey and watch an agent run it on iOS, Android, and web.

    Try for free
    Autosana

    Catch bugs before your users do.

    Try for free

    Product

    • Platform
    • Use cases
    • Enterprise
    • Pricing

    Resources

    • Docs
    • Case Studies
    • Product Updates
    • Blog

    Company

    • Contact sales
    • Careers
    • LinkedIn

    Trust

    • Status
    • Security
    • SOC 2 Type 1 · Type 2 in review
    • Backed by

    © 2026 Autosana. All rights reserved.