Android studio Gradle project scope dependency - android

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

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

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.

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}"
}

Related

how to add a project level dependency in gradle

I have an app and an android library in my application, in development env i would like to provide a project level dependency i.e. dependency in the
(Project:XXX) build.gradle file.
This is how my project level build.gradle looks (partial view)
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
compile 'io.reactivex:rxandroid:1.1.0'
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
compile 'io.reactivex:rxjava:1.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
I get an error Gradle DSL method not found : compile().
Is there a way in android to provide common project level dependency.
You can't add dependencies to any other scope except of classpath in the buildscript block.
Here's how you declare a project dependency (in another project, not in buildscript):
dependencies {
compile project(':shared')
}
You are using the dependencies block inside the buildscript. It is wrong.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
}
}
dependencies {
compile 'io.reactivex:rxandroid:1.1.0'
compile 'io.reactivex:rxjava:1.1.0'
}

What's the use of classpath in Gradle buildscript

What is the use of including classpath in dependencies section of buildscript build.gradle(Project). Example :
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
classpath 'com.android.tools.build:gradle:1.5.0' what is its purpose / what does it do?
This is the classpath used by Gradle itself. If you want to extend your build logic with non-standard plugins or other supporting classes they must be found in this classpath.
The mentioned dependency com.android.tools.build:gradle:1.5.0 contains the plugin doing the android specific build stuff.

plugin with id not working studio

I am trying to add google play services library in Android studio.I have added the google play services in build.gradle.
But it is now working for me.I have referred many SO Post regarding to this.But it doesn't solved my problem.
Below I am posted the build.gradle code where I have added the google play services.
build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
apply plugin : 'android'
dependencies {
compile 'com.google.android.gms:play-services:4.0.30'
}
allprojects {
repositories {
jcenter()
}
}
}
While adding the google play services in dependencies at gradle compile com.google.android.gms:play-services:4.0.30. I am getting an error:plugin with id not working.
Anyone can help me with this.Thank you.
The build.gradle you have posted seems to be the project configuration. The global project build.gradle has the buildscript tag and also the comment about not adding application dependencies as can be seen in your question.
There should be another build.gradle inside your app module where you should also find something similar to this depending on your version of Android Studio and Gradle,
apply plugin: 'com.android.application'
android {
...
}
dependencies {
...
}
Place your compile 'com.google.android.gms:play-services:4.0.30' inside the app module's build.gradle depedencies
Use latest version of play services:
compile 'com.google.android.gms:play-services:7.5.0'
Update:
Think you using wrong gradle file. You must add the above code in the gardle file under app folder.
Please add your library dependencies in the build.gradle file the resides inside your app module
apply plugin: 'com.android.application'
repositories {
maven { url "http://dl.bintray.com/populov/maven" }
mavenCentral()
}
android {
...............
}
dependencies {
compile 'com.google.android.gms:play-services:4.0.30'
}
You should not add these dependencies inside build.gradle file for the root folder
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
Do not place your application dependencies inside the outer build.gradle file they belongin the individual module build.gradle files

Gradle errors while adding dependencies in build.gradle

I've tried to included cards library in my project using the below code in my build.gradle file.
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
dependencies {
//Core card library
compile 'com.github.gabrielemariotti.cards:cardslib-core:2.0.1'
//Optional for built-in cards
compile 'com.github.gabrielemariotti.cards:cardslib-cards:2.0.1'
//Optional for RecyclerView
compile 'com.github.gabrielemariotti.cards:cardslib-recyclerview:2.0.1'
//Optional for staggered grid view support
compile 'com.github.gabrielemariotti.cards:cardslib-extra-staggeredgrid:2.0.1'
//Optional for drag and drop support
compile 'com.github.gabrielemariotti.cards:cardslib-extra-dragdrop:2.0.1'
//Optional for twowayview support (coming soon)
//compile 'com.github.gabrielemariotti.cards:cardslib-extra-twoway:2.0.1'
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
But when compiling, android studio is throwing up errors as below.
Error:(23, 0) Gradle DSL method not found: 'compile()'
Possible causes:The project 'cardslib_1' may be using a version of Gradle that does not contain the method.
Open Gradle wrapper fileThe build file may be missing a Gradle plugin.
Apply Gradle plugin
I'm guessing the reason to be gradle version, which is lower in libraries I'm including.
How to know the gradle version my dependencies are using and how to adjust them to my project.
When I thought to add the libraries, maven has repositories in aar file which I don't think will let you know the gradle version.
Thanks for any help in this regards.
You're adding the dependencies in the wrong place. They should be outside of the buildscript section and in the modules/applications build.gradle.
Parent build.gradle. This should be in the root directory of your project
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
Module build.gradle. This should be in the folder of the module you're trying to add the dependencies to.
apply plugin: 'com.android.application'
android {
// Android related settings go here
}
dependencies {
compile 'com.github.gabrielemariotti.cards:cardslib-core:2.0.1'
compile 'com.github.gabrielemariotti.cards:cardslib-cards:2.0.1'
compile 'com.github.gabrielemariotti.cards:cardslib-recyclerview:2.0.1'
compile 'com.github.gabrielemariotti.cards:cardslib-extra-staggeredgrid:2.0.1'
compile 'com.github.gabrielemariotti.cards:cardslib-extra-dragdrop:2.0.1'
}
This assumes that the structure of your project is something like
Project
|___build.gradle
|___Module
|____build.gradle

How do I add dependencies to gradle.build

I am working on Android Studio and my activities include ViewPagers. For that I had to install packages (Android Support Repository and Android Support Library). Now that I have already installed these, I opened gradle.build and this is what I see:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.13.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
My question:
What should I add and where should I add it in here?
Also, I cant locate SDK in my computer :/ . A little help would be really appreciated.
Android use multi project build setup. Your existing file is MAIN build.gradle file.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.13.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
// this is where you need to put dependencies of your BUILD SCRIPT
}
}
allprojects {
repositories {
jcenter()
}
dependencies {
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.1'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.1'
// This is where you put dependencies EVERYONE of your modules need.
}
}
In the sub directories of your main build.gradle resides individual build.gradle files.
Module A
dependencies {
// This is where you put dependencies, your module need.
}
Module B
dependencies {
// This is where you put dependencies, your module need.
}
There are more than one gradle files. one of them is located in your project directory. Same name (build.gradle). find it and put your dependencies there.
To find your SDK path: File -> Project Structure -> SDK Location. (Android Studio v0.8.14)

Categories

Resources