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:
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
Kotlin DSL (build.gradle.kts):
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
Kotlin DSL:
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:
Method 3: a flatDir repository
A flat directory repository lets you reference jars by name, without the .jar extension:
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:
Then reference it by coordinates:
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
| Method | Versioning | Transitive Deps | Team Friendly | Setup Effort |
files() | Manual | No | Low | Minimal |
fileTree() | Manual | No | Low | Minimal |
flatDir | Manual | No | Medium | Low |
mavenLocal() | Proper | Yes (with POM) | High | Moderate |
| Private repository (Nexus/Artifactory) | Proper | Yes | High | High |
Paths in multi-module projects
Paths in files() and fileTree() are relative to the module's directory, not the root project:
To point at the root project's libs directory:
To share a set of jars across all modules, declare them in the root build.gradle with subprojects or allprojects:
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.
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
.gitignoreor 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
ClassNotFoundExceptionorNoSuchMethodErrorat runtime. Inspect the full tree with./gradlew dependencies. - Using
compile. Thecompileconfiguration is deprecated since Gradle 3.0. Useimplementation(private to the module) orapi(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
libsdoes not always trigger a rebuild. Run./gradlew cleanafterwards.
Related reading
- How to add local jar files to a Maven project?
- How to add multiple application.properties files in spring-boot?
- How to add parameters to HttpURLConnection using POST using NameValuePair
- How to add reference to a method parameter in javadoc?
- How to additionally configure autocreated Spring Boot beans?
- How to allow a User only access their own data in Spring Boot / Spring Security?
- How to alter databasechangelog.filename for Spring Boot and Liquibase?
- How to analyze a java thread dump?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.