使用 Geolocation 插件编译时出错

Error in compilation with Geolocator pluggin

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

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.

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

尝试把targetSdkVersioncompileSdkVersion改成31.

别忘了检查一下:

app/src/build.gradle:

  • minSdkVersion 20

gradle.properties:

  • android.useAndroidX=真
  • android.enableJetifier=真

希望能帮到你!

我在使用 geolocator 7.7.0 时遇到了同样的问题。 Here
为了修复它,我将 build.gradle compileSdkVersion 从 29 更改为 31。这对我有用。

android {

    compileSdkVersion 31
    defaultConfig {
        minSdkVersion 21 
        targetSdkVersion 29
        ...
    }
...
}

我有同样的问题,我通过下载平台版本 sdkmanager "platforms;android-31" 修复,并更改 compileVersion 31、minSdkVersion 21 和 targetSdk 31。

android {
    compileSdkVersion 31

    ...

    defaultConfig {
        ...
        minSdkVersion 21
        targetSdkVersion 31
        ...
    }
}

在我的例子中,我将 compileSdkVersion 更改为 30

android {
   compileSdkVersion 30
   defaultConfig {
     minSdkVersion 21
     targetSdkVersion 29
   }
 }

You should pin the geolocator to version 7.6.2 (without using the caret ^ character) like so:

地理定位器:7.6.2 如果您不这样做,Flutter 仍会将地理定位器插件升级到版本 7.7.0。可以在此处找到更多信息:https://dart.dev/tools/pub/dependencies#caret-syntax