723

所以我尝试将我的本地 .jar 文件依赖项添加到我的 build.gradle 文件中:

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

您可以看到我将 .jar 文件添加到了这里的 referencedLibraries 文件夹中:https ://github.com/WalnutiQ/wAlnut/tree/version-2.3.1/referencedLibraries

但问题是,当我在命令行上运行命令时: gradle build 我收到以下错误:

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

这是我的整个回购:https ://github.com/WalnutiQ/wAlnut/tree/version-2.3.1

4

19 回答 19

851

根据文档,对本地 jar 依赖项使用相对路径,如下所示。

Groovy 语法:

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

Kotlin 语法:

dependencies {
    implementation(files("libs/something_local.jar"))
}
于 2014-01-06T18:12:22.170 回答
529

如果您确实需要从本地目录中获取该 .jar,

在您的模块 gradle 旁边添加(不是应用程序 gradle 文件):

repositories {
   flatDir {
       dirs 'libs'
   }
}


dependencies {
   implementation name: 'gson-2.2.4'
}

但是,作为实际 maven 存储库中的标准 .jar,您为什么不尝试一下呢?

repositories {
   mavenCentral()
}
dependencies {
   implementation 'com.google.code.gson:gson:2.2.4'
}
于 2013-12-20T09:21:49.007 回答
347

您也可以这样做,这将包括本地存储库中的所有 JAR。这样您就不必每次都指定它。

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
于 2014-05-27T19:43:25.080 回答
60

以下对我有用:

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

请参阅Gradle 文档

于 2017-08-05T17:05:33.253 回答
45

您可以尝试为 Gradle 重用本地 Maven 存储库:

  • 将 jar 安装到本地 Maven 存储库中:

    mvn install:install-file -Dfile=utility.jar -DgroupId=com.company -DartifactId=utility -Dversion=0.0.1 -Dpackaging=jar

  • 检查您是否已将 jar 安装到~/.m2/本地 Maven 存储库 中

  • 在文件中启用本地 Maven 存储库build.gradle

    repositories {
      mavenCentral()  
      mavenLocal()  
    }
    
    dependencies {  
      implementation ("com.company:utility:0.0.1")  
    }
    
    • 现在您应该启用 jar 以在您的项目中实现
于 2016-12-23T01:40:45.743 回答
36

使用 Kotlin DSL 的解决方案

到目前为止添加的解决方案非常适合 OP,但如果不先翻译它们就不能与 Kotlin DSL 一起使用。下面是我如何使用 Kotlin DSL 在构建中添加本地 .JAR 的示例:

dependencies {
    compile(files("/path/to/file.jar"))
    testCompile(files("/path/to/file.jar"))
    testCompile("junit", "junit", "4.12")
}

请记住,如果您使用的是 Windows,则必须对反斜杠进行转义:

...
compile(files("C:\\path\\to\\file.jar"))
...

还要记住,引号必须是双引号,而不是单引号。


2020年编辑:

Gradle 更新已弃用compiletestCompile支持implementationtestImplementation. 因此,对于当前的 Gradle 版本,上述依赖块看起来像这样:

dependencies {
    implementation(files("/path/to/file.jar"))
    testImplementation(files("/path/to/file.jar"))
    testImplementation("junit", "junit", "4.12")
}
于 2019-02-11T20:08:10.073 回答
35

公认的答案很好,但是,我需要在我的多项目 Gradle 构建中使用各种库配置才能使用相同的第 3 方 Java 库。

将“ $rootProject.projectDir ”添加到我的“ allprojects ”闭包中的“ dir ”路径元素意味着每个子项目都引用了相同的“ libs ”目录,而不是该子项目的本地版本:

//gradle.build snippet
allprojects {
    ...

    repositories {
        //All sub-projects will now refer to the same 'libs' directory
        flatDir {
            dirs "$rootProject.projectDir/libs"
        }
        mavenCentral()
    }

    ...
}

由 Quizzie 编辑:将“${rootProject.projectDir}”更改为“$rootProject.projectDir”(适用于最新的 Gradle 版本)。

于 2014-12-10T16:28:27.260 回答
18

较短的版本:

dependencies {
    implementation fileTree('lib')
}
于 2020-06-22T18:06:44.820 回答
14

一个简单的方法是

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

它将编译 App 中 libs 目录中的所有 .jar 文件。

于 2015-08-18T09:10:47.290 回答
14

问题已经详细回答了。我仍然想添加一些让我感到非常惊讶的东西:

“gradle 依赖项”任务没有列出任何文件依赖项。即使您可能这么认为,毕竟它们已经在“依赖项”块中指定了..

所以不要依赖它的输出来检查你引用的本地 lib 文件是否正常工作。

于 2015-10-30T16:07:37.073 回答
8

我无法在https://stackoverflow.com/a/20956456/1019307上获得上述建议。不过,这对我有用。对于secondstring-20030401.jar我存储在libs/项目根目录中的文件:

repositories {
    mavenCentral()
    // Not everything is available in a Maven/Gradle repository.  Use a local 'libs/' directory for these.
    flatDir {
       dirs 'libs'
   }
}

...

compile name: 'secondstring-20030401'
于 2015-03-18T06:40:52.693 回答
7

最好的方法是将其添加到您的 build.gradle 文件中并点击同步选项

dependency{
    compile files('path.jar')
}
于 2016-08-12T08:04:43.747 回答
7

对我有用的解决方案是在 build.gradle 文件中使用 fileTree。将需要添加为依赖项的 .jar 保留在 libs 文件夹中。在 build.gradle 的dependenices 块中给出以下代码:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
于 2016-08-24T05:21:14.653 回答
4

您可以添加罐子做:

对于 gradle,只需将以下代码放入build.gradle

dependencies {
...
compile fileTree(dir: 'lib', includes: ['suitetalk-*0.jar'])
...
}

对于 Maven,只需按照以下步骤操作:

对于Intellij:文件->项目结构->模块->依赖选项卡->单击+符号-> jar和依赖项->选择要导入的jar->确定->应用(如果可见)->确定

请记住,如果您java.lang.NoClassDefFoundError: Could not initialize class在运行时遇到任何异常,这意味着未安装 jar 中的依赖项,您必须在父项目中添加所有依赖项。

于 2019-07-05T10:57:32.520 回答
1

使用 Kotlin DSL (build.gradle.kts) 添加本地库文件的更多方法:

implementation(
    files(
        "libs/library-1.jar",
        "libs/library-2.jar",
        "$rootDir/foo/my-other-library.jar"
    )
)
implementation(
    fileTree("libs/") {
        // You can add as many include or exclude calls as you want
        include("*.jar")
        include("another-library.aar") // Some Android libraries are in AAR format
        exclude("bad-library.jar")
    }
)
implementation(
    fileTree(
        "dir" to "libs/",
        // Here, instead of repeating include or exclude, assign a list of paths
        "include" to "*.jar",
        "exclude" to listOf("bad-library-1.jar", "bad-library-2.jar")
    )
)

上面的代码假设库文件位于模块的libs/目录中(我所说的模块是指这个 build.gradle.kts 所在的目录)。

您可以在s 和s 中使用Ant 模式,如上所示。includeexclude

有关文件依赖项的更多信息,请参阅Gradle 文档

感谢这篇文章提供了一个有用的答案。

于 2022-01-25T14:05:59.307 回答
0

对于带有 Groovy 构建文件的 Gradle 7.4 版

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    implementation ':gson-2.2.4'
}
于 2022-03-05T12:50:57.960 回答
-1

转到文件->项目结构->模块->应用程序->依赖项选项卡->单击+(按钮)->选择文件依赖项->选择lib文件夹中的jar文件

此步骤将自动将您的依赖项添加到 gradde

非常简单

于 2016-03-17T07:19:03.750 回答
-1

如果您使用持续集成,请小心,您必须将库添加到构建服务器上的相同路径中。

出于这个原因,我宁愿将 jar 添加到本地存储库,当然,在构建服务器上也这样做。

于 2017-10-17T11:17:54.670 回答
-3

其他方式:

在树视图中添加库。右键单击这个。选择菜单“添加为库”。出现一个对话框,让您选择模块。好的,它完成了。

于 2014-12-16T11:10:39.483 回答