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

本文翻译自:How to add local .jar file dependency to build.gradle file?

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


#1楼

参考:https://stackoom.com/question/1Or1p/如何将本地-jar文件依赖项添加到build-gradle文件


#2楼

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

#3楼

According to the documentation , use a relative path for a local jar dependency as follows: 根据文档 ,对本地jar依赖项使用相对路径,如下所示:

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

#4楼

You could also do this which would include all JARs in the local repository. 您也可以这样做,将本地存储库中的所有JAR都包括在内。 This way you wouldn't have to specify it every time. 这样,您不必每次都指定它。

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

#5楼

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构建中,我需要各种库配置才能使用相同的第三方Java库。

Adding ' $rootProject.projectDir ' to the ' dir ' path element within my ' allprojects ' closure meant each sub-project referenced the same ' libs ' directory, and not a version local to that sub-project: 在我的' allprojects '闭包中的' dir '路径元素中添加' $ rootProject.projectDir '意味着每个子项目都引用相同的' 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版本中有效)。


#6楼

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. 好,就完成了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值