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

Solution 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"))
}

Solution 2:

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

Solution 3:

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

Solution 4:

The following works for me:

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

Refer to the Gradle Documentation.