You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

集成easy_geofencing后geolocator依赖出现Android编译错误求助

解决Flutter集成easy_geofencing后的geolocator编译错误

问题背景

我有一个基于Flutter的项目,原本使用geolocator: ^9.0.2实现位置获取功能,核心定位代码如下:

Map mreturn;
bool serviceEnabled;
LocationPermission permission;

serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
  Location location = new Location();
  serviceEnabled = await location.serviceEnabled();
  if (!serviceEnabled) {
    serviceEnabled = await location.requestService();
    if (!serviceEnabled) {
      mreturn = {"status": 0, "message": 'Location Denied once'};
      return mreturn;
    }
  }
}

permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
  permission = await Geolocator.requestPermission();
  if (permission == LocationPermission.denied) {
    mreturn = {"status": 0, "message": 'Location permissions are denied'};
    return mreturn;
  }
}

if (permission == LocationPermission.deniedForever) {
  mreturn = {
    "status": 0,
    "message":
        'Location permissions are permanently denied, we cannot request permissions.'
  };
  return mreturn;
}

Position position = await Geolocator.getCurrentPosition();
mreturn = {"status": 1, "message": "Location Received", "position": position};
return mreturn;

集成easy_geofencing: ^0.2.0实现地理围栏功能后,出现如下编译错误:

/Users/lasthoney/.pub-cache/hosted/pub.dartlang.org/geolocator_android-2.1.0/android/src/main/java/com/baseflow/geolocator/permission/PermissionUtils.java:27: warning: [deprecation] getPackageInfo(String,int) in PackageManager has been deprecated
              .getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS);
              ^
error: warnings found and -Werror specified
1 error
1 warning

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':geolocator_android:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

解决方案

方法一:强制升级geolocator_android依赖版本

错误根源是easy_geofencing: ^0.2.0依赖的geolocator版本较低(^7.0.0),导致项目自动降级了geolocator_android到2.1.0,该版本包含已废弃的Android API方法,触发编译警告并被转为错误。

在项目的pubspec.yaml中添加依赖覆盖,强制使用兼容geolocator: ^9.0.2的geolocator_android版本:

dependency_overrides:
  geolocator_android: ^4.1.0

执行flutter pub get更新依赖后重新编译。

方法二:关闭Android编译的警告转错误机制

在项目的android/build.gradle文件中,修改lintOptions配置,禁止将警告视为错误:

android {
    // 保留原有其他配置
    lintOptions {
        abortOnError false
    }
}

方法三:升级easy_geofencing到最新版本

查看easy_geofencing的最新版本,若新版本已更新geolocator依赖到兼容版本,直接在pubspec.yaml中升级easy_geofencing的版本号,再执行flutter pub get即可解决依赖冲突问题。

内容的提问来源于stack exchange,提问作者Honey Last

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 08:25:33