Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 705
Wang-Zhao-Liu Q Avatar asked Dec 20 '13 09:12

Wang-Zhao-Liu Q


People also ask

How do I add a dependency in build Gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.

Where does Gradle store local dependencies?

The Gradle dependency cache consists of two storage types located under GRADLE_USER_HOME/caches : A file-based store of downloaded artifacts, including binaries like jars as well as raw downloaded meta-data like POM files and Ivy files.


2 Answers

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")) } 
like image 176
leeor Avatar answered Sep 28 '22 05:09

leeor


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 name: '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' } 
like image 30
Jorge_B Avatar answered Sep 28 '22 04:09

Jorge_B