Android Studio Gradle Method for Replacing Alibaba Cloud Image

  • 2021-12-05 07:34:12
  • OfStack

When developing with Android Studio, you often encounter compilation jamming problems because Gradle downloads dependent resources too slowly. I can't help it. If there is the Great Wall, I still have to change the mirror image.

Again, this is a common problem, and we hope to configure it globally. Add the init. gradle file under the. gradle (refer to C:\ Users\ username\. gradle for the path) directory as follows:


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') || url.startsWith('http://repo1.maven.org/maven2')) {
          project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
          remove repo
        }
        if (url.startsWith('https://jcenter.bintray.com/') || url.startsWith('http://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
    }
  }


  buildscript{
    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') || url.startsWith('http://repo1.maven.org/maven2')) {
            project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
            remove repo
          }
          if (url.startsWith('https://jcenter.bintray.com/') || url.startsWith('http://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
      }
    }
  }
}

To configure a single project, you can add the following code to the build. gradle file under the project root:


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' }

Done, download speed flies ~


Related articles: