I am working on integrating twitter with my android application, I am using twitter4j library to do this.
I have added the twitter4j library files and put it in a folder libs and added compile dependency in build.gradle inside app folder
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.google.android.gms:play-services:6.5.+'
compile 'org.twitter4j:twitter4j-core:4.0.2'
}
but I am still not able to use this library
In the Gradle drawer to the right, could you hit the refresh button to see if that helps? Also, the dependencies block that you've pasted above doesn't contain a directive for twitter4j - did you happen to simply miss it out in the question, or is it really missing from the gradle.build file?
Try to add this at "repositories" on your buildscript.
repositories { mavenCentral() }
I have tried to add my local .jar file dependency to my build.gradle file:
apply plugin: 'java'
sourceSets {
main {
java {
srcDir 'src/model'
}
}
}
dependencies {
runtime files('libs/mnist-tools.jar', 'libs/gson-2.2.4.jar')
runtime fileTree(dir: 'libs', include: '*.jar')
}
And you can see that I added the .jar files into the referencedLibraries folder here: https://github.com/WalnutiQ/wAlnut/tree/version-2.3.1/referencedLibraries
But the problem is that when I run the command: gradle build on the command line I get the following error:
error: package com.google.gson does not exist
import com.google.gson.Gson;
Here is my entire repo: https://github.com/WalnutiQ/wAlnut/tree/version-2.3.1
According to the documentation, use a relative path for a local jar dependency as follows.
Groovy syntax:
dependencies {
implementation files('libs/something_local.jar')
}
Kotlin syntax:
dependencies {
implementation(files("libs/something_local.jar"))
}
If you really need to take that .jar from a local directory,
Add next to your module gradle (Not the app gradle file):
repositories {
flatDir {
dirs("libs")
}
}
dependencies {
implementation("gson-2.2.4")
}
However, being a standard .jar in an actual maven repository, why don't you try this?
repositories {
mavenCentral()
}
dependencies {
implementation("com.google.code.gson:gson:2.2.4")
}
You could also do this which would include all JARs in the local repository. This way you wouldn't have to specify it every time.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
The following works for me:
compile fileTree(dir: 'libs', include: '*.jar')
Refer to the Gradle Documentation.
You can try reusing your local Maven repository for Gradle:
Install the jar into your local Maven repository:
mvn install:install-file -Dfile=utility.jar -DgroupId=com.company -DartifactId=utility -Dversion=0.0.1 -Dpackaging=jar
Check that you have the jar installed into your ~/.m2/ local Maven repository
Enable your local Maven repository in your build.gradle file:
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
implementation ("com.company:utility:0.0.1")
}
Now you should have the jar enabled for implementation in your project
A solution for those using Kotlin DSL
The solutions added so far are great for the OP, but can't be used with Kotlin DSL without first translating them. Here's an example of how I added a local .JAR to my build using Kotlin DSL:
dependencies {
compile(files("/path/to/file.jar"))
testCompile(files("/path/to/file.jar"))
testCompile("junit", "junit", "4.12")
}
Remember that if you're using Windows, your backslashes will have to be escaped:
...
compile(files("C:\\path\\to\\file.jar"))
...
And also remember that quotation marks have to be double quotes, not single quotes.
Edit for 2020:
Gradle updates have deprecated compile and testCompile in favor of implementation and testImplementation. So the above dependency block would look like this for current Gradle versions:
dependencies {
implementation(files("/path/to/file.jar"))
testImplementation(files("/path/to/file.jar"))
testImplementation("junit", "junit", "4.12")
}
The accepted answer is good, however, I would have needed various library configurations within my multi-project Gradle build to use the same 3rd-party Java library.
Adding '$rootProject.projectDir' to the 'dir' path element within my 'allprojects' closure meant each sub-project referenced the same 'libs' directory, and not a version local to that sub-project:
//gradle.build snippet
allprojects {
...
repositories {
//All sub-projects will now refer to the same 'libs' directory
flatDir {
dirs "$rootProject.projectDir/libs"
}
mavenCentral()
}
...
}
EDIT by Quizzie: changed "${rootProject.projectDir}" to "$rootProject.projectDir" (works in the newest Gradle version).
Shorter version:
dependencies {
implementation fileTree('lib')
}
The Question already has been answered in detail. I still want to add something that seems very surprising to me:
The "gradle dependencies" task does not list any file dependencies. Even though you might think so, as they have been specified in the "dependencies" block after all..
So don't rely on the output of this to check whether your referenced local lib files are working correctly.
A simple way to do this is
compile fileTree(include: ['*.jar'], dir: 'libs')
it will compile all the .jar files in your libs directory in App.
Some more ways to add local library files using Kotlin DSL (build.gradle.kts):
implementation(
files(
"libs/library-1.jar",
"libs/library-2.jar",
"$rootDir/foo/my-other-library.jar"
)
)
implementation(
fileTree("libs/") {
// You can add as many include or exclude calls as you want
include("*.jar")
include("another-library.aar") // Some Android libraries are in AAR format
exclude("bad-library.jar")
}
)
implementation(
fileTree(
"dir" to "libs/",
// Here, instead of repeating include or exclude, assign a list of paths
"include" to "*.jar",
"exclude" to listOf("bad-library-1.jar", "bad-library-2.jar")
)
)
The above code assumes that the library files are in libs/ directory of the module (by module I mean the directory where this build.gradle.kts is located).
You can use Ant patterns in includes and excludes as shown above.
See Gradle documentations for more information about file dependencies.
Thanks to this post for providing a helpful answer.
I couldn't get the suggestion above at https://stackoverflow.com/a/20956456/1019307 to work. This worked for me though. For a file secondstring-20030401.jar that I stored in a libs/ directory in the root of the project:
repositories {
mavenCentral()
// Not everything is available in a Maven/Gradle repository. Use a local 'libs/' directory for these.
flatDir {
dirs 'libs'
}
}
...
compile name: 'secondstring-20030401'
The best way to do it is to add this in your build.gradle file and hit the sync option
dependency{
compile files('path.jar')
}
The solution which worked for me is the usage of fileTree in build.gradle file.
Keep the .jar which need to add as dependency in libs folder. The give the below code in dependenices block in build.gradle:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
You can add jar doing:
For gradle just put following code in build.gradle:
dependencies {
...
compile fileTree(dir: 'lib', includes: ['suitetalk-*0.jar'])
...
}
and for maven just follow steps:
For Intellij:
File->project structure->modules->dependency tab-> click on + sign-> jar and dependency->select jars you want to import-> ok-> apply(if visible)->ok
Remember that if you got any java.lang.NoClassDefFoundError: Could not initialize class exception at runtime this means that dependencies in jar not installed for that you have to add all dependecies in parent project.
For Gradle version 7.4 with Groovy build file
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
implementation ':gson-2.2.4'
}
If you are on gradle 4.10 or newer:
implementation fileTree(dir: 'libs', includes: ['*.jar'])
Goto File -> Project Structure -> Modules -> app -> Dependencies Tab -> Click on +(button) -> Select File Dependency - > Select jar file in the lib folder
This steps will automatically add your dependency to gralde
Very Simple
Be careful if you are using continuous integration, you must add your libraries in the same path on your build server.
For this reason, I'd rather add jar to the local repository and, of course, do the same on the build server.
An other way:
Add library in the tree view. Right click on this one. Select menu "Add As Library".
A dialog appear, let you select module. OK and it's done.
I am writing an android sdk and I now want to distribute it but I am having problems with its dependencies. I am using gradle and android studio.
My sdk has a dependency on volley and gson and I have them added as jars with in my sdk. When ever I try to create an aar or a jar to use within a separate client app I always get a crash when ever my sdk tries to reference volley as it wasnt included in either the aar or jar.
Any ideas on how I should be doing this? I have looked into creating fat jars with out much success.
I have also tried using remote dependencies like below but even after a gradle build in the client app both volley and gson are still not included.
This is currently what I have in my sdk build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.mcxiaoke.volley:library-aar:1.0.0'
compile 'com.google.code.gson:gson:2.3'
}
then with in the client build gradle I have
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile(name:'mysdk-1.0.0', ext:'aar')
}
I am using ./gradlew clean build to build the aar and jar of my sdk
Can anyone tell me the correct way to include dependencies in my own lib?
If you wish to include the transitive dependencies of your library in your client app, you'll need to create a proper maven compatible package that contains a pom.xml. This pom will describe the dependencies of your lib, so that client can pull those deps transitively when they include your lib.
See this tutorial: http://blog.blundell-apps.com/locally-release-an-android-library-for-jcenter-or-maven-central-inclusion/
You don't have to upload the artifact anywhere during development, just use your local maven repo as a target, and have mavenLocal() as a repository for your client app.
This way, you'll be able to refer to your lib as a standard maven dependency using
compile 'com.mysdk:mysdk:1.0.0'
In your client, change your dependencies block like so:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile(name:'mysdk-1.0.0', ext:'aar') {
transitive = true
}
}
This should pull in your transitive dependencies as well.
I have imported an Android Studio project as a library for another of my projects. My original question for that is here. It worked excellently for a while, until today. Today I finished updating my library (in its own project) and copied the new files over. I edited the settings.gradle and build.gradle files as I did before but I am getting the error:
Project with path 'Eed:libraries:Overt:Util' could not be found in project ':CES'.
The library's original project is structured like this:
Overt/
+ Visible/
+ Control/
+ CES/
+ Util/
In that project, CES is dependent upon Util
dependencies {
compile 'com.android.support:appcompat-v7:19.+'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':Util')
}
When I copied the new version of Overt over I modified the project level settings.gradle and module level build.gradle.
Structure:
Eed/
Eed/
libraries/
Overt/
+ Visible/
+ Control/
+ CES/
+ Util/
settings.gradle
include ':Eed'
include ':Eed:libraries:Overt:Visible'
include ':Eed:libraries:Overt:Control'
include ':Eed:libraries:Overt:Util'
include ':Eed:libraries:Overt:CES'
and build.gradle (Eed module)
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project('libraries:Overt:Visible')
compile project('libraries:Overt:Control')
compile project('libraries:Overt:Util')
compile project('libraries:Overt:CES')
}
I have played with modifying the build.gradle in CES but cannot figure out a solution. What am I doing wrong?
If your CES project still has this dependency, then it looks fishy:
compile project(':Util')
It works best if your dependency statements use the same paths as what appears in settings.gradle, to wit:
compile project(':Eed:libraries:Overt:Util')
I don't know if you can build up a relative Gradle project path to refer to a sibling project at the same level as the referring project. What I mean is that CES and Util are both at the same level, :Eed:libraries:Overt:, what what you really want is something like what you can do in a filesystem path with ../Util, but I don't know offhand if that's possible in Gradle.
It could be that if you're expecting your complex module to be able to be imported somewhere and still maintain its module relationships with relative paths, you may be asking a lot. Having said that, if it's a firm requirement, then you can look into remapping directories in settings.gradle via projectDir. See Gradle subproject name different than folder name for a hint on how to get started with that if you want to go that route.
I am trying to import my own library into one of my apps in Android Studio. I have seen instructions in several places that basically amount to :
1 Move the files into a library folder within your project diectory.
2 Add include 'projectName:folderName:libraryName to your settings.gradle file
3 Add compile project('folderName:libraryName') under dependencies to your project level build.gradle file
This works for me if I am importing a module... I think its a module. I'm still trying to figure out gradle. My library structure looks like this:
ProjectName
Module1
Classes
Module2
Classes2
If I import Module1 or Module2 separately in this fashion (where Module1 is my 'libraryName' in the instructions above), they will work. But if I try to import the whole project as one library I get errors.
What am I doing wrong? I would like to import the entire project as I would like it to have many modules. Is there another/better way to do this? What am I misunderstanding?
Update:
As per #ScottBarta 's instructions I merged settings.gradle files. This appears to have worked in my main project because the gradle sync completed properly but I'm not ready to run that project. So I made a tester project and imported the library using the same steps. This time I get the error:
Gradle 'Tester' project refresh failed:
Build script error, unsupported Gradle DSL method found. 'compile()'!
As near as I can tell, the files are equivalent in the two projects:
Main:
include ':Eed'
include ':Eed:libraries:Overt:Visible'
include ':Eed:libraries:Overt:Control'
Test:
include ':app'
include ':app:libraries:Overt:Visible'
include ':app:libraries:Overt:Control'
Main:
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project('libraries:Overt:Visible')
compile project('libraries:Overt:Control')
}
Test
dependencies {
compile 'com.android.support:appcompat-v7:19.+'
compile fileTree(dir: 'libs', include: ['*.jar'])
complie project('libraries:Overt:Visible')
complie project('libraries:Overt:Control')
}
And here's a screenshot of the file structure in the Tester project.
You can only have one settings.gradle file per project, so if you have a multi-module library with its own settings.gradle, you can't bring it in with a single include statement. You'll need to merge the library's settings.gradle into that of the project you're including it in.
When you do the merge, make sure that all the include statements in the merged settings.gradle have sensible paths that will locate their libraries relative to the project root. Also keep in mind that the paths specified in your settings.gradle file have to match what's in your dependencies block. So if you have this:
include ':app'
include ':app:libraries:Overt:Visible'
include ':app:libraries:Overt:Control'
You need this:
dependencies {
compile 'com.android.support:appcompat-v7:19.+'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':app:libraries:Overt:Visible')
compile project(':app:libraries:Overt:Control')
}
In the build script you posted in your question, there's a typo: it says complie instead of compile. If that's a typo in your build script and not just a typo in your question here, that will be a problem.