Home » Java » How to add local .jar file dependency to build.gradle file?

How to add local .jar file dependency to build.gradle file?

Posted by: admin November 2, 2017 Leave a comment

Questions:

So 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

Answers:

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 {
   compile name: 'gson-2.2.4'
}

However, being a standard .jar in an actual maven repository, why don’t you try this?

repositories {
   mavenCentral()
}
dependencies {
   compile 'com.google.code.gson:gson:2.2.4'
}

Questions:
Answers:

According to the documentation, use a relative path for a local jar dependency as follows:

dependencies {
    compile files('libs/something_local.jar')
}

Questions:
Answers:

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'])
}

Questions:
Answers:

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).

Questions:
Answers:

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.

Questions:
Answers:

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 {

    compile (“com.company:utility:0.0.1”)

    }

    • Now you should have the jar enabled for compilation in your project
Questions:
Answers:

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'

Questions:
Answers:

The Question already has been answered in detail. I still want to add something that seems very surprising for 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.

Questions:
Answers:

The following works for me:

compile fileTree(dir: 'libs', include: '*.jar')

Refer to the Gradle Documentation.

Questions:
Answers:

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

Questions:
Answers:

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')
}

Questions:
Answers:

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'])
}

Questions:
Answers:

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.

Questions:
Answers:

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.