对于Gradle来说带build.gradle文件的就是一个project
Project Api组成
gradle生命周期api
project相关api
管理父project及子project
task相关api
新增task,以及使用其他peoject中已有task
属性相关api
修改属性
file相关api
操作当前project下文件
其他api
project相关api
this.getProject
this.getSubProject
this.getParenProject
this.getRootProject
project(‘app’){Project project->
applyplugin:’com.android.application’
group‘com.imooc’
version‘1.0.0-release’
dependencies{
}
android{
}
}
allproject{
}
subproject{
}
问题:
如果根project跟子project有区别的话,以谁为准,执行顺序是怎样的
属性:
DEFAULT_BUILD_FILE = “build.gradle: //默认build文件
PATH_SEPARATOR = “:” //分隔符
DEFAULT_BUILD_DIR_NAME = “build” //默认输出路径
GRADLE_PROPERTIES = “gradle.properties”
扩展属性
示例
ext {
configuration = [
buildToolsVersion: "26.0.2",
compileVersion : 26,
minSdk : 15,
targetSdk : 24,
versionCode : 9,
versionName :"1.0.8",
]
libraries = [
supportVersion : "26.1.0",
retrofit :"2.2.0",
rxjava :"2.0.1",
rxandroid :"2.0.1",
rxpermissions :"0.9.4@aar",
okhttp3_logging :"3.4.0-RC1",
okhttp3 :"3.4.1",
glide :"4.0.0",
butterknife :"8.8.1",
rxlifecycle :"2.2.0",
permissionsdispatcher: "2.3.2",
constraint_layout :"1.0.2",
glide_transformations: "3.0.1",
rxjava2_adapter :"1.0.0",
banner :"1.4.5",
systembartint :"1.0.3",
swipeback :"1.0.1",
SmartRefreshLayout :"1.0.2-alpha-7",
brvah :"2.9.30",
objectbox :"1.2.1",
tablayout :"1.0.4",
md_dialog :"0.9.0.2",
tablayout : "1.0.4",
loading :"1.1.0",
logging :"2.0.3",
rxhttputils :"2.0.3",
loadinglayout :"1.3.1",
extextview :"1.0.2",
flowlayout :"1.1.2",
greendao :"3.2.0",
number_progress :"1.4@aar",
takephoto :"4.0.3",
floatingactionbutton :"1.10.1",
]
}
脚本引入
apply from: "config.gradle"
apply from:this.file('common.gradle')
通过gradle.properties配置
仅支持key=value形式
文件相关API
路径获取相关api
getRootDir()
getBuildDir()
getProjectDir()
文件操作相关api (gradle提供的api只允许在根工程下操作,不能操作文件系统)
定位
file(filepath)
拷贝
copy{ //支持文件和文件夹的整体拷贝
from file(‘filename’)
into getRootProject().getBuildDir()
exclude{}
rename{}
}
文件树遍历
fileTree(‘build/outputs/apk/’){FileTree filetree ->
fileTree.visit{FileTreeElement element ->
}
}
依赖相关api
buildscript { ScriptHandler scriptHandler->
//配置工程仓库地址
scriptHandler.repositories { RepositoryHandler repositoryHandler ->
repositoryHandler.jcenter()
repositoryHandler.mavenCentral()
repositoryHandler.mavenLocal()
repositoryHandler.maven {
url 'http://localhost:8081:/nexus/repositories/'
name 'personal'
credentials{
username = 'admin'
password = 'admin'
}
}
}
//配置工程'插件'依赖地址
scriptHandler.dependencies { //gradle本身对插件的依赖
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
}
}
Project中的
dependencies{ //应用程序中对第三方的依赖
apifileTree(include: ['*.jar', '*.aar'], dir: 'libs')
implementation 'com.google.android.material:material:1.1.0'
apiproject(':basic')
compile files('libs/push-6.0.1.aar'){
exclude module:’suppor-v4’
exclude group ‘com.android.support’
transitive false //是否传递依赖
}
provided() //占位编译,只参与编译,不参数打包,适用于只在编译阶段起作用的库,有效减少包的体积
}
PS:
可省略参数,是因为使用了闭包的delegete,忘了,重回基本语法学习
aar,安卓中特有,可理解未带资源的jar包
执行外部命令:
task(name : 'apkcopy'){ //固定写法
def sourcePath = this.buildDir.path + '/outputs/apk'
def desationPath = '/Users/Downloads/'
def command = "mv -f ${sourcePath} ${desationPath}"
exec{
try{
executable 'bash'
args '-c',command
}catch(GradleException e){
}
}
}
网友评论