Very modulable android library project with gradle configuration/flavors

I posted it in the old forums, but since it can’t be answered there anymore, I’m migrating here :slight_smile:

I am building a library on android, which is composed of a big core and several modules (let’s name them A, B, C, D, E and F) Those modules sometimes require another library and sometimes have things to add in the resulting manifest. We can even have A and B depending on a different version of the same library (so not compatible)

I would like to be able to build my core with any number of modules.

I started by creating flavors which are really useful but are a bit limited in my case.

If I wanted to create a flavor for each cases it would mean creating A LOT of them! But I’m guessing there is a simpler way to do what I would like to do.

Let the code talk a bit :smile:

apply plugin: 'com.android.library'
  android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"
      flavorDimensions "libA", "libB", "libC", "libD", "libF"
    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 21
        versionCode 11
        versionName "3.1.0"
    }
      productFlavors {
        A {
          flavorDimension "libA"
        }
          B {
          flavorDimension "libB"
        }
          C {
          flavorDimension "libC"
        }
          D {
          flavorDimension "libD"
        }
          E {
          flavorDimension "libB"
        }
          F {
          flavorDimension "libF"
        }
    }
      sourceSets {
        A {
            java {
                srcDirs = ['src/A']
            }
        }
          B {
            java {
                srcDirs = ['src/B']
            }
        }
          C {
            java {
                srcDirs = ['src/C']
            }
        }
          D {
            java {
                srcDirs = ['src/D']
            }
        }
          E {
            java {
                srcDirs = ['src/E']
            }
        }
          F {
            java {
                srcDirs = ['src/F']
            }
        }
    }
}
  dependencies {
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.google.android.gms:play-services:6.5.87'
      androidTestCompile 'org.mockito:mockito-core:1.10.19'
    androidTestCompile 'com.google.dexmaker:dexmaker:1.0'
    androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.0'
      ACompile files('libs/A-2.1.0.jar')
      BCompile files('libs/B-4.1.0.jar')
      CCompile files('libs/C-5.1.0.jar')
      DCompile files('libs/D-1.01.jar')
      // same library as B, but different version
    ECompile files('libs/B-5.2.2.jar')
      // F nothing
}

What I would like to be able to produce is:

A.aar B.aar C.aar D.aar E.aar F.aar A+B.aar A+C.aar A+D.aar A+E.aar A+F.aar A+B+C.aar A+B+D.aar A+B+F.aar (since B and E are not compatible) A+C+D.aar A+C+E.aar

etc. etc. etc…

When I try with this gradle file, and launch “gradlew assembleRelease” I only obtain: A+B+C+D+F.aar and A+C+D+E+F.aar

I thought of maybe compiling each of them with the main core, and then maybe merging it using Gradle merge tools.

What would be modified to obtain the result I’m looking for? And could I depend on a external file which would tell me which components to build?

thanks!

I got no answer, but I’d still like to share the solution if ever people were to have thise kind of idea (and maybe it’s not the best solution, so feel free to criticize!)

I put a series of constants in my gradle.properties, removed all flavors and compile depending on those.

So it looks like:

sourceSets {
main{
if (A) {
srcDirs += [‘src/A’]
}
if (B) {
srcDirs += [‘src/B’]
}

             etc...

}

dependencies {
compile 'com.android.support:appcompat-v7:21.0.3’
compile ‘com.google.android.gms:play-services:6.5.87’

if (A) {
      compile files('libs/A-2.1.0.jar')
}
if (B) {
      compile files('libs/B-2.1.0.jar')
}

etc…
}