How to test location features in an Android app
A practical guide to checking location-dependent behaviour without travelling: what to test, four ways to feed a test location, a step-by-step for a physical device, and what goes wrong.
Test a location feature by feeding it controlled positions instead of real travel: the Android Emulator for quick checks, a mock location app on a physical device for realistic runs, and test providers in your own code for automated tests. Whichever you use, test the states around the coordinate as well: permission level, first fix, no fix, accuracy, movement, background use and location services switched off.
What actually needs testing in a location feature?
A single "it shows the right city" check covers very little. Most location bugs live in the states around the coordinate, so list those first.
- Permission states. Test denied, granted while the app is in use, and approximate-only. According to the Android location permission guide (opens in a new tab), if the user grants only approximate location, your app gets approximate location regardless of which permissions it declares, so features that need precision must degrade sensibly.
- No fix and first fix. What does the screen show before any location arrives? How long will it wait? A test location arrives instantly, so also test the waiting state deliberately, for example by running the app with no location source active.
- Accuracy changes. A coordinate with an accuracy of 5 metres and one of 500 metres should not behave the same way in a feature that matches users to places.
- Movement and speed. Anything that reacts to travel (route progress, distance, "you have arrived") needs a sequence of positions with believable timing, not one point.
- Background behaviour. Background access needs the
ACCESS_BACKGROUND_LOCATIONpermission on Android 10 and later, and an app with a foreground service is treated as using foreground location. Check what happens when the screen is off and when the app is removed from the recent apps list. - Location services off. Turn location off in the system quick settings while the app is running and confirm it explains what is wrong instead of showing a stale position.
If your feature uses geofences, that has enough of its own quirks to deserve a separate read: see our guide to testing geofences on Android.
What are the ways to feed a test location to an Android app?
There are four common routes. They overlap, and the right one depends on whether you need realism, repeatability or automation.
| Method | Good at | Not good at |
|---|---|---|
Android Emulator location controls and adb emu geo fix |
No hardware needed, scriptable from a terminal, saved points and routes, GPX/KML playback | Not a real GNSS chip, so it says little about signal quality or device-specific behaviour |
| Mock location app (Developer options) | Real device, works with apps you cannot modify, good for manual and exploratory testing | Manual setup, the app can detect the mock flag, easy to forget to switch off |
| Test providers in your own code | Fully automated and repeatable, runs in CI, exact control of every field | Only affects what your test sets up; needs the mock location permission and app selection |
| A real walk or drive | The only true end-to-end check of signal, accuracy and background behaviour | Slow, hard to repeat, and you cannot reproduce a failure on demand |
The emulator
The Android Emulator's extended controls (opens in a new tab) include a Location section with Single points and Routes tabs. You can search for a point and use Set location, build and save a route and use Play route, change the Playback speed, or use Load GPX/KML to replay a file.
From a terminal, the emulator console (opens in a new tab) accepts a geo fix command, which you can send with adb emu:
# longitude first, then latitude; altitude, satellites and velocity are optional
adb emu geo fix 28.9784 41.0082
Note the order: longitude comes before latitude, the reverse of how most people say coordinates. According to the documentation, the emulator supplies the location by creating a mock location provider.
A mock location app on a device
Android lets you pick one installed app as the system's mock location source. The Developer options documentation (opens in a new tab) describes the Select mock location app setting for testing how your app behaves in other locations. The next section walks through it.
Test providers in your own code
LocationManager (opens in a new tab) has test provider methods: addTestProvider, setTestProviderEnabled, setTestProviderLocation and removeTestProvider. They throw a SecurityException unless your app has been allowed to mock locations through the developer setting above.
val lm = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
// addTestProvider(...) is called first; see the LocationManager reference
// for the overload that fits your minimum API level.
lm.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true)
val fix = Location(LocationManager.GPS_PROVIDER).apply {
latitude = 41.0082
longitude = 28.9784
accuracy = 8f
time = System.currentTimeMillis()
elapsedRealtimeNanos = SystemClock.elapsedRealtimeNanos()
}
lm.setTestProviderLocation(LocationManager.GPS_PROVIDER, fix)
// when the test ends
lm.removeTestProvider(LocationManager.GPS_PROVIDER)
If your app uses the Fused Location Provider, the FusedLocationProviderClient (opens in a new tab) offers setMockMode(true), then setMockLocation(location). Two details from its documentation matter: entering mock mode clears cached locations, and the effect is global, applying to other processes and APIs such as geofencing, so set it back to false when you finish.
A real walk or drive
Keep at least one real outing in your plan. It is the only method that exercises real signal behaviour, indoor-to-outdoor transitions and background limits on the actual hardware.
How do I test with a mock location app on a physical device?
This walk-through uses Mock GPS, our own Android app, as the example. It supports Android 7.0 and later and is free with ads. Menu names vary between device makers, so treat the paths as a guide.
- Enable Developer options. Open Settings, find Build number (usually under About phone), and tap it seven times. Developer options then appears in Settings. The Android documentation (opens in a new tab) lists where Build number lives on several devices.
- Select the mock location app. In Developer options, open Select mock location app and choose Mock GPS. Open the app and grant the location and notification permissions it asks for; the overlay permission is optional and only needed for floating controls.
- Set a point. In Mock GPS, search for an address, enter coordinates or select a point on the map, then start the session. An ongoing notification keeps the session visible and includes a Stop control.
- Open your app and check it. Your app should now receive the test position. Check that the map, place name or nearby results match.
- Then a route. Mock GPS can build routes with several stops and waiting times, play them back at an adjustable speed with pause and resume, or move you with a joystick. It can also import and export GPX files, and you can adjust the update interval, the reported accuracy and a small amount of location jitter, which is useful for the accuracy cases above.
- Stop the session when you are done, and open your app again to confirm it returns to real location.
Mock GPS is a manual tool. It does not hide Android's mock-location flag and does not promise compatibility with every app, so an app that rejects mock locations will still reject it. The product page lists what the app does and what data it handles.
What goes wrong when testing locations?
- The app rejects mock locations. Android marks them:
Location.isMock()(opens in a new tab) was added in API level 31, andisFromMockProvider()is deprecated from API level 31 in its favour. The reference notes that a user may have a good reason to mock their location, so apps should reject mock locations only when real positions are essential. If you own the app, accept them in debug and test builds. - Mocking left switched on. After testing, other apps may keep seeing the test position. Stop the session, and if you used
setMockMode(true), set it back tofalse. - Stale cached locations. A last-known location can be of any age; the Fused Location Provider reference says to check the timestamp. If a result looks too good, remember that entering mock mode clears cached locations, so start each test from a known state.
- Emulator and device differ. The emulator is excellent for logic, but it has no real GNSS hardware. Re-run the important paths, especially background and permission flows, on a real device before you trust them.
- Unrealistic timing. A test location that jumps 20 kilometres in one second proves little. Give movement realistic intervals and speeds.
What should I check before a release?
- Denied, approximate-only and precise permission each give a sensible screen.
- First launch with no fix shows a waiting state that ends, with a timeout message if nothing arrives.
- Location services off produces a clear prompt, and turning them back on recovers.
- Accuracy of a few metres and of several hundred metres both behave correctly.
- A route with several legs updates progress, distance and arrival correctly.
- The app works in the background as designed, and stops requesting location when it should.
- Mock locations are accepted in test builds, and the production behaviour toward mock locations is a deliberate decision.
- Mock mode is off, no test provider remains registered and the mock location app is not selected on any device you hand back.
- At least one real outing has been done on a physical device.
Is it okay to fake my location?
Mock locations exist so that developers and testers can check software without travelling. Use them on apps you build or are authorised to test. Do not use them to misrepresent where you are to other people or to services that rely on honest location, which can breach those services' terms and, depending on the case, the law. If an app refuses mock locations, treat that as the owner's decision rather than something to work around.
QUESTIONS
Frequently asked questions
How do I fake a GPS location on an Android phone for testing?
Enable Developer options, choose a mock location app under Select mock location app, then start a location from that app. Any app that reads Android location can then receive the test position.
How do I set a location in the Android Emulator?
Open Extended controls and use the Location tab to set a single point or play a route, or run adb emu geo fix with longitude first and latitude second.
Why does my app ignore a mock location?
Some apps check Location.isMock() and reject locations that Android marks as mock. If the app is yours, accept them in test builds; if it is not, it may be intentionally refusing them.
Can I write an automated test that uses a fake location?
Yes. Add a test provider with LocationManager, or use setMockMode and setMockLocation on FusedLocationProviderClient. The app running the test must be selected as the mock location app.
Is it legal to use mock locations?
Using them to test your own app, or one you are authorised to test, is ordinary development practice. Using them to mislead another service or person about where you are is a different matter and may break that service's terms or the law.
FROM THE STUDIO
Mock GPS
A location testing tool for Android developers and QA teams. Test points, routes and joystick movement with visible session controls.
KEEP READING
More guides.
Puzzle strategy · 8 min read
Block puzzle strategy: keep the board open
A block puzzle is lost when one awkward piece has nowhere to go, not when moves run out. These habits keep the board open and the run alive.
Read the guide →Family and learning · 8 min read
Family games to play on one phone
A practical guide to games the whole family can play around a single phone: which kinds work, what to check first, how to run the evening, and ideas that need no app.
Read the guide →Family and learning · 10 min read
How to choose a math game for ages 8–12
Eight practical checks you can apply to any math game in about ten minutes, plus how to try one together and how our own game measures up, including the parts you may not like.
Read the guide →