19

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

Dan
  • 12,857
  • 7
  • 40
  • 57
  • In `settings.gradle` just use `include ":projectA", ":projectB" ` and check. I have a multi module project configured in same way, I don't face any issue. – blizzard May 13 '18 at 10:59
  • Unfortunately that's one of the variations I tried already – Dan May 13 '18 at 11:00
  • :( Try Invalidating cache and restart Android studio. – blizzard May 13 '18 at 11:03
  • Alas, I've also tried that several times. It builds neither in Android Studio, nor by calling `./gradelw clean build` from the command line. – Dan May 13 '18 at 11:07
  • 1
    `Open Module Settings` check if your modules and their dependencies are listed properly, check If a module has any cyclic dependencies and remove if any exists. – blizzard May 13 '18 at 11:09
  • I can't see anything untoward there. However, the available tabs are different for the two modules, as are the icons. `projectA` has a green circle next to its folder and several tabs, whereas `projectB` has a blue square/cup (in the tree view & settings respectively) and only the dependencies tab. – Dan May 13 '18 at 11:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170947/discussion-between-blizzard-and-dan). – blizzard May 13 '18 at 11:23
  • Have you tried using `api` instead of `implementation`? – P Fuster May 16 '18 at 09:46
  • 4
    Your folder structure is not really `com/something/something`, right? It should be `src/main/java/com/something/something` unless you specifically altered the source directories – Nexcius May 16 '18 at 20:14
  • I believe I'm seeing the same issue. Android module `app` depends on android module `core` and things are fine in Java-land. However when a Kotlin unit test in `app` imports a Kotlin class from `core` I get the same misbehavior as Dan. AS looks happy but `Unresolved Reference` when building (ide or command line). – tennessee sombrero May 17 '18 at 03:04
  • @Nexcius Sorry, the directory structure is as it should be, I just transcribed it incorrectly. – Dan May 19 '18 at 07:13
  • @PFuster I tried that already. Unfortunately it doesn't help – Dan May 19 '18 at 07:13

1 Answers1

2

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.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216