Search code examples
androidgradlegroovyandroid-gradle-pluginandroid-build

Gradle configuration inheritance


I have multiple dependencies inside a gradle file and I introduced a new build variant call "apple". But I don't want to copy and paste as the following.

dependencies {
    debugCompile "com.android:libraryA:1.0.0"    
    debugCompile "com.android:libraryB:1.0.0"    
    debugCompile "com.android:libraryC:1.0.0"    

    appleCompile "com.android:libraryA:1.0.0"    
    appleCompile "com.android:libraryB:1.0.0"    
    appleCompile "com.android:libraryC:1.0.0"    
}

Is there a way I can say appleCompile depends on debugCompile?


Solution

  • You can declare a new configuration:

    configurations {
        [debugCompile, appleCompile].each { it.extendsFrom commonCompile }
    }
    

    Now commonCompile configuration will apply dependencies for both debug and apple configurations, so you don't need to specify those twice.

    dependencies {
        commonCompile "com.android:libraryA:1.0.0"    
        commonCompile "com.android:libraryB:1.0.0"    
        commonCompile "com.android:libraryC:1.0.0"    
    }