Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle subproject external library dependency based on (main) project module flavours

Tags:

android

gradle

I have two modules an app and a library and two external maven libraries that I use.

The external libraries have slight differences and are selected based on the build flavour together with the maven classifier.

Both the (subproject/module) and the (main module) use the same external library based on the flavour.

My problem is I cannot control/select the subproject library when compiling.

Module App:

apply plugin: 'android-sdk-manager'
apply plugin: 'com.android.application'

android {
productFlavors {
fOne {}
fTwo {}
}
}

dependencies {   
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.android.support:gridlayout-v7:22.2.1'
    compile 'com.android.support:support-annotations:22.2.1'

// selecting the library based on the flavour
fOneCompile(group: 'com.xyz', name: 'SDK', version: '1.0', ext: 'aar')
fTwoCompile(group: 'com.xyz', name: 'SDK', version: '1.0', classifier: 'qa', ext: 'aar')

 //<< the library also needs the com.xyz.SDK
 fOneCompile project(path: ':library', configuration: "fOneCompile") 
 fTwoCompile project(path: ':library', configuration: "fTwoCompile")


}

Module library:

apply plugin: 'com.android.library'
android {
....
}

repositories{    
  jcenter()
  flatDir{
      dirs 'libs'
   }
}

configurations {
    fOne
    fTwo 
}

dependencies {
    ??? what goes here, flavours are not available ??? 
    //the library also needs the com.xyz.SDK
    fOneCompile(group: 'com.xyz', name: 'SDK', version: '1.0', ext: 'aar')
    fTwoCompile(group: 'com.xyz', name: 'SDK', version: '1.0', classifier: 'qa', ext: 'aar')
}

Module library fails to compile because it can not find the SDK and I need to include one based on the flavour being compiled.

like image 200
Wayne Avatar asked Nov 29 '25 16:11

Wayne


2 Answers

Use this config:

defaultConfig {
       publishNonDefault true
}

configurations {
    fOneDebugCompile
    fOneReleaseCompile
    fTwoDebugCompile
    fTwoReleaseCompile
}
like image 113
peng jiang Avatar answered Dec 02 '25 07:12

peng jiang


The solution is quite simple, since there are duplicate dependencies and the main project selects the appropriate dependency one can use the "provided" statement int the sub modules when dependencies are resolved.

In this case one can replace the "compile" statement in the dependencies section with "provided" in the Module library(sub module):

Module library:

apply plugin: 'com.android.library'
android {
....
}

repositories{    
  jcenter()
  flatDir{
      dirs 'libs'
   }
}

dependencies {
    //the library also needs the com.xyz.SDK
    provided(group: 'com.xyz', name: 'SDK', version: '1.0', ext: 'aar')
}
like image 31
Wayne Avatar answered Dec 02 '25 07:12

Wayne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!