Solutions to initializing gradle or resolving dependencies timeout in flutter

Time:2019-8-14

Overview of the problem

After executing flutter run, the card is initializing gradle or resolving dependencies for a long time, and then an error is reported.

This situation is usually caused by the inability to obtain relevant resources from Google and JCenter, and gradle can not access resources successfully even when it configures agents because it bypasses system agents (not verified).

Solution

This solution has only been used on mac, but Windows and Linux operate the same way. In the case of unscientific Internet access, you can replace Google and JCenter with Aliyun’s source, and you can modify one of the following two files (or both).

Modify build. gradle in the project directory

Modifying build. gradle in a project can modify the source used by the current project gradle in the following locations:

Your flutter project root directory / Android / gradle / build. gradle

Make the following modifications:

buildscript {
    repositories {
        // google()
        // jcenter()
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/jcenter' }
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
    }
}

allprojects {
    repositories {
        // google()
        // jcenter()
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/jcenter' }
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
    }
}
...

Modify flutter. gradle

Alternatively, you can modify the gradle file in the flutter SDK directly, as follows:

Your flutter SDK root directory / packages / flutter_tools / gradle / flutter. gradle

Make the following modifications:

import ...
...
buildscript {
    repositories {
        // google()
        // jcenter()
        maven { 
            url'https://maven.aliyun.com/repository/google' 
        }
        maven{
            url 'https://maven.aliyun.com/repository/jcenter'
        }
        maven{
            url 'http://maven.aliyun.com/nexus/content/groups/public'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'
    }
}
...