Dan
Dan

Reputation: 13383

Kotlin + Gradle Unresolved Reference

As per this question I'm trying to setup the following project structure

project/
   settings.gradle
   projectB/  # some common code (using spring-boot)
       build.gradle
       com/
          foo/
             bar/...
   projectA/  # an android app
       build.gradle
       com/
          foo/
             baz/...

settings.gradle looks like

rootProject.name = "project"
include ":projectB"
project(":projectB").projectDir = new File(rootDir, './projectB')
include ":projectA"
project(":projectA").projectDir = new File(rootDir, './projectA')

and in projectA/build.gradle I have

dependencies {
    implementation project(":projectB")
}

Android Stuido seems happy and will provide code completion and searching for code in projectB within projectA. However compilation fails with an error

Unsresolved reference: bar

on the line where I try to import com.foo.bar.whatever.

I have tried a number of different changes to the various gradle files but nothing has fixed this error.

What is the problem with this setup and how can it be resolved?

Thanks

Upvotes: 20

Views: 5060

Answers (1)

Martin Zeitler
Martin Zeitler

Reputation: 76569

in Android Studio & IntelliJ IDE, it is better to define these as "modules".

eg. with a settings.gradle alike:

include ":projectA", ":projectB"
rootProject.name = "project"

with a directory structure alike:

project/
   settings.gradle
   projectB/  # common code
       build.gradle
       src/
          test/
          debug/
          main/
             res/
             assets/
             java/
             kotlin/
                com/
                   foo/
                      bar/...


   projectA/  # android app
       build.gradle
       src/
          test/
          debug/
          main/
             res/
             assets/
             java/
             kotlin/
                com/
                   foo/
                      baz/...

when intending to use other directory-structure, one has the change the sourceSets configuration.

Upvotes: 2

Related Questions