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

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

问题描述:

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

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

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

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

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

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

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

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

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

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

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

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

repositories {
   flatDir {
       dirs 'libs'
   }
}


dependencies {
   implementation name: 'gson-2.2.4'
}

然而,作为实际 Maven 存储库中的标准 .jar,您为什么不试试这个?

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

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