Flutter 异常无法解析 com.google.android.gms:play-services-location:16.+

Flutter exception Could not resolve com.google.android.gms:play-services-location:16.+

伙计们

现在尝试模拟应用程序时,终端抛出异常,应用程序无法启动:

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:processDebugResources'.
> Could not resolve all task dependencies for configuration ':app:debugRuntimeClasspath'.
   > Could not resolve com.google.android.gms:play-services-location:16.+.
     Required by:
         project :app > project :location
      > Failed to list versions for com.google.android.gms:play-services-location.
         > Unable to load Maven meta-data from https://google.bintray.com/exoplayer/com/google/android/gms/play-services-location/maven-metadata.xml.
            > Could not get resource 'https://google.bintray.com/exoplayer/com/google/android/gms/play-services-location/maven-metadata.xml'.
               > Could not GET 'https://google.bintray.com/exoplayer/com/google/android/gms/play-services-location/maven-metadata.xml'. Received status code 502 from server: Bad Gateway

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 37s
Exception: Gradle task assembleDebug failed with exit code 1
Exited (sigterm)

Flutter 医生结果:

[√] Flutter (Channel stable, 1.22.5, on Microsoft Windows [versão 10.0.19041.1348], locale pt-BR)
 
[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[√] Android Studio (version 3.6)
[√] Connected device (1 available)

我已经尝试替换 * project / android / build.gradle * 中的“jcenter” * 传递 * mavenCentral * 如下,据报道 and here,但那没有'解决问题

repositories {
    google()
    mavenCentral()
    maven {
        url "https://maven.google.com"
    }
}

我强调一下,这个项目是正常运行的,这个时候就发生了。

恳请各位帮忙解决

在尝试再次模拟该应用程序时,在终端上显示错误之前,它向我显示了以下消息:“插件项目:location_web 未找到。请更新 settings.gradle。 “和 I found that in my project the error was caused by the location package, which has a dependency on location_web

解决方案 也就是说,在他们解决问题之前,解决方案是不使用“位置”包并将其替换为没有此 location_web 依赖项的包。我使用“geolocator”包完成了它,在我的例子中,我需要它来获取地理坐标并调用另一个返回地址数据的函数,如下所示:

在 pubspec.yaml 上:

# location: ^3.0.0
geolocator: '6.2.1'

然后我执行了命令:

flutter pub get

在 AndroidManifest 文件中,我添加了:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>

对于我需要的,即获取地理坐标,我使用“geolocator”按如下方式完成,替换了之前使用“location”包的代码:

import 'package:geolocator/geolocator.dart';

Future _obtemCoordenadas() async {
    LocationPermission permissao;
    Position _dadosLocalizacao;

    await Geolocator.requestPermission();

    permissao = await Geolocator.checkPermission();
    if (permissao == LocationPermission.denied) {
      permissao = await Geolocator.requestPermission();
      if (permissao == LocationPermission.denied) {
        return;
      }
    }

    if (permissao == LocationPermission.deniedForever) {
      return;
    }

    if (permissao == LocationPermission.always) {
      _dadosLocalizacao = await Geolocator.getCurrentPosition();
      var latitude = _dadosLocalizacao.latitude;
      var longitude = _dadosLocalizacao.longitude;
      localizacao.latitude = latitude.toString();
      localizacao.longitude = longitude.toString();
    }
  }

作为切换到另一个位置插件(可能需要大量工作和测试)的替代方法,我找到了一个解决方法。下面列出的步骤适用于 IntelliJ IDEA,在其他 IDE 中看起来可能略有不同。

  1. 在项目工具 window(其中列出了您的文件)中,一直向下滚动直到找到 Flutter Plugins。如果您没有看到它,请确保 Project 在项目工具 window.
  2. 顶部的下拉列表中 selected
  3. 打开Flutter plugins,找到location-4.0.0并打开(location-之后的版本号可能不同,没关系)。
  4. 打开文件location-4.0.0/android/build.gradle
  5. 找到行api 'com.google.android.gms:play-services-location:16.+'
  6. 改为api 'com.google.android.gms:play-services-location:16.0.0'。如果您的编辑说该文件不属于您的项目,select "I want to edit this file anyway".

您现在应该可以构建应用程序了。

此更改当然是一个丑陋的 hack,如果您更新 location 插件,您的更改将被覆盖。但至少在 Bintray 再次上线(或直到您有时间迁移到另一个位置插件)之前,您将能够进行构建。