简体   繁体   English

如何使用Gradle将原生依赖项添加到Java项目中

[英]How to add native dependencies to in java project using gradle

I am working on a project using opencv. 我正在使用opencv进行项目。 I have successfully created the required jar and native libs for macOS platform. 我已经成功创建了macOS平台所需的jar和本机库。 I now want to use this in my java project. 我现在想在我的java项目中使用它。 I am using gradle for build and dependency management. 我正在使用gradle进行构建和依赖项管理。 I have added the jar and native dependency in lib folder of my project. 我在项目的lib文件夹中添加了jar和本机依赖项。

build.gradle 的build.gradle

group 'com.udaykale'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile(
            files('lib/opencv-320.jar')
    )
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

System.setProperty("java.library.path", "lib")

I am getting the following error: 我收到以下错误:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java320 in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
    at java.lang.Runtime.loadLibrary0(Runtime.java:870)
    at java.lang.System.loadLibrary(System.java:1122)
    at com.udaykale.imageeditor.Main.<clinit>(Main.java:13)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)

I understand that I the native library is not visible at runtime. 我知道本机库在运行时不可见。 How do I change my gradle config to handle this? 如何更改gradle配置以解决此问题?

Local install the opencv-320.jar file with maven and use it as a dependency in your build.gradle.To local install the artifact use following command. 使用maven在本地安装opencv-320.jar文件,并在build.gradle中将其用作依赖项。要在本地安装工件,请使用以下命令。 Specify the version, group id and artifact id accordingly. 相应地指定版本,组标识和工件标识。

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> 
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=jar

Also add local repositories to build.gradle 同时将本地存储库添加到build.gradle

repositories {
 mavenLocal()
}

And then the dependency 然后依赖

dependencies {
 compile "<group-id>:<artifact-id>:<version>"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM