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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20700053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:39:20  来源:igfitidea点击:

How to add local .jar file dependency to build.gradle file?

javagradledependency-managementbuild.gradlegradle-eclipse

提问by Q Zhao-Liu

So I have tried to add my local .jar file dependency to my build.gradle file:

所以我尝试将我的本地 .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')
} 

And you can see that I added the .jar files into the referencedLibraries folder here: https://github.com/WalnutiQ/wAlnut/tree/version-2.3.1/referencedLibraries

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

But the problem is that when I run the command: gradle build on the command line I get the following error:

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

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

Here is my entire repo: https://github.com/WalnutiQ/wAlnut/tree/version-2.3.1

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

采纳答案by Jorge_B

If you really need to take that .jar from a local directory,

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

Add next to your module gradle (Not the app gradle file):

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

repositories {
   flatDir {
       dirs 'libs'
   }
}


dependencies {
   implementation name: 'gson-2.2.4'
}

However, being a standard .jar in an actual maven repository, why don't you try this?

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

repositories {
   mavenCentral()
}
dependencies {
   implementation 'com.google.code.gson:gson:2.2.4'
}

回答by leeor

According to the documentation, use a relative path for a local jar dependency as follows:

根据文档,使用本地 jar 依赖项的相对路径如下:

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

回答by Nightwolf

You could also do this which would include all JARs in the local repository. This way you wouldn't have to specify it every time.

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

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

回答by Big Rich

The accepted answer is good, however, I would have needed various library configurations within my multi-project Gradle build to use the same 3rd-party Java library.

接受的答案很好,但是,我需要在我的多项目 Gradle 构建中进行各种库配置才能使用相同的 3rd-party Java 库。

Adding '$rootProject.projectDir' to the 'dir' path element within my 'allprojects' closure meant each sub-project referenced the same 'libs' directory, and nota version local to that sub-project:

将“ $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()
    }

    ...
}

EDIT by Quizzie: changed "${rootProject.projectDir}" to "$rootProject.projectDir" (works in the newest Gradle version).

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

回答by YannXplorer

An other way:

其它的办法:

Add library in the tree view. Right click on this one. Select menu "Add As Library". A dialog appear, let you select module. OK and it's done.

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

回答by HankCa

I couldn't get the suggestion above at https://stackoverflow.com/a/20956456/1019307to work. This worked for me though. For a file secondstring-20030401.jarthat I stored in a libs/directory in the root of the project:

我无法在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'

回答by ZygoteInit

A simple way to do this is

一个简单的方法是

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

it will compile all the .jar files in your libs directory in App.

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

回答by icyerasor

The Question already has been answered in detail. I still want to add something that seems very surprising to me:

题主已经详细回答了。我仍然想添加一些令我感到非常惊讶的内容:

The "gradle dependencies" task does not list any file dependencies. Even though you might think so, as they have been specified in the "dependencies" block after all..

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

So don't rely on the output of this to check whether your referenced local lib files are working correctly.

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

回答by Jaya

Goto File -> Project Structure -> Modules -> app -> Dependencies Tab -> Click on +(button) -> Select File Dependency - > Select jar file in the lib folder

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

This steps will automatically add your dependency to gralde

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

Very Simple

很简单

回答by saunlogan

The best way to do it is to add this in your build.gradle file and hit the sync option

最好的方法是在 build.gradle 文件中添加它并点击同步选项

dependency{
    compile files('path.jar')
}