gradle无法更新

概述

gradle是android上使用的自动构建工具,属于apk文件编译工具链中必不可上的程序,但是在国内由于不可抗拒的原因会出现无法下载的问题。android开发的错误日志找不到了,但是表现在flutter上会出现如下情况:

flutter running gradle task 'assembleDebug'

然后等待很久以后,提示无法下载制定的文件。

解决办法

解决办法有两种:让gradle使用代理访问官方服务器或者直接使用镜像服务器:

方法一 使用代理工具访问官方服务器

gradle的配置有项目内的链接设置和全局的连接设置,这里使用全局设置:
打开$HOME/.gradle/gradle.properties文件

org.gradle.jvmargs=-DsocksProxyHost=127.0.0.1 -DsocksProxyPort=1080

以上是采用socks5的链接方式

方法二 使用镜像服务器

  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' }
        }
    }
  1. 修改项目内文件/ProjectName/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' }
    }
}
  1. 修改flutter sdk下文件FlutterSDK/packages/flutter_tools/gradle/flutter.gradle文件
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'
    }
}

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

参考链接

参考