gradle demo

productFlavor{
  free{}
}
FreeImplementation 'com.google.android.gms:play-services-ads:17.2.0'

dependencies {
    gradle.startParameter.getTaskNames().each { task ->
        if(task.contains("free")) {
             implementation 'com.google.android.gms:play-services-ads:17.2.0'
        }
    }
}

extract-common-methods-from-gradle-build-script

// Define methods as usual
def commonMethod1(param) {
    return true
}
def commonMethod2(param) {
    return true
}

// Export methods by turning them into closures
ext {
    commonMethod1 = this.&commonMethod1
    otherNameForMethod2 = this.&commonMethod2
}

gradle tasks