gradle下載依賴jar包慢

引言:
我們在Android Studio開發過程中,Gradle編譯顯得尤其重要。特別是我們在下載依賴架包的時候,由於國內“閉關鎖國”比較嚴重,導致下載速度很慢,甚至提示架包找不到,如下面的日誌。既然國外的不可靠,國人還是有很多具有當擔的人,一如阿里。替換成阿里鏡像,就可以完美解決問題。親測有效

Execution failed for task ':app:lint'.
> Could not resolve all files for configuration ':app:lintClassPath'.
   > Could not download  XXX.jar (com.android.tools.lint:lint-api:26.1.2)
      > Could not get resource 'https://dl.google.com/dl/android/maven2/com/android/tools/lint/lint-api/26.1.2/lint-api-26.1.2.jar'.
         > Could not GET 'https://dl.google.com/dl/android/maven2/com/android/tools/lint/lint-api/26.1.2/lint-api-26.1.2.jar'.
            > Remote host closed connection during handshake

一 . 單個項目配置
找到對應工程的build.gradle,在buildscript和allprojects中都添加上阿里的maven鏡像

buildscript {
    repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin' }
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
    }
}
allprojects {
    repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin' }
        google()
        jcenter()
    }
}

2.所有項目配置
找到C:\Users\xxx.gradle文件夾,一般就是系統盤C 用戶下面,在裏面新建一個init.gradle,把以下代碼複製到文件中

allprojects{
    repositories {
        def ALIYUN_REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public'
        def ALIYUN_JCENTER_URL = 'http://maven.aliyun.com/nexus/content/repositories/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
        }
    }
}

參考文章:
https://www.jianshu.com/p/6dd91849138b

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章