- 查看所有的project
this.getProjects()
def getProjects() {
println '-----------------------'
println 'projects'
println '-----------------------'
this.getAllprojects().eachWithIndex { Project project, int index ->
if(index == 0) {
println "Root project: '${project.name}'"
} else {
println "+-------- project: '${project.name}'"
}
}
}
- getParentProject
this.getParentProject()
def getParentProject() {
def parent = this.getParent()
if (parent == null) {
println 'the parent is null '
} else {
println "+++++++++++++++++ the parent name is : ${parent.name}"
}
}
- 配置制定的project
project('app') { Project project ->
apply plugin: 'com.android.application'
group 'com.jiangshuai'
version '1.0.1'
dependencies {}
android {}
}
- 配置所有project
allprojects {
group 'com.jiangshuai'
version '1.0.1'
}
- 只配置子工程
subprojects { Project project ->
if (project.plugins.hasPlugin('com.android.library')) {
}
}
- model直接依赖根目录下某文件
apply from: rootProject.file('common.gradle')
apply from: this.file('common.gradle')
- gradle.properties文件变量使用
if (hasProperty('isLoadTest') ? isLoadTest.toBoolean() : false) {
include ':test'
}
- 获取当前目录下文件的内容
def getFileContent(String path) {
try {
def file = file(path)
return file.text
} catch (GradleException e) { //file()方法在当前project目录下找,找不到会报FileNotFound异常
println "File $path not found"
}
return null
}
- sourceSets
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
main {
java {
srcDirs += []
}
kotlin {
srcDirs += ['src/main/kotlin', 'src/main/java']
}
}
}
- applicationVariants
this.afterEvaluate {
this.android.applicationVariants.all { variant ->
def name = variant.name
def output = variant.outputs.first()
println "the name is ${name}"
println "this apk out path is: ${output}"
}
}
- file树的遍历
//文件树的遍历
fileTree("src/main/java") {//基于基准目录创建文件树
FileTree fileTree ->
fileTree.visit {//访问文件树的元素
FileTreeElement element ->
println "the file name is :" + element.file.name
}
}
- file输出gradle.properties的内容
//在根工程中
println "gradle.properties file content is :" + getFileContent('gradle.properties') //输出gradle.properties的内容
def getFileContent(String path) {
try {
def file = file(path)
return file.text
} catch (GradleException e) { //file()方法在当前project目录下找,找不到会报FileNotFound异常
println "File $path not found"
}
return null
}
- copy file
//文件的拷贝,讲解Groovy语法之文件操作中通过读取文件中的内容,然后写入到目标文件实现
//Gradle提供了更加简便的方法:copy()
//app module的build.gradle文件中
copy {
from file('build/outputs/') //可以是文件也可以是目录
into getRootProject().getBuildDir().path + '/rootOutputs/' //目标路径
include '**/*.apk' //选择要复制的文件
include '**/*.json' //选择要复制的文件
exclude { detail -> detail.file.name.contains('json') } //排除
rename { 'aaa3.apk'} //重命名
}
- 添加file tree依赖
implementation fileTree(include: ['*.jar'], dir: 'libs') //添加文件树依赖(本地的jar包),另外还可以添加file和files依赖,取决于添加的依赖是文件树还是单一文件或者多个文件
- 在gradle中执行task,task中执行exec command命令
task(name: 'fileCopy') {
doLast {
def sourcePath = this.projectDir+ '/librarytest1'
println "sourcePath is : ${sourcePath}"
def desationPath = '/Users/jiangshuai/Documents/11'
def command = "mv -f ${sourcePath} ${desationPath}"
exec {
try {
executable 'bash'
args '-c', command
println '------------- the file exec success'
} catch (GradleException e) {
println 'the command exec file failed'
}
}
}
}
- 解决依赖冲突
//版本依赖冲突解决
//1. 强制依赖指定版本
configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:26.1.0'
}
//完整写法,排除multidex
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '26.1.0'
}
}
}
}
//2. 排除
implementation ('com.squareup.retrofit2:adapter-rxjava:2.1.0'){
exclude group: 'io.reactivex' //排除指定包下的所有库
}
implementation 'io.reactivex.rxjava2:rxjava:2.0.6'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
//另外如果冲突的是其中的一个module
implementation ('com.squareup.retrofit2:adapter-rxjava:2.1.0'){
exclude module: '冲突的module name' //排除指定module
}
//传递依赖
//假设有工程A,B,C,工程A依赖了B,而B又依赖了C,这个时候就构成了传递依赖
//而在开发中一般是不允许A直接使用C的,因为可能B升级了之后,不需要再依赖C了,如果在A中直接使用了工程C中的东西的话,就会导致各种编译错误,因为B已经没有依赖C了,在A中使用的C中的东西都已经找不到了
conpile ('com.squareup.retrofit2:adapter-rxjava:2.1.0'){
transitive false //禁止传递依赖
}
//在Gradle中默认transitive就是false,另外在Android Studio 2.x 版本和3.x依赖指令的区别部分,我们也可以知道,使用implementation,本身也是非传递依赖,在B中使用implementation依赖C,A再依赖B,本身C中的API那些就无法在A中直接使用
- task高阶用法
task taskA {
doLast {
println 'taskA run'
}
}
task taskB {
doLast {
println 'taskB run'
}
}
task taskC(dependsOn:[taskA,taskB]) { //为taskC配置依赖
doLast {
println 'taskC run'
}
}
//动态的指定依赖
task taskA(group:'gradle'){
doLast {
println 'taskA run'
}
}
task taskB(group:'gradle'){
doLast {
println 'taskB run'
}
}
task taskC(group:'java'){
doLast {
println 'taskC run'
}
}
task taskD { //动态的指定task依赖
dependsOn this.tasks.findAll {task -> task.group == 'gradle' }
doLast {
println 'taskD run'
}
}
- 单元测试
testImplementation 'junit:junit:4.12' //单元测试
- 查看某model的依赖
./gradlew :app:model1:dependencies
- 查看某工程的所有依赖
./gradlew buildEnv -S
- 在某文件中查看某关键字
find . -name '*.gradle' | grep -rn "27.1-android"
参考资料:
https://blog.csdn.net/qq282330332/article/details/90963422
;
https://www.jianshu.com/p/b437558df369
;
https://docs.gradle.org/current/dsl/org.gradle.api.tasks.SourceSet.html
版权声明:本文为j18874964028sss原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。