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

前端 未结 2 655
一整个雨季
一整个雨季 2021-01-04 05:05

I am trying to create an AAR file for a library Android project using Android Studio and gradle.

I want to exclude from this archive specific folders and files but

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-04 05:22

    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")
        }
    

提交回复
热议问题