所以我尝试将我的本地 .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
根据 the documentation,为本地 jar 依赖项使用相对路径,如下所示。
Groovy 语法:
dependencies {
implementation files('libs/something_local.jar')
}
Kotlin 语法:
dependencies {
implementation(files("libs/something_local.jar"))
}
如果您确实需要从本地目录中获取该 .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'
}
gradle.build
文件 - 这些“应用程序 gradle 文件”和“模块 gradle”是什么?
implementation(":gson:2.2.4")
似乎是这样做的方法。
您也可以这样做,这将包括本地存储库中的所有 JAR。这样您就不必每次都指定它。
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
implementation fileTree(dir: 'lib', includes: ['*.jar'])
(或 api
,取决于您的需要)
以下对我有用:
compile fileTree(dir: 'libs', include: '*.jar')
library/build/outputs/aar/library-release.aar
您可以尝试为 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 存储库中
在 build.gradle 文件中启用本地 Maven 存储库: repositories { mavenCentral() mavenLocal() } dependencies { implementation ("com.company:utility:0.0.1") } 现在您应该启用 jar 以在项目中实现
现在您应该启用 jar 以在您的项目中实现
使用 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 更新已弃用 compile
和 testCompile
,取而代之的是 implementation
和 testImplementation
。因此,对于当前的 Gradle 版本,上述依赖块看起来像这样:
dependencies {
implementation(files("/path/to/file.jar"))
testImplementation(files("/path/to/file.jar"))
testImplementation("junit", "junit", "4.12")
}
compile(project(":MyProject"))
,其中“MyProject”在您的 settings.gradle
文件中定义为类似include("MyProject")
。
公认的答案很好,但是,我需要在我的多项目 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 版本)。
较短的版本:
dependencies {
implementation fileTree('lib')
}
问题已经详细回答了。我仍然想添加一些让我感到非常惊讶的东西:
“gradle 依赖项”任务没有列出任何文件依赖项。即使您可能这么认为,毕竟它们已经在“依赖项”块中指定了..
所以不要依赖它的输出来检查你引用的本地 lib 文件是否正常工作。
一个简单的方法是
compile fileTree(include: ['*.jar'], dir: 'libs')
它将编译 App 中 libs 目录中的所有 .jar 文件。
我无法在 https://stackoverflow.com/a/20956456/1019307 获得上述建议。不过,这对我有用。对于我存储在项目根目录的 libs/
目录中的文件 secondstring-20030401.jar
:
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'
最好的方法是将其添加到您的 build.gradle 文件中并点击同步选项
dependency{
compile files('path.jar')
}
对我有用的解决方案是在 build.gradle 文件中使用 fileTree。将需要添加为依赖项的 .jar 保留在 libs 文件夹中。在 build.gradle 的dependenices 块中给出以下代码:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
您可以添加罐子做:
对于 gradle,只需将以下代码放入 build.gradle
:
dependencies {
...
compile fileTree(dir: 'lib', includes: ['suitetalk-*0.jar'])
...
}
对于 Maven,只需按照以下步骤操作:
对于Intellij:文件->项目结构->模块->依赖选项卡->单击+符号-> jar和依赖项->选择要导入的jar->确定->应用(如果可见)->确定
请记住,如果您在运行时遇到任何 java.lang.NoClassDefFoundError: Could not initialize class
异常,这意味着未安装 jar 中的依赖项,因此您必须在父项目中添加所有依赖项。
使用 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 所在的目录)。
如上所示,您可以在 include
和 exclude
中使用 Ant patterns。
有关文件依赖项的详细信息,请参阅 Gradle documentations。
感谢 this post 提供有用的答案。
对于带有 Groovy 构建文件的 Gradle 7.4 版
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
implementation ':gson-2.2.4'
}
如果您使用的是 gradle 4.10 或更高版本:
implementation fileTree(dir: 'libs', includes: ['*.jar'])
转到文件->项目结构->模块->应用程序->依赖项选项卡->单击+(按钮)->选择文件依赖项->选择lib文件夹中的jar文件
此步骤将自动将您的依赖项添加到 gradde
很简单
build.gradle
时被删除。
如果您使用持续集成,请小心,您必须将库添加到构建服务器上的相同路径中。
出于这个原因,我宁愿将 jar 添加到本地存储库,当然,在构建服务器上也这样做。
另一种方式:
在树视图中添加库。右键单击这个。选择菜单“添加为库”。出现一个对话框,让您选择模块。好的,它完成了。
不定期副业成功案例分享