解决Flutter编译一直显示Running Gradle task 'assembleDebug'

Android Stduio 创建 Flutter 项目的时候,会出现各种奇奇怪怪的坑,踩了一个有一个,最后编译的时候可能会出现一直显示 Running Gradle task 'assembleDebug' 然后就不动了,或者会显示 Could not resolve io.flutter 等问题,归根结底是网络无法访问到 Google 服务引起的,两种解决方案:

一. FQ,妥妥的解决问题;

二. 修改镜像;

具体步骤如下:

1. 找到 Flutte SDK 目录下的 Flutter 打包配置文件 flutter.gradle

路径为 flutter\packages\flutter_tools\gradle\flutter.gradle

  • 修改第1处配置

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.5.0'
    }
}
复制代码
  • 修改第2处配置

apply plugin: FlutterPlugin

class FlutterPlugin implements Plugin<Project> {
    // private static final String MAVEN_REPO      = "https://storage.googleapis.com/download.flutter.io";
    private static final String MAVEN_REPO = "https://storage.flutter-io.cn/download.flutter.io";

    // The platforms that can be passed to the `--Ptarget-platform` flag.
    private static final String PLATFORM_ARM32  = "android-arm";
    private static final String PLATFORM_ARM64  = "android-arm64";
    private static final String PLATFORM_X86    = "android-x86";
    private static final String PLATFORM_X86_64 = "android-x64";

    // The ABI architectures.
    private static final String ARCH_ARM32      = "armeabi-v7a";
    private static final String ARCH_ARM64      = "arm64-v8a";
    private static final String ARCH_X86        = "x86";
    private static final String ARCH_X86_64     = "x86_64";
复制代码
  • 修改第3处配置

void addFlutterDependencies(buildType) {
        String flutterBuildMode = buildModeFor(buildType)
        if (!supportsBuildMode(flutterBuildMode)) {
            return
        }
        String repository = useLocalEngine()
            ? project.property('local-engine-repo')
            : MAVEN_REPO

        project.rootProject.allprojects {
            repositories {
                maven {
                    url repository
                }

                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' }
            }
        }
复制代码

2. 修改 Flutter 项目下的 android 下的 build.gradle

buildscript {
    ext.kotlin_version = '1.3.50'
    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.5.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

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' }
    }
}
复制代码

修改完镜像之后,重新 build 就没问题了。

我来评几句
登录后评论

已发表评论数()

相关站点

+订阅
热门文章