preface

This is actually a tutorial on bad streets. Baidu on a pile of solutions, easy to use few. So I decided to review it. Recently I wrote several Flutter add-ons, many people said that they could not download the Android plugin. As a result, there was a lot of confusion and confusion. Gradle dependencies do not need to be scaled, but the download speed is not very good, and some networks are not very good, so you need to use a domestic image to solve this problem. Of course, there may be drawbacks to using a domestic mirror, such as not updating in a timely manner, but it does solve the problem.

A simple game

The easy way to do this is simply to modify the build.gradle directory in your project root directory:

// Maven library def cn = allProjects {repositories {Google () jCenter ( "http://maven.aliyun.com/nexus/content/groups/public/" def abroad = "http://central.maven.org/maven2/" // Maven {url cn artifactUrls abroad}}} maven {url cn artifactUrls abroad}}Copy the code

Senior play

It might be a bit cumbersome to configure every project once, so we can write an initialization script for the initial Gradle. Create a new init.gradle file under ~/. Gradle/(Windows default is C:\Users\UserName\.gradle) and type the following:

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
        }
    }
}
Copy the code

Of course, you can also put this script in the build.gradle directory at the root of your project. Above, the next time build will fly like.

  • Reference: Gradle official documentation init_scripts.