Home > Java > javaTutorial > How to Properly Include Local JAR Dependencies in Gradle?

How to Properly Include Local JAR Dependencies in Gradle?

Susan Sarandon
Release: 2024-12-25 00:51:10
Original
285 people have browsed it

How to Properly Include Local JAR Dependencies in Gradle?

Including Local JAR Dependencies in Gradle Buildscripts

In Gradle, adding dependencies from local JAR files requires specific configurations within the build.gradle file. An attempt to include local JARs using the following code block may lead to errors:

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')
}
Copy after login

Solution:

To successfully include local JAR dependencies, Gradle recommends using a relative path instead. The corrected code block for Groovy syntax is:

dependencies {
    implementation files('libs/something_local.jar')
}
Copy after login

For Kotlin syntax, the code would be:

dependencies {
    implementation(files("libs/something_local.jar"))
}
Copy after login

By following this approach, Gradle can locate and include the specified JAR dependencies during the build process. This resolves the issue where packages from the JAR files are not recognized, as seen in the error message:

error: package com.google.gson does not exist
import com.google.gson.Gson;
Copy after login

The above is the detailed content of How to Properly Include Local JAR Dependencies in Gradle?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template