java - Switching dependencies in Gradle depending on platform -
i trying gradle select different dependencies in multiproject build based on whether building desktop or android. have common subproject (a library) trying reuse. however, cannot gradle correctly switch dependency configurations.
my main settings.gradle includes dependencies:
// /settings.gradle rootproject.name = 'myproject' include 'androidui' include 'reusablelibrary' include 'desktopui' now both androidui , desktopui specify reusablelibrary dependency:
// /androidui/build.gradle , /desktopui/build.gradle apply plugin: 'java' dependencies { compile project(path: ':reusablelibrary', configuration: 'desktop') } reusablelibrary specifies 2 configurations, because dependencies different whether it's building on desktop or android:
// /reusablelibrary/build.gradle apply plugin: 'java' configurations { desktop { extendsfrom compile } android { extendsfrom compile } } dependencies { // examples, real list longer. // point h2database included on desktop, // , ormlite included on android. android 'com.j256.ormlite:ormlite-jdbc:5.0' desktop 'com.h2database:h2:1.4.192' } this looks fine me. when compile either desktopui or androidui, can see although dependencies of reusablelibrary being included on classpath in manner desire, actual jar provided reusablelibrary itself not included. of course causes build fail. suspect i'm not setting reusablelibrary correctly; i'm not clear on configurations {} blocks do.
why aren't compiled items in reusablelibrary being included on classpaths of ui projects? , canonical way include platform-specific dependencies in manner?
the original configuration pretty close right. key understand dependency graph gradle java plugin's documentation:
this visualization of java plugin's various dependency configurations, gradle-ese "list of dependencies." when add compile lines dependencies {...} block, you're adding dependency elements compile dependency list.
the default dependency configuration special; 1 included compile project("path") line unless different 1 chosen configuration: argument. means when build library, runtime dependency list (which includes compiled jar library itself) added classpath of client project.
the original configuration creates 2 new nodes, desktop , android in graph, , couples them both compile using extendsfrom. not otherwise connected graph! problem original configuration apparent: switching upstream project either of these, missing compiled code runtime. explains classpath omission.
the solution bit more subtle aiming desktop , android @ runtime. in order ensure correctly decoupled when add tests, need 1 layer of dependency configurations keep testcompile indirectly depending on runtime. additionally, library's source code may need things on classpath typecheck; can use compileonly this. end solution looks this:
configurations { desktopcompile androidcompile compileonly.extendsfrom desktopcompile testcompile.extendsfrom desktopcompile // assuming tests run on desktop desktop { extendsfrom desktopcompile extendsfrom runtime } android { extendsfrom androidcompile extendsfrom runtime } } dependencies { androidcompile "some.android:dependency" desktopcompile "other.desktop:dependency" } 
Comments
Post a Comment