How to add external jars to build.gradle from local system folder - gradle

I have created a custom plugin in groovy. now i want to add it to my build.gradle in another project:
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
Above lines does not seem to work for me. i have put my external jar in libs folder in project root folder. I am getting error plugin with id not found.Please correct me if i am wrong.

Related

Is it possible to use resource files from jar in gradle?

I want to use resource files from two jar files. Is it possible ?
build.gradle
sourceSets {
main {
resources {
srcDir 'src/resources'
files('libs/myOwnFile-1.jar:src/resources')
//add another resource jar file
}
}
}
This means , I want to include resource files (mainly xml files) also from libs/myOwnFile-1.jar file. The jar is creating by other team but it's another project which cannot be linked with this project. But I know the path of resources (src/resources)
Is it possible to use src/resources under jar file ?
If an external JAR contains resources you want to use, consider just adding that JAR as a dependency to your project. That way, you can also use these resources in your own code.
For example, the following code will add all JARs in the libs folder as dependencies:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
The sourceSets property on the other hand is typically for sources/resources that are part of the project itself (and aren't part of another JAR).

Can gradle android apk project dependencies another apk project's jar task?

I have a project with two modules:
first one HostProject
second one SubProject
LibProject is a android apk project but has a jar task to make a jar from the code.
jar task:
task sourcesJar(type: Jar) {
from 'build/intermediates/classes/debug'
archiveName 'SubProject.jar'
}
In HostProject I want to use libproject's code by the jar file. Is there someway easy to add dependency?
HostProject build.gradle:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
****compile project(':LibProject')****
}
gradle show error: resolves to an APK archive which is not supported as a compilation dependency
can I use the dependencies in HostProject's build.gradle without add new task?

Unsupported Gradle DSL method found: 'compile()'!

I'm going through Google's documentation on "Add Google Play Services to Your Project" in Android Studio:
https://developer.android.com/google/play-services/setup.html
I'm using that documentation to modify the build.gradle file of a freshly created Android project. In Step 2 (Add Google Play Services to Your Project), it states:
Add this line:
apply plugin: 'android'
Under Dependencies, add this:
compile 'com.google.android.gms:play-services:5.0.77'
It also says to update that version after updating Google Play Services, which is now at 18 according to Android SDK Manager.
Here is my entire build.gradle file at the top-level (parent of this file is the root folder).
// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply plugin: 'android'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
compile 'com.google.android.gms:play-services:18'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
Upon saving, it prompts for a Sync. I Sync it, but get:
Build script error, unsupported Gradle DSL method found: 'compile()'!
Error:(10, 0) Possible causes could be:
- you are using a Gradle version where the method is absent
- you didn't apply Gradle plugin which provides the method
- or there is a mistake in a build script
I'm using Android Studio 0.8.2. I didn't install Gradle, just using the plugin that came with Android Studio.
It's interesting to note that the build.gradle file generated when I made this new project says:
//NOTE: Do not place your application dependencies here
But Google's documentation says (which conflicts with the above):
Note: Android Studio projects contain a top-level build.gradle file and a build.gradle
file for each module. Be sure to edit the file for your application module.
What's wrong with my build.gradle file (or environment)?
The Google documentation you quoted is correct, and doesn't conflict. There's more than one build.gradle file. Instead of putting dependencies in the top-level one as you have, put them in the build file that's in your module's directory.
Also, don't put an apply plugin: 'android' statement in that top-level build file; it will cause an error.
You can also add dependencies through the Project Structure UI, which does the right thing.
Do not add dependencies in your project by editing its most 'external' build.gradle (YourProject/build.gradle). Edit the one that is under the 'app' module instead (YourProject/app/build.gradle).
There, by the way, you will find the declaration of one dependency, such as:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
This block will be just below android { ... } configuration block.
In my case, I am just adding leeloo dependencies, so it became:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'net.smartam.leeloo:oauth2-client:0.1'
compile 'net.smartam.leeloo:oauth2-common:0.1'
}
Then sync your project and dependencies will be downloaded. Hope it helps!
the compile-time dependencies should reside in the dependencies block under allprojects, not under buildscript:
apply plugin: 'android'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
allprojects {
repositories {
jcenter()
}
dependencies {
compile 'com.google.android.gms:play-services:18'
}
}
This should work fine.
Think of “Gradle DSL method” as a Java method. So in Gradle, methods can be distinguished by either {} or “.”. So
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
is the same as
dependencies.compile fileTree(dir: 'libs', include: ['*.jar'])
where both “dependencies” and “compile” are methods.
So you are including a method somewhere in your build.gradle file that is not supported by your program. For example, make your dependencies this:
dependencies {
nothing 'this.does.nothing.build:gradle:0.7.+'
}
Which is the same as writing:
dependencies.nothing 'this.does.nothing.build:gradle:0.7.+'
And you will see an error saying “unsupported Gradle DSL method found: ‘nothing()’!”
Obviously "nothing" is not a real method. I just made it up.
So one of your "compile" methods inside your build.gradle is wrong.
When I faced this problem I used android developer UI to import dependencies as follows:-
1 Go to View ---> Open Module Settings
Select Dependency tab. Click + to add a dependency and select Library dependency. Choose the downloaded library here.

Gradle Plugin jar Dependencies

I have a gradle build that relies on a plugin (MyTools) which is compiled in the buildSrc directory. This part is working correctly. The issue I'm having is trying to import a class from an external jar to use in the myTools plugin's source.
My Directory Structure looks like this:
buildSrc
---build.gradle
---MyTools
-----build.gradle
-----settings.gradle
-----libs
-------yuicompressor-2.4.6.jar
-----src
-------main
---------groovy
-----------com
-------------my
---------------MyTools.groovy
---------------MyToolsPlugin.groovy
---------resources
-----------META-INF
-------------gradle-plugins
-------------gradle-plugins/MyTools.properties
The contents of MyTools/build.gradle are:
dependencies {
runtime fileTree(dir: 'libs', include: '*.jar')
}
When I try to import com.yahoo.platform.yui.compressor.CssCompressor from MyTools.groovy, I get this message:
"unable to resolve class com.yahoo.platform.yui.compressor.CssCompressor"
Can somebody please tell me what I'm doing wrong?
You need to add a compile dependency, not a runtime dependency. Also, I don't see how the main build is going to pick up the plugin, given that it's located in a MyTools subdirectory (and buildSrc doesn't have a settings.gradle). Probably best to lift up MyTools into buildSrc.

is this a gradle eclipse task bug or inconsistent on purpose?

So I have the following dependencies section in gradle
dependencies {
compile project(':sdi-master')
compile fileTree(dir: '../webserver/lib', include: '*.jar')
compile fileTree(dir: '../webserver/play-1.2.4/framework/lib', include: '*.jar')
compile fileTree(dir: '../webserver/play-1.2.4/framework', include: 'play-*.jar')
}
I also have a copy jars task like so
task deleteJars(type: Delete) {
ext.collection = files { genLibDir.listFiles() }
delete ext.collection
}
task copyJars(type: Copy) {
from(configurations.compile) {}
into genLibDir
}
copyJars.dependsOn('deleteJars')
classes.dependsOn('copyJars')
Notice how it depends on sdi-master which then has ONE compile fileTree as well. When I run copyJars, as expected, I get all the jars from the sdi-master as well copied into genLibDir. When I run the eclipse task however, those jars do NOT show up in the .classpath file as I would expect so my project doesn't compile in eclipse.
Is this a gradle eclipse task bug I need to report or is this supposed to be the behavior(though it seems very inconsistent with the copy jars using configurations.compile.
thanks,
Dean
Eclipse understands transitive dependencies, so the dependencies of sdi-master will not (and should not) show up in the current project's .classpath file. They should just show up in sdi-master's .classpath file and should be marked as exported there.

Resources