1

I have created a library in kotlin using gradle init and following the prompts. It compiles and produces a jar file in lib/build/libs. I then have another project that needs to access functions in the library. through intelliJ, I add the dependency by going to Project Settings > Modules > Dependencies and adding the jar file. I then attempt to import the package defined in the library with import DemoLib where DemoLib is the name of the package (and the name of the library). This does not compile as it does not recognize the package name. I have also tried importing as a library rather than a jar, with the same results. How can I achieve the desired result?

EDIT: In case it helps here is the code:

Library:

package DemoLib

class Library {
    fun someLibraryMethod(): Boolean {
        return true
    }
}

Client Code:

package DemoClientAppOne

import DemoLib.*



class App {
    val greeting: String
        get() {
            return "Hello World!"
        }
}

fun main() {
    println(App().greeting)
}

Not terribly interesting, but the point is that DemoLib is an unresolved reference even after adding the jar as a dependency

2 Answers 2

1

You have three options

  1. Combine two projects under same IDE project and use Gradle dependencies on project:

settings.gradle:

include 'project-lib'

build.gradle:

implementation project(':project-lib')

See https://docs.gradle.org/current/userguide/declaring_dependencies.html#sub:project_dependencies

  1. Install the library into local repository and add it as a Gradle dependency:

    apply plugin: 'maven'

then run gradle publishToMavenLocal

and add mavenLocal() repository in gradle.build file.

See How to install a compiled by Gradle jar into the local Gradle Repository files-2.1 instead of the Maven repository?

  1. Use this jar as a flat library Gradle dependency (assuming this jar is located in the libs directory:

    repositories { flatDir { dirs 'libs' } }

    dependencies { implementation name: 'lib-jar-name' }

Sign up to request clarification or add additional context in comments.

Comments

0

If DemoLib is a package,

you need to

import DemoLib.*

to import all its names into scope. Just import DemoLib wouldn't do anything. Or better,

import DemoLib.SomeClass

to import only one specific name. Actually, if you start typing SomeClass (assuming that's a name in your DemoLib package), IDEA should suggest adding the import.

Also, it's better to follow naming conventions from the beginning, and DemoLib is not a good package name:

Names of packages are always lower case and do not use underscores (org.example.project). Using multi-word names is generally discouraged, but if you do need to use multiple words, you can either simply concatenate them together or use camel case (org.example.myProject).

1 Comment

point taken about the naming convention, but even if I do import DemoLib.*, I get a compiler error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.