How to Override the hosts File on Android Without Root Access
Every mobile developer or QA engineer has faced a situation where they need to point an app to a test environment. On desktop, it's solved in a couple of seconds by opening 2 or 3. On Android, everything hits a wall of system security. Without root access, editing the system 4 file is impossible.
Of course, you can set up a local DNS server like 5 on your office network and specify its IP in the Wi-Fi settings on your phone. But what if you're working from a café, on the road, or just don't want to deal with router configuration?
This is where the Virtual-Hosts repository comes to the rescue. The project solves the IP address redirection problem directly on the mobile device without hacking the firmware or obtaining superuser rights.
How DNS Override Works Without Root
The utility's idea is elegant. Android doesn't allow modifying system files, but it does permit apps to create local network interfaces through the standard 6 API.
The app sets up a virtual network adapter right on your device. All outgoing traffic from the phone passes through this local interface. A custom DNS resolver inside Virtual Hosts intercepts requests to domain names.
If a domain is in your text rules file, the app instantly returns the specified IP address. If the domain isn't in the list, the request goes to the standard DNS server of your provider or Google. Everything stays within the phone's memory—traffic doesn't go to third-party servers.
To implement this scheme, the author took the LocalVPN library as a foundation and wrote a convenient Java wrapper around it.
What Makes Virtual Hosts Convenient
A regular 7 file in operating systems has a significant limitation: each entry has to be typed in manually. If you have a dozen subdomains, the list grows quickly.
Virtual Hosts supports wildcard entries. This saves a lot of time when working with microservices architecture or multi-tenant applications.
Instead of a long list of entries:
0You write just one line:
1The dot before the domain name means that absolutely all subdomains of any level for 8 will automatically be sent to the specified IP address.
Among the app's basic features, the following are worth highlighting:
- Loading rules from plain text files from device storage or via a direct link
- Quick toggle to enable or disable override with one click
- Support for standard IP and domain mapping syntaxes
- Minimal battery and RAM consumption
Launch and Practical Usage
The app is distributed as ready-made builds and source code. You can download the APK directly from the F-Droid catalog or Google Play:
The workflow looks like this:
- Create a text file 9 with the redirection rules you need on your phone.
- Open Virtual Hosts and select this file through the system dialog.
- Press the big power button.
- Android will ask for confirmation to create a VPN connection. Confirm the request.
After that, any browser, curl request, or mobile app on the device will start seeing your test servers by the specified domain names.
Real-World Use Cases
The project comes in handy in a variety of daily development situations.
Testing API with a local machine. A developer runs the backend on a laptop (10), but the mobile app is hardcoded to use the domain 11. Instead of rebuilding the APK with a different URL, it's enough to add a redirection in Virtual Hosts.
Checking layouts and frontend. You can quickly point a real smartphone to a locally running DevServer with hot code reloading.
Isolating test environments. When testing multiple branches simultaneously, you can switch configuration files in seconds right from the app's interface.
Limitations and Gotchas
Despite all the advantages, this approach has a couple of nuances you should know about upfront.
The main downside stems from Android's architecture. Only one VPN service can be active in the system at a time. If you need to keep a corporate WireGuard or OpenVPN enabled to access work resources, you won't be able to use Virtual Hosts in parallel.
The second point concerns the code. The project was created in 2017, and the Java codebase looks a bit dated by modern Android development standards. However, there's nothing to break: the utility does one clear task and still handles it perfectly on recent Android versions.
Summary
Virtual Hosts is an excellent utility in any mobile developer or QA engineer's arsenal. It solves a specific, targeted problem without the need to torture test devices with custom firmware, bootloader unlocking, or obtaining root. If you need to quickly redirect a domain to a local IP and test an app build in the field, this repository definitely deserves a spot in your bookmarks.
Progetti correlati