Android Studio 配置国内镜像源、HTTP代理(详细步骤)
作者:mmseoamin日期:2024-02-04

一、简介

  • 本文只介绍单个项目配置,所有项目配置自行百度。

  • 新老版本 Android Studio 配置文件稍有不同。

    • pluginManagement 和 dependencyResolutionManagement 里面的 repositories 都需要填写。

    • 不同版本的 gradle 文件的 url 格式不一样。

    • 备用一套镜像地址,这套与下面的镜像地址不同,如果下面的案例中的不行可以替换成这份,注意 url 书写格式

      // 新版本 settings.gradle.kts
      maven { url url=uri ('http://maven.aliyun.com/nexus/content/groups/public/')}
      maven { url url=uri ('http://maven.aliyun.com/nexus/content/repositories/jcenter')}
      maven { url url=uri ('http://maven.aliyun.com/nexus/content/repositories/google')}
      maven { url url=uri ('http://maven.aliyun.com/nexus/content/repositories/gradle-plugin')}
      // 老版本 build.gradle
      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'}
      

      二、单个项目生效 - 新版本 Android Studio

      • 打开 Android Studio 工程文件,找到 settings.gradle.kts

        pluginManagement {
            repositories {
                google()
                mavenCentral()
                gradlePluginPortal()
            }
        }
        dependencyResolutionManagement {
            repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
            repositories {
                google()
                mavenCentral()
            }
        }
        rootProject.name = "My Application"
        include(":app")
        
      • 修改为

        pluginManagement {
            repositories {
                maven { url=uri ("https://www.jitpack.io")}
                maven { url=uri ("https://maven.aliyun.com/repository/releases")}
                maven { url=uri ("https://maven.aliyun.com/repository/google")}
                maven { url=uri ("https://maven.aliyun.com/repository/central")}
                maven { url=uri ("https://maven.aliyun.com/repository/gradle-plugin")}
                maven { url=uri ("https://maven.aliyun.com/repository/public")}
                google()
                mavenCentral()
                gradlePluginPortal()
            }
        }
        dependencyResolutionManagement {
            repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
            repositories {
                maven { url=uri ("https://www.jitpack.io")}
                maven { url=uri ("https://maven.aliyun.com/repository/releases")}
                maven { url=uri ("https://maven.aliyun.com/repository/google")}
                maven { url=uri ("https://maven.aliyun.com/repository/central")}
                maven { url=uri ("https://maven.aliyun.com/repository/gradle-plugin")}
                maven { url=uri ("https://maven.aliyun.com/repository/public")}
                google()
                mavenCentral()
            }
        }
        rootProject.name = "My Application"
        include(":app")
        

        三、单个项目生效 - 老版本 Android Studio

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

          // 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:4.1.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
          }
          
        • 修改为

          // Top-level build file where you can add configuration options common to all sub-projects/modules.
          buildscript {
              repositories {
                  // google()
                  // jcenter()
                  maven { url "https://www.jitpack.io"}
                  maven { url "https://maven.aliyun.com/repository/releases"}
                  maven { url "https://maven.aliyun.com/repository/google"}
                  maven { url "https://maven.aliyun.com/repository/central"}
                  maven { url "https://maven.aliyun.com/repository/gradle-plugin"}
                  maven { url "https://maven.aliyun.com/repository/public"}
              }
              dependencies {
                  classpath 'com.android.tools.build:gradle:4.1.1'
                  // NOTE: Do not place your application dependencies here; they belong
                  // in the individual module build.gradle files
              }
          }
          allprojects {
              repositories {
                  // google()
                  // jcenter()
                  maven { url "https://www.jitpack.io"}
                  maven { url "https://maven.aliyun.com/repository/releases"}
                  maven { url "https://maven.aliyun.com/repository/google"}
                  maven { url "https://maven.aliyun.com/repository/central"}
                  maven { url "https://maven.aliyun.com/repository/gradle-plugin"}
                  maven { url "https://maven.aliyun.com/repository/public"}
              }
          }
          task clean(type: Delete) {
              delete rootProject.buildDir
          }
          

          四、配置 HTTP 代理

          • 代理服务器的作用

            • 内容缓存:缓存经常访问的网页和数据,当再次访问时可以直接从代理服务器获取,加快访问速度。

            • 绕过限制:某些国外源在国内访问的速度极慢,通过代理可以绕过限制,实现跨地域访问。

            • 安全性:匿名浏览,数据加密

            • 配置 HTTP 代理【方式一】

              • 镜像地址

                腾讯: https://mirrors.cloud.tencent.com/AndroidSDK/
                阿里: https://mirrors.aliyun.com/android.googlesource.com/
                
              • 打开 Settings,搜索 HTTP Proxy,然后填入镜像地址,点击 Apply,在点击 OK。

                Android Studio 配置国内镜像源、HTTP代理(详细步骤),image.png,第1张

                重新启动 Android Studio 会弹出这个配置面板,可以填下面的常用代理服务器,任意选个。

                Android Studio 配置国内镜像源、HTTP代理(详细步骤),image.png,第2张

              • 配置 HTTP 代理【方式二】

                • 打开 Settings,搜索 HTTP Proxy,然后填下面的常用代理服务器,任意选个,点击 Apply,在点击 OK。

                  Android Studio 配置国内镜像源、HTTP代理(详细步骤),image.png,第3张

                • 国内常用的代理服务器(注意:只需要域名即可)

                  1、东软信息学院

                  http://mirrors.neusoft.edu.cn 端口:80
                  

                  2、北京化工大学

                  http://ubuntu.buct.edu.cn/ 端口:80
                  http://ubuntu.buct.cn/ 端口:80
                  http://ubuntu.buct6.edu.cn/ 端口:80
                  

                  3、中国科学院开源协会

                  http://mirrors.opencas.cn 端口:80
                  http://mirrors.opencas.org 端口:80
                  http://mirrors.opencas.ac.cn 端口:80
                  

                  4、上海GDG镜像服务器

                  http://sdk.gdgshanghai.com 端口:8000
                  

                  5、电子科技大学

                  http://mirrors.dormforce.net 端口:80
                  

                  6、腾讯 Bugly 镜像

                  http://android-mirror.bugly.qq.com 端口:8080