我正在尝试将ANT构建中的任务转换为Gradle:

<target name="index-assets" depends="copy-assets">
    <path id="assets.path">
        <!-- contexts (and surplus) -->
        <dirset id="assets.dirset" dir="assets/" defaultexcludes="yes"/>
        <!-- assets -->
        <fileset id="assets.fileset" dir="assets/" includes="**" excludes="asset.index" defaultexcludes="yes"/>
    </path>

    <pathconvert pathsep="${line.separator}" property="assets" refid="assets.path" dirsep="/">
        <mapper>
            <globmapper from="${basedir}/assets/*" to="*" handledirsep="yes"/>
        </mapper>
    </pathconvert>

    <echo file="assets/asset.index">${assets}</echo>

</target>

<target name="-pre-build" depends="index-assets"/>

我想我仍然没有完全掌握Gradle的基本概念,但这是我尝试的方法:
task indexAssets << {

def assets = file("assets")
def contexts = files(assets)
inputs.file(assets)
outputs.file("assets/assets-gradle.index")

def tree = fileTree(dir: 'assets', include: ['**/*'], exclude: ['**/.svn/**', 'asset.index'])

contexts.collect { relativePath(it) }.sort().each { println it }
tree.collect { relativePath(it) }.sort().each { println it }
}
  • 树很好,但仅包含文件(叶)路径
  • 我只是似乎无法获得目录(上下文)的简单明细列表。我尝试了其他几种变体(树,包含/排除),但是我要么在该目录中得到一个文件,要么目录名本身,要么什么也没有。我只想在“ Assets ”目录中找到目录的简单列表。
  • 现在,我只是尝试打印路径,但我也想知道以后将其写入文件(如ANT的echo文件)的正确方法。

    更新:
    这个简单的代码 fragment 似乎完成了这一部分(+ svn过滤器),但是我宁愿找到一种更“Gradley”的方式来执行此任务。它稍后在构建变体的上下文中作为构建前依赖项运行。 (注意:在此hack中,我必须指定“项目”作为路径的一部分,因为我想我不在该任务的项目环境中?)
    def list = []
    def dir = new File("Project/assets")
    dir.eachDirMatch (~/^(?!\.svn).*/) { file ->
        list << file
    }
    
    list.each {
        println it.name
    }
    
  • 最佳答案

    好的,这是到目前为止我发现的最干净的方法。
    如果FileTree收集模式能够做到这一点,我仍然会更开心,但这几乎是简明的,甚至可能更加明确和不言自明。
    关键是将fileTree.visitrelativePath结合使用(请参见下文)
    另外,我添加了任务上下文并添加了对相关构建步骤的依赖关系,并为每个变体构建编写了实际的 Assets 索引文件。
    您问为什么要这样做?由于AssetManager的运行速度非常慢,请参见here及其后的答案线程(触发了原始ANT目标)。

    android {
    
    task indexAssets {
        description 'Index Build Variant assets for faster lookup by AssetManager later'
    
        ext.assetsSrcDir = file( "${projectDir}/src/main/assets" )
        ext.assetsBuildDir = file( "${buildDir}/assets" )
    
        inputs.dir assetsSrcDir
        //outputs.dir assetsBuildDir
    
        doLast {
            android.applicationVariants.each { target ->
                ext.variantPath = "${buildDir.name}/assets/${target.dirName}"
                println "copyAssetRec:${target.dirName} -> ${ext.variantPath}"
    
                def relativeVariantAssetPath = projectDir.name.toString() + "/" + ext.variantPath.toString()
                def assetIndexFile = new File(relativeVariantAssetPath +"/assets.index")
                def contents = ""
    
                def tree = fileTree(dir: "${ext.variantPath}", exclude: ['**/.svn/**', '*.index'])
    
                tree.visit { fileDetails ->
                    contents += "${fileDetails.relativePath}" + "\n"
                }
    
                assetIndexFile.write contents
            }
        }
    }
    
    indexAssets.dependsOn {
        tasks.matching { task -> task.name.startsWith( 'merge' ) && task.name.endsWith( 'Assets' ) }
    }
    
    tasks.withType( Compile ) {
        compileTask -> compileTask.dependsOn indexAssets
    }
    
        ...
    }
    

    10-07 19:14
    查看更多