cisen / blog

Time waits for no one.
134 stars 20 forks source link

maven jcenter #40

Open cisen opened 6 years ago

cisen commented 6 years ago

// 指定源 maven { url 'https://maven.fabric.io/public' } maven { url 'http://s3.amazonaws.com/fabric-artifacts/public' }

cisen commented 6 years ago

jcenter 下载慢、超时问题解决

在使用 Gradle 构建工程时,jcenter 下载慢、超时的解决方法。

方法一: https 改成 http 协议下载,修改项目根目录下 build.gradle 文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
//        jcenter()
        jcenter(){url 'http://jcenter.bintray.com/'}
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
//        jcenter()
        jcenter(){url 'http://jcenter.bintray.com/'}
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

方法二: 切换到国内的Maven镜像仓库。 修改项目根目录下 build.gradle 文件,将 jcenter() 或者 mavenCentral() 替换掉即可。

repositories {
    maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
       maven{ url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
}

或全局修改,USER_HOME/.gradle/ 文件夹下新建 init.gradle 文件,添加如下内容:

allprojects{
    repositories {
        def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/repositories/central/'
        all { ArtifactRepository repo ->
            if(repo instanceof MavenArtifactRepository){
                def url = repo.url.toString()
                if (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."
                    remove repo
                }
            }
        }
        maven {
            url REPOSITORY_URL
        }
    }
}

init.gradle 文件其实是 Gradle 的初始化脚本 (Initialization Scripts),也是运行时的全局配置。

cisen commented 5 years ago

Mac系统默认下载到:/Users/(用户名)/.gradle/caches/modules-2/files-2.1 或者相似目录

Windows系统默认下载到:C:\Users(用户名).gradle\caches\modules-2\files-2.1 或者相似目录