Flutter和Android中覆盖gradle中的repositories仓库地址

简介: Flutter和Android中覆盖gradle中的repositories仓库地址

覆盖Android三方库的仓库地址

问题场景

flutter中使用了一个定位权限库location_permissions,运行发现报以下错误。

* What went wrong:
A problem occurred configuring project ':location_permissions'.
> Could not resolve all artifacts for configuration ':location_permissions:classpath'.
   > Could not resolve org.ow2.asm:asm-util:6.0.
     Required by:
         project :location_permissions > com.android.tools.build:gradle:3.5.0
         project :location_permissions > com.android.tools.build:gradle:3.5.0 > com.android.tools.build:builder:3.5.0
         project :location_permissions > com.android.tools.build:gradle:3.5.0 > com.android.tools.build.jetifier:jetifier-processor:1.0.0-beta04
      > Could not resolve org.ow2.asm:asm-util:6.0.
         > Could not get resource 'https://jcenter.bintray.com/org/ow2/asm/asm-util/6.0/asm-util-6.0.pom'.
            > Could not HEAD 'https://jcenter.bintray.com/org/ow2/asm/asm-util/6.0/asm-util-6.0.pom'.
               > The server may not support the client's requested TLS protocol versions: (TLSv1.2, TLSv1.3). You may need to configure the client to allow other protocols to be used. See: https://docs.gradle.org/7.2/userguide/build_environment.html#gradle_system_properties
                  > Remote host terminated the handshake
> Failed to notify project evaluation listener.
   > Could not get unknown property 'android' for project ':location_permissions' of type org.gradle.api.Project.
   > Could not get unknown property 'android' for project ':location_permissions' of type org.gradle.api.Project.

问题分析

根据错误提示,是这个库依赖了其他三方库,从jcenter仓库下载失败了。

但是已经在android中替换了默认的jcenter仓库和maven仓库,为什么还会从jcenter下载呢。

我们打开的location_permissions的Github仓库,打开android目录下的build.gradle


看到这里就明白了,原来插件的android配置使用了jencter仓库,所以和插件有关的依赖都会从jencter里面下载。

解决方案

接下来我们来覆盖掉三方库中的repositories引用。

打开settings.gradle.

添加以下内容

dependencyResolutionManagement {
  repositories {
        maven {
            url 'https://maven.aliyun.com/repository/public/'
        }
        maven {
            url 'https://maven.aliyun.com/repository/central/'
        }
        maven {
            url 'https://maven.aliyun.com/repository/google/'
        }
        maven {
            url 'https://maven.aliyun.com/repository/gradle-plugin/'
        }
  }
  repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
}

打开项目根目录的build.gradle

buildscript {
    repositories {
        maven {
            url 'https://maven.aliyun.com/repository/public/'
        }
        maven {
            url 'https://maven.aliyun.com/repository/central/'
        }
        maven {
            url 'https://maven.aliyun.com/repository/google/'
        }
        maven {
            url 'https://maven.aliyun.com/repository/gradle-plugin/'
        }
    }
  ......
}

allprojects {
    buildscript {
      repositories {
            maven {
                url 'https://maven.aliyun.com/repository/public/'
            }
            maven {
                url 'https://maven.aliyun.com/repository/central/'
            }
            maven {
                url 'https://maven.aliyun.com/repository/google/'
            }
            maven {
                url 'https://maven.aliyun.com/repository/gradle-plugin/'
            }
      }
  }
    repositories {
        maven {
            url 'https://maven.aliyun.com/repository/public/'
        }
        maven {
            url 'https://maven.aliyun.com/repository/central/'
        }
        maven {
            url 'https://maven.aliyun.com/repository/google/'
        }
        maven {
            url 'https://maven.aliyun.com/repository/gradle-plugin/'
        }
    }
}

再次运行,三方库中的依赖就能正常下载了,不会报下载失败的错误。

替换flutter sdk的repository

问题场景

再次运行,还是发现以下错误

* Where:
Build file '/Users/ado/Developer/project/x_creator/android/app/build.gradle' line: 33

* What went wrong:
A problem occurred evaluating project ':app'.
> Could not resolve all artifacts for configuration 'classpath'.
   > Could not resolve com.google.flatbuffers:flatbuffers-java:1.12.0.
     Required by:
         unspecified:unspecified:unspecified > com.android.tools.build:gradle:4.1.0
      > Could not resolve com.google.flatbuffers:flatbuffers-java:1.12.0.
         > Could not get resource 'https://repo.maven.apache.org/maven2/com/google/flatbuffers/flatbuffers-java/1.12.0/flatbuffers-java-1.12.0.pom'.
            > Could not HEAD 'https://repo.maven.apache.org/maven2/com/google/flatbuffers/flatbuffers-java/1.12.0/flatbuffers-java-1.12.0.pom'.
               > The server may not support the client's requested TLS protocol versions: (TLSv1.2, TLSv1.3). You may need to configure the client to allow other protocols to be used. See: https://docs.gradle.org/7.2/userguide/build_environment.html#gradle_system_properties
                  > Remote host terminated the handshake
   > Could not resolve org.tensorflow:tensorflow-lite-metadata:0.1.0-rc1.
     Required by:
         unspecified:unspecified:unspecified > com.android.tools.build:gradle:4.1.0
      > Could not resolve org.tensorflow:tensorflow-lite-metadata:0.1.0-rc1.
         > Could not get resource 'https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-metadata/0.1.0-rc1/tensorflow-lite-metadata-0.1.0-rc1.pom'.
            > Could not GET 'https://repo.maven.apache.org/maven2/org/tensorflow/tensorflow-lite-metadata/0.1.0-rc1/tensorflow-lite-metadata-0.1.0-rc1.pom'.
               > The server may not support the client's requested TLS protocol versions: (TLSv1.2, TLSv1.3). You may need to configure the client to allow other protocols to be used. See: https://docs.gradle.org/7.2/userguide/build_environment.html#gradle_system_properties
                  > Remote host terminated the handshake

问题分析

根据错误提示,找到app/build.gradle,在第33行发现以下引用。

Android端run或者compile时,会调用flutter sdk中的gradle仓库配置去下载对应的依赖。

然后我们根据这个地址去flutter sdk的对应目录下的flutter.gradle,发现了以下仓库配置。刚好对应了上面报错信息中的gradle版本号4.1.0。

解决方案

我们只需要把google和mavenCentral()换成国内的仓库镜像即可。

buildscript {
    repositories {
        //google()
        //mavenCentral()
        maven {
            url 'https://maven.aliyun.com/repository/public/'
        }
        maven {
            url 'https://maven.aliyun.com/repository/central/'
        }
        maven {
            url 'https://maven.aliyun.com/repository/google/'
        }
        maven {
            url 'https://maven.aliyun.com/repository/gradle-plugin/'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0'
    }
}

参考:Cannot override Gradle repositories in transitive plugin dependencies

相关文章
|
1月前
|
Android开发
Android基于gradle task检查各个module之间资源文件冲突情况
Android基于gradle task检查各个module之间资源文件冲突情况
Android基于gradle task检查各个module之间资源文件冲突情况
|
1月前
|
Android开发
Android gradle task任务检查各个module之间资源文件冲突.md
Android gradle task任务检查各个module之间资源文件冲突.md
Android gradle task任务检查各个module之间资源文件冲突.md
|
1月前
|
Java Android开发 Windows
玩转安卓之配置gradle-8.2.1
为安卓开发配置Gradle 8.2.1,包括下载和解压Gradle、配置环境变量、修改配置文件以增加国内镜像,以及在Android Studio中配置Gradle和JDK的过程。
73 0
玩转安卓之配置gradle-8.2.1
|
3月前
|
Java 开发工具 Android开发
解决flutter doctor出现Android license status unknown或cmdline-tools component is missing
解决flutter doctor出现Android license status unknown或cmdline-tools component is missing
79 4
解决flutter doctor出现Android license status unknown或cmdline-tools component is missing
|
3月前
|
Android开发
Android Studio: 解决Gradle sync failed 错误
本文介绍了解决Android Studio中出现的Gradle同步失败错误的步骤,包括从`gradle-wrapper.properties`文件中获取Gradle的下载链接,手动下载Gradle压缩包,并替换默认下载路径中的临时文件,然后重新触发Android Studio的"Try Again"来完成同步。
1306 0
Android Studio: 解决Gradle sync failed 错误
|
3月前
|
XML 安全 Android开发
Flutter配置Android和IOS允许http访问
Flutter配置Android和IOS允许http访问
115 3
|
3月前
|
Android开发 Kotlin
The Android Gradle plugin supports only Kotlin Gradle plugin version 1.3.10 and higher
The Android Gradle plugin supports only Kotlin Gradle plugin version 1.3.10 and higher
85 3
|
3月前
|
Java Android开发
添加Flutter模块到已有项目(Android)
添加Flutter模块到已有项目(Android)
46 3
|
3月前
|
开发工具 Android开发
Flutter: Android SDK not found at this location,Android Studio not found at xxx
Flutter: Android SDK not found at this location,Android Studio not found at xxx
157 2
|
3月前
|
Java 开发工具 Android开发
Android Studio利用Build.gradle导入Git commit ID、Git Branch、User等版本信息
本文介绍了在Android Studio项目中通过修改`build.gradle`脚本来自动获取并添加Git的commit ID、branch名称和用户信息到BuildConfig类中,从而实现在编译时将这些版本信息加入到APK中的方法。
71 0