Gradle在Mac上安装及Idea中配置

前言

笔者在安装Gradle的过程中发现过程有点繁琐且安装教程比较少,因此记录一下安装过程,方便同行参考

环境信息

macOS:10.15.5
IntelliJ IDEA:2020.3
Gradle:6.8.2
Java:1.8.0_151
Homebrew: 3.0.1

进入Gradle官网

点此进入Gradle官网

点击安装

Gradle在Mac上安装及Idea中配置

安装文档

Gradle在Mac上安装及Idea中配置

包管理安装

Gradle在Mac上安装及Idea中配置

安装brew

本次安装使用brew安装,brew可以简化 macOS 和 Linux 操作系统上软件的安装

//brew是ruby开发的,需要确认ruby是否已安装,默认是已经安装的
//检查ruby是否安装
huanliu@B-LW01J1WL-0028 ~ % ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin19]

//安装brew命令,此命令可以进入官网查看
//输入命令之后需要输入一次密码
//安装时间稍长,不要着急
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"


//检查brew是否安装成功
huanliu@B-LW01J1WL-0028 ~ % brew --version
Homebrew 3.0.1

Gradle在Mac上安装及Idea中配置

正式安装Gradle

//命令
//笔者安装需要10分钟左右,主要是下载资源时间较长,主要看网速了
brew install gradle

//检查是否安装成功
huanliu@B-LW01J1WL-0028 ~ % gradle -v

Welcome to Gradle 6.8.2!

Here are the highlights of this release:
 - Faster Kotlin DSL script compilation
 - Vendor selection for Java toolchains
 - Convenient execution of tasks in composite builds
 - Consistent dependency resolution

For more details see https://docs.gradle.org/6.8.2/release-notes.html


------------------------------------------------------------
Gradle 6.8.2
------------------------------------------------------------

Build time:   2021-02-05 12:53:00 UTC
Revision:     b9bd4a5c6026ac52f690eaf2829ee26563cad426

Kotlin:       1.4.20
Groovy:       2.5.12
Ant:          Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM:          15.0.1 (Oracle Corporation 15.0.1+9)
OS:           Mac OS X 10.15.5 x86_64

Idea中配置Gradle

gradle安装目录
Gradle在Mac上安装及Idea中配置
Idea配置

Gradle user home : 安装目录 + libexec
本例:/usr/local/Cellar/gradle/6.8.2/libexec

Gradle在Mac上安装及Idea中配置

Gradle镜像配置

//全局配置,在${USER_HOME}/.gradle/目录下创建 init.gradle 文件,添加如下内容即可
allprojects{
    repositories {
        def ALIYUN_REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public'
        def ALIYUN_JCENTER_URL = 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
        all { ArtifactRepository repo ->
            if(repo instanceof MavenArtifactRepository){
                def url = repo.url.toString()
                if (url.startsWith('https://repo1.maven.org/maven2')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
                    remove repo
                }
                if (url.startsWith('https://jcenter.bintray.com/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
                    remove repo
                }
            }
        }
        maven {
            url ALIYUN_REPOSITORY_URL
            url ALIYUN_JCENTER_URL
        }
    }
}

//单个项目,在项目中的 build.gradle 添加如下内容
buildscript {
    repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
                maven{ url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'

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

allprojects {
    repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven{ url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
    }
}