Android Emulator Snapshot Restore Leaves GPS Location Unchanged
0 reputation · 04 Jan 2024, 20:42 UTC
0 reputation · 04 Jan 2024, 20:42 UTC
Goal: Obtain a deterministic emulator state after restoring a snapshot so that location‑dependent tests start from a known baseline.
Constraint: While the snapshot mechanism reverts most system settings, certain hardware sensor states such as GPS coordinates, battery level, and telephony signal strength are not automatically cleared, leading to potential test flakiness. The appropriate way to reset these values—whether via an emulator flag, a snapshot option, or a standardized adb routine—is not clearly documented.
Questions:
• Does the emulator provide a flag or snapshot option to force sensor state reset on restore?
• Is there a standardized adb command that clears all sensor‑related state without requiring hard‑coded values?
• Can snapshot metadata be configured to exclude sensor state from the saved VM image?
33275 reputation · 05 Jan 2024, 05:51 UTC
No. The Android Emulator does not provide a snapshot flag or option to force a sensor state reset on restore, there is no standardized adb command that clears all sensor-related state without supplying a value, and snapshot metadata cannot be configured to exclude host-side sensor state.
geo fix command feed a system-level override into Android's Location Manager.emu geo fix continue to be fed after restore.GPS coordinates, battery level and telephony signal strength are presented to the guest as emulated hardware. The emulator keeps the active override values outside the VM snapshot. On restore the VM state rolls back, but the host provider keeps pushing the previously set coordinates, so apps see the old location even though the snapshot itself is clean.
App-internal caches may also retain location. Those caches are inside the VM and are reverted by the snapshot, while the system provider remains unchanged. This mix is why tests appear flaky.
emulator -avd <name> -snapshot <name>.adb -s <serial> emu geo fix <longitude> <latitude> [<altitude>]
Example baseline:
adb -s emulator-5554 emu geo fix -122.084 37.422 0
Emulator console equivalent:
telnet localhost <console-port>
geo fix -122.084 37.422 0
To make the reset repeatable in CI, run the geo fix command after every snapshot restore and before launching the test suite. If you need “no location”, set a neutral baseline you control, e.g., 0 0, and treat that as the known state.
emu geo fix requires longitude and latitude. There is no emu geo clear or equivalent.Verification you can run locally:
adb shell dumpsys location. The restored VM will still see the last host-set coordinate.One missing diagnostic detail that changes the recommendation: Are you injecting location via the Extended Controls UI, via adb emu geo fix, or via an in-app mock location provider? The reset procedure differs if an app is using its own mock provider versus the emulator system override.
Use comments to ask for clarification. Post a solution as an answer.
33,275 reputation · 04 Jan 2024, 22:47 UTC
After restoring a snapshot, the emulator’s host‑side location provider continues to feed the last fix set via emu geo fix or the Extended Controls panel. To obtain a known baseline you can immediately issue a reset command, for example:
adb -s <serial> emu geo fix 0 0This forces the GPS to return (0°, 0°) until a new fix is injected. The same approach works for any coordinate you choose, and it does not affect other sensor states such as battery level, which must be reset separately via
adb shell dumpsys battery setif needed.