1. 程式人生 > 實用技巧 >Android Studio配置使用阿里雲的映象

Android Studio配置使用阿里雲的映象

單獨工程配置

一、刪除 Android Studio 的代理設定

首先你需要確認你已經在 Settings -> Appearance&Behavior -> System Settings -> HTTP Proxy 中選中了 No Proxy。 然後找到專案根目錄下的 gradle.properties 檔案,開啟檢視該檔案中是否有關於 proxy 設定(代理的地址和埠)的相關語句,刪除這些內容 最後,你需要找到你的另一個 gradle.properties 檔案:C:\Users\Administrator.gradle\gradle.properties,刪除與 proxy 設定相關的語句

二、讓專案通過阿里雲 maven jcenter 下載依賴資源

開啟專案根目錄下的 build.gradle 新增阿里 maven 庫地址:
buildscript {
    repositories {
        // 新增阿里雲 maven 地址         
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
        // jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
allprojects {
    repositories {
        // 新增阿里雲 maven 地址         
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
        // jcenter()
        google()
    }
}

三、重新 SYNC 專案,編譯時的資源下載一般就會如絲般順滑了。

全域性配置

一、在C:\Users\使用者名稱\.gradle中新建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
        }
    }
}

二、重啟Android Studio 並clean rebuild 即可