• 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.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    问题分析

    根据错误提示,是这个库依赖了其他三方库,从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/google'
            }
            maven {
                url 'https://maven.aliyun.com/repository/gradle-plugin'
            }
      }
      repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    打开项目根目录的build.gradle

    buildscript {
        repositories {
            maven {
                url 'https://maven.aliyun.com/repository/public/'
            }
            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/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/google'
            }
            maven {
                url 'https://maven.aliyun.com/repository/gradle-plugin'
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

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

    替换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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    问题分析

    根据错误提示,找到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/google'
            }
            maven {
                url 'https://maven.aliyun.com/repository/gradle-plugin'
            }
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:4.1.0'
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    参考:Cannot override Gradle repositories in transitive plugin dependencies

  • 相关阅读:
    ABB PU515A 3BSE032401R1 自动化控制模块
    NAACL最佳方法论文:课本上的A*搜索算法可以提升文本生成效果!
    【面试必刷TOP101】链表中环的入口结点 & 链表中倒数最后k个结点
    科普一下:拍抖音需要什么设备,可能用到的设备合集
    12 个优化 Docker 镜像安全性的技巧
    JVM中的栈和栈帧
    如何申请文心一言接口
    Jetson平台180度鱼眼相机畸变校正调试记录
    kubernetes/cluster/addons/fluentd-elasticsearch
    计算机毕业设计(86)php小程序毕设作品之求职招聘小程序系统
  • 原文地址:https://blog.csdn.net/adojayfan/article/details/126269390