学而实习之 不亦乐乎

Gradle 使用国内源(阿里云源)

2023-09-28 08:12:24

Gradle在国内经常抽风,一直使用梯子来使用倒是没有出现什么问题,今天梯子被墙了,Gradle就用不了。最近好像查的有点严,估计梯子一时好不了,试试换gradle国内源。没有想到换了国内源比用梯子更快,故特在此记录一下。

一、在项目中进行修改

1、关闭Gradle的代理(如果没有设置代理跳过这一步)

当打开 Android Studio 项目下的文件 gradle.properties,大概可以看到下面的内容,然后注释设置代理的行。

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx512m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app"s APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn

#systemProp.http.proxyHost=127.0.0.1
#systemProp.http.proxyPort=1081
#systemProp.https.proxyHost=127.0.0.1
#systemProp.https.proxyPort=1081

2、更换Gradle国内源

打开项目根目录的 build.gradle,注释 google() 和 jcenter(),添加阿里云的源。

注意:Android Studio 和 IDEA 中对于此文件及其内容虽然有一点点不同,但是不用太在意,其实这里修改的只有文件中 repositories 部分,找到这部分进行修改就可以了。

修改如下:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    
    repositories {
       // google()
       // jcenter()
        maven { url 'https://plugins.gradle.org/m2/' }
        maven { url 'https://maven.aliyun.com/nexus/content/repositories/google' }
        maven { url 'https://maven.aliyun.com/nexus/content/groups/public' }
        maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
        

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
      //  google()
      //  jcenter()
        maven { url 'https://plugins.gradle.org/m2/' }
        maven { url 'https://maven.aliyun.com/nexus/content/repositories/google' }
        maven { url 'https://maven.aliyun.com/nexus/content/groups/public' }
        maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

二、全局修改

找到或者创建(我这里是没有的,需要创建) C:/User/Administor/.gradle/init.gradle 文件。

修改/添加 init.gradle 文件内的 repositories 配置

allprojects {
    repositories {
        maven { url 'https://plugins.gradle.org/m2/' }
        maven { url 'https://maven.aliyun.com/nexus/content/repositories/google' }
        maven { url 'https://maven.aliyun.com/nexus/content/groups/public' }
        maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
    }
}

根据阿里云官方教程,如果想使用 maven.aliyun.com 提供的其它代理仓,以使用 spring 仓为例,代码如下:

allProjects {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/public/' }
        maven { url 'https://maven.aliyun.com/repository/spring/'}
        mavenLocal()
        mavenCentral()
    }
}