Gradle project: creating a dependency on an imported module

Answered

I have a kotlin gradle project (mainproj) that depends on another kotlin gradle project (projlib).

I've imported projlib as a module into mainproj.

Class files in mainproj can see the functions in projlib (no syntax errors, import statements pointing to projlib ok).

But when I try to build I get  errors pointing to the projlib import statements:

unresolved reference: projlib

Done a lot of Googling on this issue and found nothing that works.

Do I need to create a composite build with both mainproj and projlib. as subprojects within it?

Any direct help, or pointing me to a valid tutorial is appreciated.

 

 

 

 

 

0
5 comments

Hello!

How the dependency is declared on the Gradle level and how do you import the other project as a Module (via IDEA IU or by changing the build script)?

Also, how do you trigger build (Build Menu, Gradle Run Configuration / Tool Window)?

0

Initially I tried something simple, using just Idea options. Also simple project structure: just adding a module to my existing project.

Steps I took from mainproj:

  • opened ‘Project Structure’
  • selected ‘Modules’
  • clicked ‘+’ to add the projlib module, and added it
  • under ‘Dependencies’ tab, added projlib

Then, within mainproj editor, in the source code, I found refs to projlib funcs (which were red), hovered over one, and got the Idea popup message: “Add dependency on module ‘projlib’”

I clicked it, waited 10 secs, and all references were resolved, with no syntax errors.

But, as I said above, selecting build (from mainproj gradle window) fails with the ‘unresolved reference’ error.

I tried adding a dependency to projlib within the mainproj build.gradle file like this:

dependencies {
	implementation(project(":projlib"))
}

But get an error saying that projlib is not within the mainproj root folder. Not sure how to access projects OUTSIDE the root?

 

 

0

Thank you for sharing the details!

Adding the Modules via Project Structure is intended for the Projects that utilize IDEA's Native Build System, for Gradle-based Projects Modules must be added via Gradle Build Scripts (settings.gradle(.kts), build.gradle(.kts)).

Specifically, it should be added as follows:

settings.gradle.kts:

include("myExternalProject")
findProject(":myExternalProject")?.projectDir = File("..\\..\\GradleProjects\\myExternalProject")

where “..” tells Gradle to go one folder level up from your Project's folder.

build.gradle.kts:

dependencies {
    implementation(project(":myExternalProject"))
}
1

Thank you 🙏🏼 , that works.

Had to translate to groovy as I'm not using .kts build file.

findProject(":projlib")?.projectDir = new File("../projlib")

Also had to 1st REMOVE ‘projlib’ as idea module, or I got errors.

After running build script, projlib showed up in the project structure UNDERNEATH my main project.

 

0

While building is working fine, I had some issues with deployment; but, I figured out how to place the projlib classes into my jar, which I'll share below.

The project I'm working on builds classes that plug in to another application.

This was my deployment task before integrating the projlib module (note that I'm obfuscating actual project and class names, but the structure is the same):

def buildDir = "src/main/kotlin"
def srcDirName = "mainproj"
def destDir = "~/mylibs"

tasks.register('deployAsJar', Jar) {
    dependsOn(build)
    from(buildDir) {
        include "**/*" into srcDirName
    }
    archiveFileName = "mainproj${version}.jar"
    destinationDirectory = file(destDir)
}

And, after adding projlib project:

def buildDir = "src/main/kotlin"
def projLibBuildDir = "../projlib/main/"
def srcDirName = "mainproj"
def destDir = "~/mylibs"

tasks.register('deployAsJar', Jar) {
    dependsOn(build)
    from(buildDir) {
        include "**/*" into srcDirName
    }
    from(projLibBuildDir) {
        include "**/*" into ""
    }
    archiveFileName = "mainproj${version}.jar"
    destinationDirectory = file(destDir)
}

 

The important bit to note here is that the projlib class structure has to be in the ROOT of the jar (denoted by “”) in order for mainproj classes to access it--not in the ‘srcDirName’ directory.

All is now working properly 😎

 

 

0

Please sign in to leave a comment.