Gradle
Build.gradle
Java
.jar file
Dependency Management

How to add a local .jar file dependency in build.gradle

Codemia Knowledge Hub ·

Use implementation files('libs/example.jar') in the dependencies block. For every jar in a directory, use implementation fileTree(dir: 'libs', include: ['*.jar']). The two differ in how well they scale, how visible each jar is, and how reproducible the build stays.

Local jars are usually needed for proprietary libraries that are not published anywhere, patched forks of open-source libraries, legacy projects without a repository manager, offline builds in air-gapped environments, and vendor SDKs distributed as jar downloads.

Where to put the jars

The common convention is a libs directory at the root of the project or module:

 
1my-project/
2  libs/
3    payment-sdk-2.1.0.jar
4    internal-utils-1.0.jar
5  src/
6    main/
7      java/
8  build.gradle

In a multi-module project, each module can have its own libs directory, or the modules can share one at the root.

Method 1: reference each file

groovy
1dependencies {
2    implementation files('libs/payment-sdk-2.1.0.jar')
3    implementation files('libs/internal-utils-1.0.jar')
4}

Kotlin DSL (build.gradle.kts):

kotlin
1dependencies {
2    implementation(files("libs/payment-sdk-2.1.0.jar"))
3    implementation(files("libs/internal-utils-1.0.jar"))
4}

This is explicit: you know exactly which jars are included. Adding a new jar means editing the build file.

Method 2: include a whole directory with fileTree

groovy
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
}

Kotlin DSL:

kotlin
dependencies {
    implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
}

This scales to many jars but hides what is included. Any jar dropped into libs is picked up without a declaration, which can cause surprises. You can filter by pattern:

groovy
1dependencies {
2    // Include only specific jars
3    implementation fileTree(dir: 'libs', include: ['payment-*.jar', 'internal-*.jar'])
4
5    // Exclude certain jars
6    implementation fileTree(dir: 'libs', include: ['*.jar'], exclude: ['*-test.jar'])
7}

Method 3: a flatDir repository

A flat directory repository lets you reference jars by name, without the .jar extension:

groovy
1repositories {
2    flatDir {
3        dirs 'libs'
4    }
5}
6
7dependencies {
8    implementation name: 'payment-sdk-2.1.0'
9}

Gradle looks in libs/ and infers the extension. This fits Gradle's dependency resolution more naturally, but flat directory repositories do not support metadata (POM files) or transitive dependencies, so Gradle cannot resolve the jar's own dependencies.

Method 4: install into the local Maven repository (best for teams)

For reproducible builds and real versioning, install the jar into the local Maven cache:

bash
1mvn install:install-file \
2  -Dfile=libs/payment-sdk-2.1.0.jar \
3  -DgroupId=com.example \
4  -DartifactId=payment-sdk \
5  -Dversion=2.1.0 \
6  -Dpackaging=jar

Then reference it by coordinates:

groovy
1repositories {
2    mavenLocal()
3    mavenCentral()
4}
5
6dependencies {
7    implementation 'com.example:payment-sdk:2.1.0'
8}

This gives proper versioning and works with normal dependency resolution and existing repository infrastructure. The jar lives in ~/.m2/repository/ like any other Maven artifact.

Comparing the methods

MethodVersioningTransitive DepsTeam FriendlySetup Effort
files()ManualNoLowMinimal
fileTree()ManualNoLowMinimal
flatDirManualNoMediumLow
mavenLocal()ProperYes (with POM)HighModerate
Private repository (Nexus/Artifactory)ProperYesHighHigh

Paths in multi-module projects

Paths in files() and fileTree() are relative to the module's directory, not the root project:

groovy
1// In app/build.gradle, this looks in app/libs/
2dependencies {
3    implementation files('libs/payment-sdk-2.1.0.jar')
4}

To point at the root project's libs directory:

groovy
1// In app/build.gradle, referencing root project libs
2dependencies {
3    implementation files("${rootProject.projectDir}/libs/payment-sdk-2.1.0.jar")
4}

To share a set of jars across all modules, declare them in the root build.gradle with subprojects or allprojects:

groovy
1// In root build.gradle
2subprojects {
3    dependencies {
4        implementation fileTree(dir: "${rootProject.projectDir}/libs", include: ['*.jar'])
5    }
6}

Android projects

The same patterns apply. Android Gradle Plugin versions before 3.0 used compile instead of implementation; modern projects should use implementation or api.

groovy
1// Android module build.gradle
2android {
3    // ...
4}
5
6dependencies {
7    implementation fileTree(dir: 'libs', include: ['*.jar'])
8    // For AAR files
9    implementation fileTree(dir: 'libs', include: ['*.aar'])
10}

Android Studio generates this fileTree declaration by default in new projects, which is why many Android developers meet local jar dependencies early.

Problems with local jars

  • The jar is not committed. If it is in .gitignore or never committed, the build breaks for everyone else. Commit the jars or use a shared repository.
  • Classes clash with remote dependencies. A local jar may bundle classes that overlap with a remote dependency, causing ClassNotFoundException or NoSuchMethodError at runtime. Inspect the full tree with ./gradlew dependencies.
  • Using compile. The compile configuration is deprecated since Gradle 3.0. Use implementation (private to the module) or api (exposed to consumers).
  • No transitive dependencies. Local jars carry no POM metadata. Declare their dependencies by hand, or use mavenLocal() with a proper POM.
  • Stale jars. Replacing a jar in libs does not always trigger a rebuild. Run ./gradlew clean afterwards.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms