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

How to Properly Include Local JAR Dependencies in Gradle?

Susan Sarandon
Susan SarandonOriginal
2024-12-25 00:51:10270browse

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

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

For Kotlin syntax, the code would be:

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

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;

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!

Statement:
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