Solution to slow download of gradle files in Android Studio

When creating a project in Android Studio, I often encounter a situation where the synchronization of gradle is very slow or directly stuck. Since the built-in Maven source address is foreign, the access speed of this Maven source in China is very slow. It can be solved by replacing the download source with Alibaba Cloud. Methods as below:

1. Only modify the current project configuration

After Android Studio loads the project, there is a build.gradle file in the root directory of the project. Add Alibaba Cloud's maven to the "repositories" node. After the modification, it is as follows:

buildscript {
  repositories {

      ///ADD START 新增的
      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' }
      ///ADD END

      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 {
      ///ADD START 新增的
      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' }
      ///ADD END
      google()
      jcenter()
  }
}

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

2. The global modification is effective for all projects

1. First find the directory where Android Studio stores gradle

Operate the menu item "File->Settings..." in Android Studio in turn, and the "Settings" dialog box will pop up. As follows:

image

image

Find and open the gradle configuration item in the "Settings" dialog box. "Gradle user home:" represents the directory where gradle is currently stored, as shown in the following figure:

image

 

As can be seen from the above figure, my current gradle configuration directory is:

C:\Users\Qiang\.gradle

2. Configure the Alibaba Cloud download source in the gradle configuration directory

Create a file init.gradle in the gradle configuration directory "C:\Users\Qiang.gradle" and save the following content:

allprojects{
    repositories {
        def MY_ALIYUN_REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public'
        def MY_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 $MY_ALIYUN_REPOSITORY_URL."
                    remove repo
                }
                if (url.startsWith('https://jcenter.bintray.com/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $MY_ALIYUN_JCENTER_URL."
                    remove repo
                }
            }
        }
        maven {
            url MY_ALIYUN_REPOSITORY_URL
            url MY_ALIYUN_JCENTER_URL
        }
    }
}

The following is my personal configuration, as shown below:

image

 

 

Restart Android Studio after the configuration is complete.

 

The big guys keep a concern before leaving, and follow-up wonderful articles continueimage

image

Guess you like

Origin blog.csdn.net/u011426115/article/details/112690643