Flutter配置Gradle镜像,解决国内下载慢问题

Flutter配置Gradle镜像,解决国内下载慢问题

未优化之前

程序运行时会卡在Running Gradle task 'assembleDebug'...如下提示信息

Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Launching lib\main.dart on sdk gphone64 x86 64 in debug mode...
Running Gradle task 'assembleDebug'...

此时Gradle正在下载gradle构建工具以及相关依赖库,由于官方gradle默认配置的依赖下载服务器处于国外,国内用户访问非常缓慢,常常一个依赖下载就要花半天时间,好在国内有阿里、清华等镜像源,我们可以配置镜像代理加快依赖的下载

优化步骤

修改build.gradle

进入项目目录的android/build.gradle,修改为如下,注意不要注释掉google()mavenCentral(),因为部分依赖可能镜像源没有,gradle找不到的话会报错

buildscript {
    ext.kotlin_version = '1.7.10'
    repositories {
        maven { url 'https://maven.aliyun.com/repository/public/' }
        maven { url 'https://maven.aliyun.com/repository/spring/'}
        maven { url 'https://maven.aliyun.com/repository/google/'}
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin/'}
        maven { url 'https://maven.aliyun.com/repository/spring-plugin/'}
        maven { url 'https://maven.aliyun.com/repository/grails-core/'}
        maven { url 'https://maven.aliyun.com/repository/apache-snapshots/'}
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/public/' }
        maven { url 'https://maven.aliyun.com/repository/spring/'}
        maven { url 'https://maven.aliyun.com/repository/google/'}
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin/'}
        maven { url 'https://maven.aliyun.com/repository/spring-plugin/'}
        maven { url 'https://maven.aliyun.com/repository/grails-core/'}
        maven { url 'https://maven.aliyun.com/repository/apache-snapshots/'}
        google()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

修改gradle-wrapper.properties

进入android/wrapper/gradle-wrapper.properties文件

distributionUrl的值用镜像源替代,需要注意的是后面的gradle版本需要和替换镜像源前的gradle版本一致

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-7.5-all.zip

你可能感兴趣的:(Flutter,flutter)