How to exclude a file from an .AAR Android library archive with gradle

走远了吗. 提交于 2019-11-30 17:46:14

Think it is related to this issue. Generally you could try with:

  • Source set excludes
  • Packaging options
  • Aapt options

I couldn't get either of them to work though.

That said, you could make some ugly hacks like this:

def filterVariant = { variantToFilter, filterTask->
        def vv = android.sourceSets."${variantToFilter}".res.srcDirs
        println "${variantToFilter} --> ${vv*.toString()}"

        def variantRes = android.sourceSets."${variantToFilter}".res
        variantRes.srcDirs.each{ resDir->
            def filterOutput = "${buildDir}/res-filter"
            if (resDir.toString().contains(filterOutput)) {
                return
            }

            println "begin filter ${resDir} to ${filterOutput}/${variantToFilter}"

            filterTask.from fileTree(dir: resDir, exclude: '**/comment.txt')
            filterTask.into "${filterOutput}/${variantToFilter}"


            variantRes.srcDirs = ["${filterOutput}/${variantToFilter}"]
        }
    }

    project.task('filterMainResources', type: Copy) {
        filterVariant 'main', it
    }

    android.libraryVariants.all{ variant ->
        project.task("filter${variant.name}Resources", type: Copy) { filterTask ->
            filterVariant "${variant.name}", filterTask
            filterTask.dependsOn "filterMainResources"
        }
        variant.mergeResources.dependsOn("filter${variant.name}Resources")
    }
sschrass

I can't try it right now, but this would probably be my approach:

sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        aidl.srcDirs = ['src']
        resources.srcDirs = ['src']           
        renderscript.srcDirs = ['src']
        java.srcDirs = ['src']
        res.srcDirs = ['res']
    }

    flavor1 {
        java.srcDirs = ['src/main/java', 'src/flavor1/java']         
        res.srcDirs = ['src/flavor1/res'] //this differs from your structure
        assets.srcDirs = []
    }

    flavor2 {
        java.srcDirs = ['src/main/java', 'src/flavor2/java']
        res.srcDirs = ['src/flavor2/res'] //this differs from your structure
        assets.srcDirs = ['src/main/res/raw']
    }
}

Again, I couldn't try it, so this could be complete nonsense. The idea was to not exclude stuff, but only include the files/dirs you do want in you flavor. This may will lead to duplication of files/folders per flavor!

As sources I would recommend: Gralde Plugin User Guide, the answer from Xavier Ducrohet and also this Gradle + Android, want to overwrite /assets with custom folder in buildFlavor.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!