Gradle/Maven配置国内镜像源(以Android Studio为例)
如默
撰写于 2019年 10月 18 日

说明

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

方法

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

对单个项目生效

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

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

// 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.修改为以下内容:

// 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

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
        }
    }
}

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

温馨提示:

本文最后更新于2019年10月18日,已超过1960天没有更新,若内容或图片失效,请留言反馈。

说明

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

方法

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

对单个项目生效

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

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

// 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.修改为以下内容:

// 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

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
        }
    }
}


赞 (0)

猜您想看

  • CSS动态获取视口尺寸

    今天看到一篇CSS动态获取视口尺寸的文章记录一下

    2024年11月14日
  • 微信开放平台账号注销方法

    最近需要注册一个公众号,发现邮箱都被占用了,需要将微信开放平台的邮箱释放,但是官方没有注销的入口,几经周折,最后终于注销,特此记录。

    2022年08月05日
  • elementUI中upload组件隐藏上传框,限制上传格式和大小,预览

    在用elementUI的时候,需要用到Upload上传组件,当list-type为picture-card时,即使写了limit为1,但还是会出现一个新的上传框,本文记录解决方法。

    2024年06月28日
  • 哔哩哔哩视频下载方式

    最近需要下载哔哩哔哩的视频,发现很多网页版的工具都带有不良插件,遂使用手机版进行下载,特此记录。

    2020年02月14日
  • 关于Mac电脑是否需要安装清理软件的思考

    Mac电脑是否需要安装类似Windows上电脑管家、安全卫士之类的软件一直饱受争议,本文就这个问题说一下看法。

    2023年10月19日
  • Python实现Gitee码云webhook

    之前使用Vuepress做文档网站,每次更新之后都需要手动构建前端页面,繁琐,不方便,想起来webhook,自动配置之后发现还是方便,记录一下。

    2023年11月23日

评论区(暂无评论)

这里空空如也,快来评论吧~

我要评论

Vaptcha 初始化中...