關於Gradle編譯時下載依賴失敗解決方法

weixin_33912445發表於2018-09-25

2018年9月18日 AndroidStudio終於更新到了3.2穩定版,但是更新之後新建專案總是會失敗,設定代理,重新清理各種操作基本上都嘗試遍了,也沒起到作用,花了六個小時,終於找到了解決方案,在project的build.gradle檔案中新增如下內容,瞭解AndroidStudio的看官應該一眼就明白了:

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

buildscript {
    repositories {
        def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public/'
        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/')) {
                    remove repo
                }
            }
        }
        maven {
            url REPOSITORY_URL
        }
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
        

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

allprojects {
    repositories {
        def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public/'
        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/')) {
                    remove repo
                }
            }
        }
        maven {
            url REPOSITORY_URL
        }
        google()
        jcenter()
    }
}

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

最後,特別鳴謝:https://blog.csdn.net/u013360850/article/details/60595210
阿里的倉庫地址:http://maven.aliyun.com/nexus/content/groups/public/
OSChina的倉庫地址:http://maven.oschina.net/content/groups/public/

相關文章