首先了解对于gradle工具配置gradle 缓存路径,就是gradle wrapper各个版本的jar 存储路径和下载的各工程的gradle依赖存储路径
1.直接到gradle 安装目录,编辑bin文件夹下的gradle
文件,然后找到如下语句:
# Add default JVM options here. You can also use JAVA_OPTS
and GRADLE_OPTS to pass JVM options to this script
在这句话的下面加上如下这一句:
GRADLE_OPTS=-Dgradle.user.home=/yourpath/gradle/gradle_cache
2.通过Android studio 找到gradle 缓存路径,直接修改路径
3.找到每一个工程的gralde.properties文件,添加 gradle.user.home=D:/Cache/.gradle
这样每一个工程都要配置一次很麻烦!!
不建议这么配置,gradle.properties 包含当前工程build tool 拉起gradle jvm 进程所有配置,网络代理配置,jvm参数等等
4.直接添加环境变量GRADLE_USER_HOME,windows通过控制面板操作;linux 在/etc/profile
或~/.bash_profile
增加如下
export GRADLE_USER_HOME=D:/Cache/.gradle
5.通过gradle 自带指令参数自我设置
gradle -g D:/Cache/.gradle build build
可以通过gradle --help
查看各参数的含义。
二、 gradle 缓存目录各个含义
caches 是缓存的gradle 工程依赖文件和module依赖文件
deamon是存储的对应wrapper的bin文件和log日志
wrapper是存储的gradle 各个版本的jar
jars-3存储的是工程gradle依赖文件;如android.jar,kotlin-android.jar,gradle-tool.jar,groovy.jar等(gralde是groovy写的)
modules-2存储的是每一个项目下面的应用具体依赖的jar包
三、 AS工程下gradle 相关各文件含义
gradle.propertie 中配置 gradle 网络代理,以及jvm 参数等
## Project-wide Gradle settings.
#
# 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.
# Default value: -Xmx1024m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
#
# 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
#Fri Dec 29 14:06:34 CST 2017
systemProp.http.proxyPort=8080
android.enableBuildCache=false
android.useDeprecatedNdk=true
org.gradle.daemon=true
systemProp.http.proxyUser=*****
org.gradle.parallel=true
systemProp.http.proxyPassword=*******
org.gradle.jvmargs=-Xmx4096m -XX\:MaxPermSize\=2048m -XX\:+HeapDumpOnOutOfMemoryError
systemProp.https.proxyHost=10.19.110.55
systemProp.https.proxyPassword=ydq@1104
org.gradle.configureondemand=true
systemProp.http.nonProxyHosts=*.sn.*****.ad|*.****.com|10.*
systemProp.https.nonProxyHosts=*.sn.****.ad|*.*****.com|10.*
systemProp.http.proxyHost=10.19.110.55
systemProp.https.proxyPort=8080
systemProp.https.proxyUser=*****
项目根目录下的build.gradle是配置项目依赖的gradle 版本,语言插件:kotlin-gradle-plugin等, 脚本如:
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
应用目录下build.gradle 配置的module依赖,脚本如:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
// Jetpack
def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
}
对于项目中已经导入的依赖dependencies{ classpath: XXX },在 应用中的build.gradle 直接使用,需要apply一下;
在应用中build.gradle 引入依赖 使用 implementation "包名:version"