Gradle 也可以用下面的方式声明使用的插件:

1
2
3
4
// build.gradle
plugins {
id 'com.example.plugin', version '1.0'
}

其实是从 Gradle 官方的插件仓库 https://plugins.gradle.org/m2/ 下载的。

但是,众所周知的原因,某些地区会连不上,导致下载不到需要的插件,例如出现如下错误:

1
2
3
4
5
6
7
* What went wrong:
A problem occurred configuring root project 'MyApp'.
> Could not resolve all files for configuration ':classpath'.
> Could not download jimfs.jar (com.google.jimfs:jimfs:1.1)
> Could not get resource 'https://plugins.gradle.org/m2/com/google/jimfs/jimfs/1.1/jimfs-1.1.jar'.
> Could not HEAD 'https://plugins.gradle.org/m2/com/google/jimfs/jimfs/1.1/jimfs-1.1.jar'.
> Connect to d29vzk4ow07wi7.cloudfront.net:443 [d29vzk4ow07wi7.cloudfront.net/54.192.84.6, d29vzk4ow07wi7.cloudfront.net/54.192.84.168, d29vzk4ow07wi7.cloudfront.net/54.192.84.128, d29vzk4ow07wi7.cloudfront.net/54.192.84.173] failed: Read timed out

又或者,插件是不对外的,存在某个私有仓库的,该如何修改或者添加额外的私有仓库地址呢?

直接修改根目录(root project)的settings.gradle来添加其它仓库:

1
2
3
4
5
6
7
8
9
// settings.gradle
pluginManagement {
repositories {
maven {
url 'http://examle.com/maven-repo'
}
gradlePluginPortal()
}
}

比如使用阿里云提供的镜像

1
2
3
maven {
url 'https://maven.aliyun.com/repository/gradle-plugin'
}

当然你也可以使用老的方式应用插件:

1
2
3
4
5
6
7
8
9
10
11
12
buildscript {
repositories {
maven {
url 'http://examle.com/maven-repo'
}
}
dependencies {
classpath 'com.example.plugin:gradle-plugin:1.0'
}
}

apply plugin: 'com.example.plugin'

references: