一、项目目录的路径获取

  1. println "the root file path is"+ getRootDir().absolutePath
  2. println "the build file path is"+getBuildDir().absolutePath
  3. println "the project file path is"+ getProjectDir().absolutePath

image.png

二、获取文件内容

  1. println getContent('local.properties')
  2. def getContent(String path){
  3. try{
  4. def file=file(path)
  5. return file.text
  6. }
  7. catch(GradleException e){
  8. println 'file not found'
  9. }
  10. return null
  11. }

跟New File一样,好处是无需传绝对路径,只需要传项目跟目录下得路径即可(相对路径)

三、拷贝文件

  1. copy{
  2. from file("app/build/outputs/apk/")
  3. into getRootProject().getBuildDir().path +"/apk/"
  4. exclude{} //排除某些文件
  5. rename{} //重命名
  6. }

可以用来打包后拷贝apk

四、对文件树进行遍历

  1. //对文件树进行遍历
  2. fileTree('build'){FileTree fileTree->
  3. fileTree.visit{ FileTreeElement element->
  4. println "this file name is :" + element.file.name
  5. }
  6. }