Flutter 一直卡在 Running Gradle task ‘assembleDebug‘...

从原理上彻底解决该问题(2023-10-26 更新)百分之百成功,至于什么原理一篇文章说不清楚

Flutter项目中,项目的依赖大概分成三种:

场景一: flutter sdk自带依赖

场景二: flutter 第三方插件(不带原生交互),如:dio  get 等插件

场景三: flutter 第三方插件(带原生交互) ,如 fluttertoast、connectivity_plus等插件

针对【场景一】和【场景二】这两种只需要配置如下,就可以加速下载依赖

先配置pub相关的环境变量

PUB_HOSTED_URL=https://pub.flutter-io.cn
FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

再修改项目中的android/build.gradle仓库地址,将如下配置

buildscript {
    ext.kotlin_version = '1.7.10'
    repositories {
        google()
        jcenter()
}
    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

 替换成

buildscript {
    ext.kotlin_version = '1.7.10'
    repositories {
        maven { url 'https://maven.aliyun.com/repository/public/' }
        maven { url 'https://maven.aliyun.com/repository/google/' }
        maven { url 'https://maven.aliyun.com/repository/central' }
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
        google()
        jcenter()
}
    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/google/' }
        maven { url 'https://maven.aliyun.com/repository/central' }
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
        google()
        jcenter()
    }
}

这样就能解决场景一和场景二

针对于【场景三】

 需要配置gradle全局代理国内镜像

进入到 C:\Users\admin\.gradle 目录下(我电脑当前登录用户名字就叫 “admin”, 其他人需要进入到自己电脑登录的对应用户目录下,如果没有.gradle目录,需要新建),之后再新建init.gradle 文件,文件内容为:

allprojects {
    repositories {
        maven {
            url 'https://maven.aliyun.com/repository/public/'
        }
        maven {
            url 'https://maven.aliyun.com/repository/google/'
        }
 
        all { ArtifactRepository repo ->
            if (repo instanceof MavenArtifactRepository) {
                def url = repo.url.toString()
 
                if (url.startsWith('https://repo.maven.apache.org/maven2/') || url.startsWith('https://repo.maven.org/maven2') 
                    || url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com/')
                    || url.startsWith('https://maven.google.com/') || url.startsWith('https://dl.google.com/dl/android/maven2/')) {
                    //project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."
                    remove repo
                }
            }
        }
    }
 
    buildscript {
        repositories {
            maven{ url 'https://maven.aliyun.com/repository/public/'}
            maven{ url 'https://maven.aliyun.com/repository/google/'}
 
            all { ArtifactRepository repo ->
                if (repo instanceof MavenArtifactRepository) {
                    def url = repo.url.toString()
                    if (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com/')
                         || url.startsWith('https://dl.google.com/dl/android/maven2/')) {
                        //project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."
                        remove repo
                    }
                }
            }
        }
    }
 
}

这样就完成了

注意:为了以后开发项目,需要把场景一、场景二、场景三对应的配置都配置上,这样在也不用担心下载不了依赖了

你可能感兴趣的:(flutter)