android - gradle multiproject include and exclude libraries
Asked Answered
C

1

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

Cranium answered 25/4, 2013 at 8:26 Comment(0)
K
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é

Klimt answered 25/4, 2013 at 21:2 Comment(4)
Thanks Rene Grieschke... It worked for me. Thank you very much.. It is possible by exclude also?Cranium
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.Infold
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 advanceCranium
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?Langlois

© 2022 - 2024 — McMap. All rights reserved.