I want to include 'org.scribe:scribe:1.3.2' dependency into my gradle project, I have added the following line to my build.gradle file
apply plugin: 'groovy'
repositories {
mavenCentral()
}
dependencies {
runtime 'org.scribe:scribe:1.3.2'
compile 'org.codehaus.groovy:groovy-all:2.2.0'
}
task fbTask << {
MyTask mT = new MyTask()
mT.loginUsingFacebook();
}
Now inside MyTask.groovy
import org.scribe.model.Token
public void loginUsingFacebook() {
Token accessToken = //some code
}
It didn't find the import, which shows that the scribe dependency we specified in build.gradle didn't worked.
So, how to import this scribe dependency into the application , so that i can use it in my Task class.
There are some misconceptions here:
Dependencies declared in the project.dependencies block are for code produced by the build, not for code used by the build.
Gradle tasks cannot be instantiated with new.
How to add a dependency used by the build itself (typically by a build script, task class, or plugin class) depends on where you put the corresponding code. In the simplest case, the task class is declared right in the build script, and its dependencies go into a buildscript block:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.scribe:scribe:1.3.2"
}
}
To learn more about these topics, check out the Gradle User Guide, and the samples in the full Gradle distribution.
If you want to add something to classpath of buildscript(build.gradle) and not the project source, then add it to buildScript closure.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.scribe:scribe:1.3.2'
}
}
Reference : In Gradle documentation's Organizing Build Logic chapter, see the section External dependencies for the build script
Related
Im using gradle to build my project mixing Kotlin and Apache Spark, but as soon I declare the spark dependency, the Fat JAR I generate gets non working. Otherwise it will work. The source code not even import anything from Spark
buildscript {
ext.kotlin_version = '1.2.40'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
// uncomment this line to get main class 'myownpkg.SparkApplicationKt'
// not found error
// compile 'org.apache.spark:spark-core_2.11:2.3.0'
}
jar {
manifest { attributes 'Main-Class': 'myownpkg.SparkApplicationKt' }
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
After some experimentation I realized the generated JAR was having there a lot of duplicated files, causing that runtime not finding the .class required.
It was triggered after enable the Spark because it is the dependency causing snowball of other dependencies having the same file paths under META-INF folder
exclude 'META-INF/*'
That line made the trick to avoid duplicates but still wil have a META-INF folder in final JAR
The main reason is because you aren't creating the "FatJar" artifact with necessary dependencies. The compile dir in configuration only contain compiled source code.
From the maven central you need at least the 50 compile dependencies that spark-core require. Have you consider using the shadow plugin ?
Take a look at this thread on gradle discuss.
I am using Android studio 1.5.1.
My build.gradle looks like below
allprojects {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
}
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
}
}
apply plugin: 'com.android.application'
dependencies {
// Unit testing dependencies
testCompile 'org.hamcrest:hamcrest-library:1.3'
testCompile 'junit:junit:4.12'
}
I am writing some JUNIT test cases for my application as below
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
public void testSomething throws Exception {
...
assertThat(result, is(true));
}
But when I am running the tests, it is showing that
error: package org.hamcrest does not exist
error: cannot find symbol assertThat(result, is(true));
But I can go to the definition of assertThat, org.hamcrest etc from studio by going to definition as studio decompiles the jars.
Also I can see the package downloaded in .gradle/caches
Can anyone suggest what I am doing wrong here?
Normally Junit test cases using assertFalse, assertTrue etc is working, only hamcrest matching is giving errors.
Manually add the jar from here
Add it to your libs folder.
Right click the jar, and click add as library.
I had the same issue with getting it to find junit. Looks like Gradle is not doing its job.
I have filed a bug report, Please star:
https://code.google.com/p/android/issues/detail?id=209832&thanks=209832&ts=1463161330
Just trying Android Studio & Gradle and I want to add Roboguice to my Project, this is my build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
}
}
allprojects {
repositories {
mavenCentral()
}
}
dependencies {
compile 'org.roboguice:roboguice:2.0'
}
gives me
Gradle 'TestApp' project refresh failed:
Build script error, unsupported Gradle DSL method found: 'compile()'!
I tried to move the line
compile 'org.roboguice:roboguice:2.0'
within the dependencies in buildscript but that also failed.
How is this done correctly?
dependencies {
compile 'org.roboguice:roboguice:2.0'
}
Move this part from build.gradle in root (top level build file) to build.gradle in your module
Hello StackOverflow,
I have recently updated my Android Studio to the latest version (0.4.6), and I am encountering weird problems with it. When first creating my project, I got this error:
So I obviously went to my SDK Manager and updated my Build Tools to version 19.0.2. However, I still got this error message. I took a look into my build.gradle file and saw that I am missing the android paragraph, So I changed it from this:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
}
}
allprojects {
repositories {
mavenCentral()
}
}
To this:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
}
}
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion '19.0.2'
//Other Configuration
}
allprojects {
repositories {
mavenCentral()
}
}
But now I am getting this error message when trying to build (and the older Build Tools error):
I'm not sure what to do now.. How can I solve this?
As suggested by Scott keep your Root level build.gradle file like this
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
}
}
allprojects {
repositories {
mavenCentral()
}
}
Content of this file will be included in each module level build.gradle file at the type of Gradle sync or compilation.
Check all build.gradle files inside your modules. They all should look similar to this
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion '19.0.2'
//Other Configuration
}
dependencies{
// Your all module dependencies here
}
Undo the changes you made to the top-level build file, and make those changes to the build files in your individual modules. In particular, putting an apply plugin: 'android' statement or android block in your top-level build file in that manner won't work. The error message you're seeing is happening because the build system is trying to build an Android app out of the root directory of your project, but none of the source files for such a project are there.
I have a root build.gradle that contains some variables I want to have as global variables for some subprojects but not all sub projects
I created a list:
List spring_dependencies = [
"org.springframework:spring-beans:$springVersion",
"org.springframework:spring-core:$springVersion",
"org.springframework:spring-web:$springVersion",
]
I have some suprojects that do NOT use this for compilation, so I want to only add:
compile spring_dependencies
to the projects that actually need spring.
How can I accomplish this global variable sharing in gradle?
One method I have just tried (and it seems to work) is to declare another sub-project (I called it 'spring'), which has the following build.gradle:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile "org.springframework:spring-beans:$springVersion"
compile "org.springframework:spring-core:$springVersion"
compile "org.springframework:spring-web:$springVersion"
}
Add this project in to the settings.gradle list of sub-projects, then in the build.gradle that requires the spring libraries, you can do:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile project(':spring')
}
To extract the dependencies from your spring sub-project.
There might be a better way to achieve the same result... :-/