上节咱们讲到project的属性相关的一些东西,今天学习project剩余部分android
文件常见操做相关api,相比之下,这部份内容就比较简单一些了git
//获取根工程的文件路径
println "root path:"+getRootDir().absolutePath
//获取build文件路径
println "build path:"+ getBuildDir().absolutePath
//当前工程文件路径
println "this project path:"+getProjectDir().absolutePath
复制代码
//调用方法
println getContent('common.gradle')
def getContent(String path){
try{
//返回多个文件路径
def file1 =files(path)
//至关于从当前project进行查找
def file =file(path)
//查找到后获取文件内容
return file.text
}catch (GradleException e){
println "file is not found..."
}
return null
}
复制代码
file方法和new一个file方法有什么不一样?github
其实没有什么不一样,都是传递一个file文件路径,
使用file方法则不需传入一个绝对路径,能够自动转化为至关路径
复制代码
from:文件(夹)来源路径
into:文件(夹)拷贝目的路径
复制代码
copy{
from file('build/outputs/apk/')
into getRootProject().getBuildDir().path+'/apk/'
exclude{}
rename{}
}
复制代码
fileTree('build/outputs/apk/'){
FileTree fileTree->fileTree.visit{
FileTreeElement element->
println 'the file name is :'+element.file.name
copy{
from element.file
into getRootProject().getBuildDir().path+'/test/'
}
}
}
复制代码
以上的属性或者操做都只能在咱们根工程中进行,没法跨工程api
说到依赖,咱们不得不提buildscript这个核心方法
经过查看buildscript源码,咱们能够知道它的闭包参数是一个ScriptHandler,发现两个很眼熟的方法bash
//配置工程的仓库地址 RepositoryHandler
void repositories(Closure configureClosure)
//配置工程“插件地址”
void dependencies(Closure configureClosure)
复制代码
buildscript{
ScriptHandler script->
script.repositories{
RepositoryHandler repository->
repository.jcenter()
repository.mavenCentral()
repository.movenLocal()
repository.maven(){
//设置组织名字
name 'personal'
//仓库地址
url 'http://localhost:8081:/nexus/repositories'
//设置帐户密码
credentials{
username='admin'
password='123456'
}
}
}
script.dependencies{
classpath 'com.android.tools.build:gradle:2.2.2'
}
}
复制代码
可简写为咱们很是熟悉的一个写法闭包
buildscript{
repositories{
jcenter()
mavenCentral()
movenLocal()
maven(){
name 'personal'
url 'http://localhost:8081:/nexus/repositories'
credentials{
username='admin'
password='123456'
}
}
}
dependencies{
//依赖gradle所须要的第三方插件
classpath 'com.android.tools.build:gradle:2.2.2'
}
}
复制代码
若是在app.build中单独的dependencies依赖,则表示的是为该应用程序添加第三方库的依赖,常见的依赖形式有app
compile fileTree(include:['*.jar'].dir:'libs')
复制代码
compile rootproject.ext.dependence.libSupportV7
复制代码
compile 'com.github.chrisbanes:PhotoView:1.3.0'
复制代码
经常在使用的时候常常会出现依赖冲突的问题,所以致使编译时候就就报错,此时咱们就须要移除冲突的依赖库,须要经过使用exclude进行排除操做
好比解决v4包的冲突,咱们能够采用下面的两种方式(选一便可)maven
compile 'com.github.chrisbanes:PhotoView:1.3.0'{
exclude module:'support-v4'
transitive false //禁止传递依赖
}
复制代码
compile 'com.github.chrisbanes:PhotoView:1.3.0'{
exclude group:'com.android.support'
}
复制代码
经常使用的依赖有compile和provided,那二者的区别在哪?ide
compile:会将第三方依赖中的全部资源和文件都会打入到apk中
provided:战略编译,只会在在编译时运行,真正在打包后可能出现资源丢失致使没法运行,若是主工程和库文件中都引用到同一个依赖,此时就能够使用,有利于减少apk的体积
复制代码
以文件移动为例:学习
task(name:'apkcopy'){
doLast{
//gradle的执行阶段执行
def sourcePath=this.buildDir.path+'/outputs/apk'
def desationPath='/users/lcf/Downloads/'
def command="mv -f ${sourcePath} ${desationPath}"
exec{
try{
executable 'bash'
args '-c' command
println 'the command is success'
}catch(GradleException e){
println 'command is failed'
}
}
}
}
复制代码
最后在生成apk文件后,再执行./gradlew apkcopy 就能够将文件移动到指定位置了
到这里project相关的内容大致就告一段落了,下面进行总结