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

文章目录

  • 覆盖Android三方库的仓库地址
    • 问题场景
    • 问题分析
    • 解决方案
  • 替换flutter sdk的repository
    • 问题场景
    • 问题分析
    • 解决方案

覆盖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
Flutter和Android中覆盖gradle中的repositories仓库地址_第1张图片
看到这里就明白了,原来插件的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

你可能感兴趣的:(Android,Studio,错误大全,Flutter踩坑集合,android,flutter,gradle,jcenter,maven)