bakua
bakua

Reputation: 14454

Android studio Gradle project scope dependency

I have multiple modules and every module uses exact same dependency. Because it is annoying to maintain that dependency in all build.gradle files (version updates etc.) I'd like to have it in project build.gradle. Is that possible please?

I tried to:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'          
        compile 'joda-time:joda-time:2.8.1'
    }
}
allprojects {
    repositories {
        jcenter()
    }
}

but that doesn't work as it seems that gradle is not able to find DSL for compile. Is there any other way please?

Thank you

Upvotes: 2

Views: 862

Answers (3)

Johan Stuyts
Johan Stuyts

Reputation: 3687

You cannot centralize the dependencies for all (sub-)projects, but is is very easy to centralize the version numbers. In build.gradle in the root add this:

ext {
    jodaTimeVersion = '2.8.1'
}

And in build.gradle of the modules that need Joda-Time add this (pay attention to the double quotes):

dependencies {
    compile "joda-time:joda-time:${jodaTimeVersion}"
}

Upvotes: 0

Will Bobo
Will Bobo

Reputation: 424

Correct me if i'm wrong but what's inside buildscript are dependencies for build.

When I generate my project build.gradle there is note :

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
        classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.1.1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

So no, you can't put dependencies for all your module here but maybe there is a plugin for that.

Upvotes: 0

M D
M D

Reputation: 47817

It's called centralize the support libraries dependencies in gradle. Working with multi-modules project, it is very useful to centralize the dependencies, especially the support libraries.

http://gmariotti.blogspot.in/2015/07/how-to-centralize-support-libraries.html

Upvotes: 2

Related Questions