Access Kotlin .jar from Intellij Gradle-Kotlin-Java project

New to Kotlin, and new to the build tools (Gradle and Maven and Ant, oh my!)

I’ve created a Gradle project in Intellij, and selected both Java and Kotlin/JVM under ‘Additional Libraries and Frameworks.’

In project root, I created a ‘lib’ directory, and added a java .jar file.

In gradle.build I added the following under ‘dependencies’
implementation fileTree(dir: ‘lib’, include: [‘*.jar’])

After building, I was able to access the package in the java .jar file.

I then added a kotlin .jar file. But, am unable to use import statements for packages within the Kotlin .jar.

Is there a separate config needed in build.gradle to allow access to Kotlin .jar files?

Maybe it’s not properly included in the project?
Here’s some stackoverflow on depending on local jar files: java - How to add local .jar file dependency to build.gradle file? - Stack Overflow

1 Like

Well, I tried changing:
implementation fileTree(dir: ‘lib’, include: [‘*.jar’])

To:
implementation fileTree(dir: ‘lib’, includes: [‘*.jar’])

And was able to import the Kotlin lib.

Then I changed it back to:
implementation fileTree(dir: ‘lib’, include: [‘*.jar’])

And was able to import the Kotlin lib…

No idea, but thanks for the link. :+1:

A follow-up:

I created a new project, with a ‘lib’ directory for my .jars, using build.gradle config:

dependencies {

implementation fileTree(dir: ‘lib’, includes: [‘*.jar’])
}

I added 2 libs (.jar files) and was able to import.

I then added a third jar, and saw red on typing import statement for package in newly added lib with error “Cannot resolve symbol…”

So, once again I changed line to
implementation fileTree(dir: ‘lib’, includes: [‘*.jar’])

And, was able to import. Changed it back to ‘include’, and could still import.

So, apparently, forcing gradle to re-read the configuration seems to fix the issue.
But, it does seem to be some kind of bug.

Here’s my specs:

Mac OS Catalina: 10.15.7
Intellij: 2020.2.3
java: 15.0.1
gradle: 6.7

When changing things you need to led intellij reload the gradle config (it may or may not do it automatically depending on your config) and do the indexing, it can take some time depending on the size of the library.

1 Like

It may also depend on what you change. There is a good chance that intellij won’t monitore local file changes in your lib directory since this is not how gradle is generally used.
If you have problems with imports you can trigger the gradle config reload manually. That should solve the issue you describe.

When changing things you need to led intellij reload the gradle config (it may or may not do it automatically depending on your config) and do the indexing

I guess editing build.gradle is one way? Is there a way to do this from the Gradle tab?

I got this from a post on another forum:
Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project

Was having trouble, in the IDEA gradle project, to get the .jar to be recognized by any of the gui methods (adding module in project structure, right-click and ‘Add as library’, etc.)

There should be a button for it in the gradle tool window. I don’t have intellij open right now so I can’t tell you how it looks like but I think it’s a generic reload icon. I think you can open the tool window under View>Tools>Gradle. You cou also add a keyboard shortcut for this (both opening the gradle window and the reload).

Your build.gradle file looks correct. I just wanted to add some context to @bjonnh’s answer why intellij might not recognise the change. IDEA only monitores the gradle files themself.

The GUI methods only work with IDEA internal build system. Gradle is spearate from that. That said some changes in there eg. JVM version do affect the gradle build as well. It’s a bit complicated, but in general you need to define all build settings in .gradle files.

1 Like

Ok, thanks. Found this @ upper left, which I assume is it:

image

1 Like