gradle java library,在我的Gradle Java项目中使用本地jar作为依赖项

I have a local jar file named mylib.jar. I want to used it as a dependency in my Gradle Java project.

This is what I tried:

I created a libs/ folder under project root. I put the jar file under libs/ folder.

MyProject

->libs/mylib.jar

->build.gradle

->src/...

In my build.gradle:

apply plugin: 'java-library'

group 'com.my.app'

version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {

mavenCentral()

flatDir {

dirs 'libs'

}

}

dependencies {

testCompile group: 'junit', name: 'junit', version: '4.12'

api files('libs/mylib.jar')

}

But I can't access the public classes defined in mylib.jar in my project code. Why?

===== More information =====

The content of my jar:

mylib.jar

> com.my.jar.package

>ClassFromJar.class

Here is how I use the jar:

// Compilation error: Cannot resolve symobl 'ClassFromJar'

import com.my.jar.package.ClassFromJar;

public class MyEntryPoint {

// Compilation error: Cannot resolve symbol 'ClassFromJar'

ClassFromJar instance = new ClassFromJar();

}

解决方案

Local dir

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

repositories {

flatDir {

dirs 'libs'

}

}

Relative path:

dependencies {

implementation files('libs/mylib.jar')

}

Use compile fileTree:

compile fileTree(dir: 'libs', include: 'mylib.jar')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值