555 words
3 minutes
安卓手机root虚拟定位代码阅读+原理解析(支持微信小程序)

实验环境#

我的。亲测:所有定位想看到效果都需要打开摇杆遥一下才会重定位,百度地图会检测提示“没有网络”,腾讯地图ok,微信定位ok,微信实时定位会闪退,微信小程序ok

Pixel 6
安卓15(API35)
kernelSU 11928
lsposed 1.10.1(7115)

github🔗: https://github.com/GLORIA925/Portal 原作者似乎给仓库闭源了

源码阅读#

The LSPosed plugin in this project achieves virtual positioning by hooking into Android’s location system APIs at the system service level using the Xposed/LSPosed framework. Here’s how the principle works based on the source code:


1. System Service Hooking (Xposed/LSPosed)#

  • The plugin does not integrate location spoofing into an app directly. Instead, it uses the Xposed/LSPosed framework to hook system-level location services, so that any app requesting location data receives the “virtual” (fake) location instead.

  • This is described in the README.md:

    The virtual positioning module based on LSPosed only provides Hook system services to achieve virtual positioning, and cannot be integrated into the APP.


2. Core Implementation Details#

a. Location Data Interception and Injection#

  • The file BaseLocationHook.kt is central. When an app requests the device location, the hook intercepts this request and creates a new Location object, filling it with fake data (latitude, longitude, accuracy, speed, bearing, etc.) from the FakeLoc object.

  • Example (simplified):

    if (!FakeLoc.enable)
        return originLocation
    
    val location = Location(originLocation.provider ?: LocationManager.GPS_PROVIDER)
    location.latitude = FakeLoc.latitude
    location.longitude = FakeLoc.longitude
    location.altitude = FakeLoc.altitude
    // ... more attributes set here
    
  • The FakeLoc singleton manages the fake location parameters (latitude, longitude, altitude, speed, bearing, accuracy, etc.).

b. Making “Realistic” Fake Locations#

  • The fake location data is not static. To avoid detection, the plugin can add jitter (random small movement) to the location using functions like jitterLocation() and can move the location smoothly with moveLocation().
    • See FakeLoc.kt:
      fun jitterLocation(...) : Pair<Double, Double> { ... }
      fun moveLocation(...) : Pair<Double, Double> { ... }
      
  • These functions use trigonometric calculations to simulate realistic movement and randomness in the reported location.

c. Remote/Programmatic Control#

  • The plugin can be controlled remotely (e.g., by developer tools) to update or move the virtual location, as handled in RemoteCommandHandler.kt. For example, commands like set_location, move, set_speed will update the FakeLoc parameters.

d. Provider and NMEA Spoofing#

  • The plugin also handles advanced aspects like faking NMEA sentences (data from GPS chips) and changing the reported provider (e.g., spoofing GPS vs. network location).
  • This ensures even apps that check for low-level GPS details can be fooled.

3. Why Does This Work?#

  • Android apps trust the location returned by the system location service.
  • By using Xposed/LSPosed, this plugin replaces the data at the system service layer, before it ever reaches the app.
  • Thus, all apps (unless they use hardware sensors directly) will see the fake data.

4. Key Files Involved#

  • xposed/src/main/java/moe/fuqiuluo/xposed/BaseLocationHook.kt – hooks and replaces system location data.
  • xposed/src/main/java/moe/fuqiuluo/xposed/utils/FakeLoc.kt – manages and generates fake location data.
  • xposed/src/main/java/moe/fuqiuluo/xposed/RemoteCommandHandler.kt – allows remote or programmatic updates to the fake location.

参考链接🔗#

[基于LSPosed的系统级修改定位模块][https://www.bilibili.com/video/BV1gzRMYDET8/]

[Portal:Android 虚拟定位开源神器,支持通过摇杆控制位置移动,并允许用户在设置中调整速度、高度、精度和方向等参数][https://www.appmiu.com/30792.html/comment-page-1]

https://github.com/ZCShou/GoGoGo

安卓手机root虚拟定位代码阅读+原理解析(支持微信小程序)
https://zycreverse.netlify.app/posts/fuckgps/
Author
会写点代码的本子画手
Published at
2025-07-28