Gradle 配置

下载Gradle

https://gradle.org/releases/

https://services.gradle.org/distributions/gradle-4.4.1-bin.zip

 

配置Gradle

windows

 1.新建环境变量 GRADLE_HOME,即 D:\usr\local\gradle-2.4

2.修改环境变量 Path,即追加 %GRADLE_HOME%\BIN;

3.输入快捷键 WIN+ R,输入 cmd,在弹出的窗口输入命令行: gradle -v

 

Mac:

第一步, 就是先download最新版本的gradle,网址如下:

http://gradle.org/gradle-download/

然后将下载下来的zip包放解压到本地任意的路径上,

例如,我本地则安装在

/Users/lixiang/gradle-2.7 

1、打开Mac上的“终端”,输入以下命令,将gradle的bin目录添加至到环境变量中:


vi ~/.bash_profile




2、打开.bash_profile 文件窗口依次输入以下命令:

i (进入vi的编辑模式,添加如下配置)

GRADLE_HOME=/Users/lixiang/gradle-2.7

export GRADLE_HOME

export PATH=$PATH:$GRADLE_HOME/bin

3、输入完毕,按esc键退出vi的编辑模式,

输入“:”(冒号)进入最后行模式,

输入 wq 保存并退出vi

4、通过以下命令来查看是否安装成功:

gradle -version  

如果输出以下的gradle版本信息就表示已经安装成功了:

------------------------------------------------------------

Gradle 2.7


------------------------------------------------------------


Build time:   2015-09-14 07:26:16 UTC


Build number: none


Revision:     c41505168da69fb0650f4e31c9e01b50ffc97893


Groovy:       2.3.10


Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013


JVM:          1.8.0_40 (Oracle Corporation 25.40-b25)


OS:           Mac OS X 10.10.4 x86_64







注意: 如果提示没有gradle命令,则可以检查:



1. GRADLE_HOME路径在环境变量中配置是否正确,

2.确定是否指定到bin目录;

 

Linux:

 

下载并解压




[html] view plain copy 
1.cd /usr/local  
2.wget https://services.gradle.org/distributions/gradle-2.8-bin.zip  
3.unzip gradle-2.8-bin.zip  
4.mv gradle-2.8 gradle28  
5.rm -rf gradle-2.8-bin.zip   







添加环境变量




[html] view plain copy 
1.vim /etc/profile  


在profile文件中添加



[html] view plain copy 
1.export GRADLE_HOME=/usr/local/gradle28  


并且在PATH路径后添加bin路径:



[html] view plain copy 
1.:$GRADLE_HOME/bin  








使配置文件生效:




[html] view plain copy 
1.source /etc/profile  




查看gradle版本




[html] view plain copy 
1.[root@BLBBST013 local]# gradle -version  
2.  
3.------------------------------------------------------------  
4.Gradle 2.8  
5.------------------------------------------------------------  
6.  
7.Build time:   2015-10-20 03:46:36 UTC  
8.Build number: none  
9.Revision:     b463d7980c40d44c4657dc80025275b84a29e31f  
10.  
11.Groovy:       2.4.4  
12.Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013  
13.JVM:          1.7.0_76 (Oracle Corporation 24.76-b04)  
14.OS:           Linux 2.6.32-358.el6.x86_64 amd64  


配置成功

 

 

修改Gradle缓存文件夹路径的方法

1.设置GRADLE_USER_HOME环境变量:D:\AndroidTools\gradle

/etc/profile~/.bash_profile增加如下:
export GRADLE_USER_HOME=D:/Cache/.gradle

 

使用国内镜像阿里云

全局配置USER_HOME/.gradle/文件夹下,创建文件init.gradle

allprojects{
    repositories {
        def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public/'
        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/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."
                    remove repo
                }
            }
        }
        maven {
            url REPOSITORY_URL
        }
    }
}

 

常见错误解决

1.启动Android Studio后一直卡组refreshing中,原因是下载Gradle太慢。

手动下载:http://services.gradle.org/distributions/gradle-4.4.1-bin.zip

删除D:\AndroidTools\gradle\wrapper\dists\gradle-4.4.1-bin\esnon9g7th47txqoadk69q7bv目录中文件,并将gradle-4.4.1-bin.zip放入。

 

2.Gradle sync failed: No cached version of com.neenbedankt.gradle.plugins:android-apt:1.8 available for offline mode.

取消离线模式

 

2.Gradle project sync failed.

1.如果是导入的项目可以先删除 项目名.iml

 

posted @ 2017-12-29 18:11  昨日微风  阅读(4510)  评论(1编辑  收藏  举报