如何将本地的.jar文件依赖添加到build.gradle文件中?

812

我尝试将本地的 .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


你确定要使用运行时而不是编译时吗? 编译文件(...) - Gabriele Mariotti
3
如果无法构建JAR包,则看起来是编译依赖关系。 尝试: compile files('libs/mnist-tools.jar', 'libs/gson-2.2.4.jar')如果仍有问题,请尝试绝对路径,因为那也可能是问题所在。 - homik
1
这个Stack Overflow问题最终给了我需要的答案。 - craned
这个回答解决了你的问题吗?Android Studio:如何将jar文件添加为库? - Mahozad
22个回答

10

使用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 的目录)。

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

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

感谢此帖子提供了有用的答案。


8

我无法理解上面在https://dev59.com/nmIj5IYBdhLWcg3wCg_X#20956456提到的建议,但是下面这个方法对我有用。对于一个文件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'

谢谢!不知道为什么,但只有这个方法对我有效。在我的项目根目录下创建了一个“libs”文件夹,把.jar文件放进去,并复制了你的“compile name”条目。当然,也删除了.jar扩展名。 - fIwJlxSzApHEZIl
不行,这对我没用。找不到:miglayout-core:。 在以下位置搜索: file:/user/path/to/miglayout-core.jar - Well Smith
如果您想将文件目录添加为存储库,请使用以下方法:https://docs.gradle.org/current/userguide/repository_types.html#sec:flat_dir_resolver - Grubhart

7
最好的方法是将以下内容添加到您的build.gradle文件中,然后点击同步选项。
dependency{
    compile files('path.jar')
}

7
我用的解决方案是在build.gradle文件中使用fileTree。 将需要作为依赖项添加的.jar文件放入libs文件夹中,然后在build.gradle的dependencies块中添加以下代码:
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

4

您可以使用以下方式添加jar包:

对于Gradle,请在build.gradle中加入以下代码:

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

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

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

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


3

对于使用Groovy构建文件的Gradle版本7.4

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    implementation ':gson-2.2.4'
}

2
如果您使用的是Gradle 4.10或更高版本:
implementation fileTree(dir: 'libs', includes: ['*.jar'])

0
这在Gradle 8.3中是有效的。如果你在Windows上操作,请参考下面的内容。
repositories { flatDir { dirs 'D:/path/of/jar/lib' } } dependencies { implementation ':json-resource2.4.0' }

0
在您的build.gradle文件的"dependencies definition"中添加以下行:
implementation files("/Users///.jar")
这对我来说有效,可以从我的本地系统中获取特定项目的.jar文件。

-1

如果您正在使用持续集成,请注意,您必须在构建服务器上的相同路径中添加库。

出于这个原因,我更愿意将jar添加到本地存储库中,并在构建服务器上执行相同操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接