使用 Geolocator 插件编译时出错

Fel*_*iro 19 dart-pub android-studio flutter flutter-dependencies

使用Geolocator 插件编译项目时出现当前错误:

C:\src\flutter\.pub-cache\hosted\pub.dartlang.org\geolocator_android-2.0.0\android\src\main\java\com\baseflow\geolocator\location\LocationMapper.java:29: error: cannot find symbol
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                                                    ^
  symbol:   variable S
  location: class VERSION_CODES
C:\src\flutter\.pub-cache\hosted\pub.dartlang.org\geolocator_android-2.0.0\android\src\main\java\com\baseflow\geolocator\location\LocationMapper.java:30: error: cannot find symbol
      position.put("is_mocked", location.isMock());
                                        ^
  symbol:   method isMock()
  location: variable location of type Location
2 errors

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.
Run Code Online (Sandbox Code Playgroud)

我已经创建了另一个项目来测试是否会发生错误,并且在导入包时也会发生错误。我按照gradle.properties配置并将compileSdkVersion设置为30并检查了Flutter Doctor,一切正常,但错误仍然存​​在。有谁知道可能是什么原因造成的?

Con*_*nce 37

我在使用 geolocator 7.7.0 时也遇到了同样的问题。为了
解决这个问题,我将 build.gradlecompileSdkVersion 从 29 更改为 31。这对我有用。

android {

    compileSdkVersion 31
    defaultConfig {
        minSdkVersion 21 
        targetSdkVersion 29
        ...
    }
...
}
Run Code Online (Sandbox Code Playgroud)


小智 8

尝试将targetSdkVersioncompileSdkVersion更改为31。

不要忘记检查一下:

应用程序/src/build.gradle:

  • minSdkVersion 20

gradle.属性:

  • android.useAndroidX=true
  • android.enableJetifier=true

我希望能帮助你!


小智 6

就我而言,我将compileSdkVersion更改为30

android {
   compileSdkVersion 30
   defaultConfig {
     minSdkVersion 21
     targetSdkVersion 29
   }
 }
Run Code Online (Sandbox Code Playgroud)

您应该将地理定位器固定到版本 7.6.2(不使用脱字符号 ^ 字符),如下所示:

geolocator:7.6.2 如果不这样做,Flutter 仍会将 geolocator 插件升级到版本 7.7.0。更多信息可以在这里找到: https: //dart.dev/tools/pub/dependency#caret-syntax