Add dependency to specific productFlavor and buildType in gradle

前端 未结 6 1745
长情又很酷
长情又很酷 2020-12-05 11:31

I\'m wondering how to add dependency to specific productFlavor and buildType in gradle. For example I have productFlavor free and build type release

6条回答
  •  醉梦人生
    2020-12-05 11:51

    android {
        ext.addDependency = {
            task, flavor, dependency ->
                def taskName = task.name.toLowerCase(Locale.US)
                if (taskName.indexOf(flavor.toLowerCase(Locale.US)) >= 0) {
                    task.dependsOn dependency
                }
        }
    
        productFlavors {
            production {
            }
            other
        }
    
        task theProductionTask << {
            println('only in production')
        }
    
        tasks.withType(JavaCompile) {
            compileTask -> addDependency compileTask, "production", theProductionTask
        }
    }
    

    To be frank, I don't which locale is used to generate names for compile taks so toLowerCase(Locale.US) may be counterproductive.

提交回复
热议问题