4

I am trying to build an Android project with Gradle.

It has following structure:

ProjectA----- MainProject,   
LibA     ---- Library project,   
LibB     ---- Library project,   
LibC     ---- Library project,   
LibD     ---- Library project,  
etc...

Based on situtation, I need to include the libraries, sometimes need to include all libraries, 1 library, 2 or 3 etc. based on flavors. In settings file I have included all projects.

Does anybody know how to include/exclude libraries based on flavors?

I have tried on dependency block, there I am getting error.
Following is the sample code

dependencies {
if (task.name.matches('compileFlovor1'){
  exclude module: 'LibD'
   }
}

Error is: Could not find method exclude() for arguments [{module=LibD}].

Please guide me resolving this issue

Vincent Malgrat
  • 66,725
  • 9
  • 119
  • 171
user2318724
  • 259
  • 5
  • 16

1 Answers1

6

do add flavour specific dependencies you must configure the according configuration. let's say you have to flavours "free" and "payed"

android {
    productFlavors {
        free
        paid
    }
}

then you can set flavour specific dependencies in the dependency block in the following way:

dependencies {
    compile project(':library1')  //library1 used in free and paid flavour
    freeCompile project(':library2')
    paidCompile project(':library3')    
    paidCompile project(':library4')    
    paidCompile project(':library5')     
}

hope that helped,

cheers, René

Ken
  • 30,811
  • 34
  • 116
  • 155
Rene Groeschke
  • 27,999
  • 10
  • 69
  • 78
  • Thanks Rene Grieschke... It worked for me. Thank you very much.. It is possible by exclude also? – user2318724 Apr 26 '13 at 15:37
  • We process the dependencies in a custom way and don't provide a way to exclude dependencies. I'm not sure how this would work anyway. The only way would be to remove a top level dependency (and all its transitive dep) as anything else could break the build. Might as well just declare it only in the flavors that require it then. – Xavier Ducrohet Apr 27 '13 at 17:41
  • In continuation to above question, How the libraries are included as part of apk of main project that is ProjectA. In ProjectA/build/class/flavour/... only library project class files are included which are required at compile time by ProjectA and only that library is working. My thing is plug and play library.. means a library can a service and not used any where or imported in ProjectA and only included in manifest file of ProjectA. In this case, how to include the libraries. for ex: in eclipse, it will bundle all together but the same is not happening in my case... Thanks in advance – user2318724 Apr 30 '13 at 10:02
  • This does not work for plain Java projects. If for example build.gradle of ':library2' is: apply plugin: 'java' apply plugin: 'idea' apply plugin: 'eclipse' then files in library1/src/main/resources/assets/ are not included in the APK assets. Or am I doing something wrong? – Blackhex Dec 17 '13 at 18:37