Dinamically add libs in repository

I’m porting several Makefile projects in Gradle. I want every dependency to be versioned, so I created a common gradle file, namely general.gradle, that is included in each specific build.gradle file. For every project there is also a file dependencies.gradle, that contains a map {dependencyName, version}. Here an example:

ext.versions  = [:]    
versions.'libraryA'= "8a3f32"
versions.'libraryB'= "4af3e5"

I declare what libraryX refers to in general.gradle, inserting the libraries in the repositories, as follows:

repositories {        
 libs(PrebuiltLibraries) {        
  libraryA {
      def libraryName = "${name}"
      headers.srcDir "../${libraryName}"
      binaries.withType(SharedLibraryBinary) {                     
        sharedLibraryFile = file("${baseDir}/lib/${targetPlatform.name}/${buildType.name}/lib${libraryName}_" + versions."${libraryName}" + ".so")
       }         
   }
         
   libraryB {
      def libraryName = "${name}"
      headers.srcDir "../${libraryName}"
      binaries.withType(SharedLibraryBinary) {                    
         sharedLibraryFile = file("${baseDir}/lib/${targetPlatform.name}/${buildType.name}/lib${libraryName}_" + versions."${libraryName}" + ".so")
      }         
   }
            
   libraryC {....}
   libraryD {....}
}

By this way, I can also set the right path depending on build type (debug or release) and platform (x86 or x86_64).

As you can see, inside every libraryX there is the very same code. I was wondering if I could avoid this unnecessary duplication, using a list

libraries = [ "libraryA", "libraryB", ...]

I tried doing:

repositories {        
      libs(PrebuiltLibraries) {        
       [ "libraryA", "libraryB", ...].each {
        it {
          def libraryName = "${name}"
          headers.srcDir "../${libraryName}"
          binaries.withType(SharedLibraryBinary) {                    
             sharedLibraryFile = file("${baseDir}/lib/${targetPlatform.name}/${buildType.name}/lib${libraryName}_" + versions."${libraryName}" + ".so")
          }         
       }
     }                
    }

but I got this error:

Exception thrown while executing model rule: model.repositories   > No signature of method: java.lang.String.call() is applicable for argument types: (general_dt1njom2wz075lo490p9e44em$_run_closure3_closure24_closure25_closure26_closure29) values: [general_dt1njom2wz075lo490p9e44em$_run_closure3_closure24_closure25_closure26_closure29@2b2954e1]        
 Possible solutions: wait(), any(), wait(long), take(int), each(groovy.lang.Closure), any(groovy.lang.Closure)

Any idea?

Thanks,
Mauro

In the scope of the each() closure, it is simply the String ‘libraryA’, ‘libraryB’, etc. You can’t call it like a method and pass a closure as an argument. You probably want to do something like this:

repositories {        
  libs(PrebuiltLibraries) {        
    [ "libraryA", "libraryB", ...].each {
        def libraryName = "${it}"
        headers.srcDir "../${libraryName}"
        binaries.withType(SharedLibraryBinary) {                    
           sharedLibraryFile = file("${baseDir}/lib/${targetPlatform.name}/${buildType.name}/lib${libraryName}_" + versions."${libraryName}" + ".so")
        }         
   }                
}