下載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
