Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 438
Dan Avatar asked Apr 15 '18 18:04

Dan


1 Answers

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.

like image 81
Martin Zeitler Avatar answered Sep 30 '22 18:09

Martin Zeitler