Gradle/Maven配置国内镜像源(以Android Studio为例)

说明

Gradle源在国外,国内构建项目的时候经常报错连接超时,修改国内镜像可以解决。

方法

配置方式有仅对单个项目生效和对所有项目生效两种方式

对单个项目生效

1.打开Android Studio工程文件,找到build.gradle

2.使用文本编辑器打开,默认格式如下:

 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
42
43
44
45
46
47
48
49
50
51
52
53
// Top-level build file where you can add configuration options common to all sub-projects/modules.



buildscript {

    repositories {

        google()

        jcenter()



    }

    dependencies {

        classpath 'com.android.tools.build:gradle:3.5.1'



        // NOTE: Do not place your application dependencies here; they belong

        // in the individual module build.gradle files

    }

}



allprojects {

    repositories {

        google()

        jcenter()



    }

}



task clean(type: Delete) {

    delete rootProject.buildDir

}

3.修改为以下内容:

 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Top-level build file where you can add configuration options common to all sub-projects/modules.



buildscript {

    repositories {

        maven {

            url 'https://maven.aliyun.com/repository/google'

        }

        maven {

            url 'https://maven.aliyun.com/repository/public'

        }

        maven {

            url 'https://maven.aliyun.com/repository/jcenter'

        }

    }

    dependencies {

        classpath 'com.android.tools.build:gradle:3.5.1'



        // NOTE: Do not place your application dependencies here; they belong

        // in the individual module build.gradle files

    }

}



allprojects {

    repositories {

        maven {

            url 'https://maven.aliyun.com/repository/google'

        }

        maven {

            url 'https://maven.aliyun.com/repository/public'

        }

        maven {

            url 'https://maven.aliyun.com/repository/jcenter'

        }

    }

}



task clean(type: Delete) {

    delete rootProject.buildDir

}

对所有项目生效

1.打开系统用户的Gradle配置目录:C:\Users\xxx\.gradle

2.新建文件init.gradle(注意文件后缀名为gradle

 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
42
43
44
45
allprojects{

    repositories {

        def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/repository/public'

        def ALIYUN_JCENTER_URL = 'https://maven.aliyun.com/repository/jcenter'

        all { ArtifactRepository repo ->

            if (repo instanceof MavenArtifactRepository){

                def url = repo.url.toString()

                if (url.startsWith('https://repo1.maven.org/maven2')) {

                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."

                    remove repo

                }

                if (url.startsWith('https://jcenter.bintray.com/')) {

                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."

                    remove repo

                }

            }

        }

        maven {

            url ALIYUN_REPOSITORY_URL

            url ALIYUN_JCENTER_URL

        }

    }

}
使用 Hugo 构建
主题 StackJimmy 设计