Remel Pugh
Remel Pugh

Reputation: 36

Add library project as dependency to another library project

I have been trying to add a library project as a dependency to another library project in Android Studio without success. Below is my project structure:

- apps
  - demo (depends on android utilities & android ui)
- libraries
  - android utilities
    - utilities
  - android ui
    - ui (depends on android utilities)

I used the following as guide Add local Library Project as a dependency to multiple Projects in Android Studio

android utilities/build.gradle

...

android utilities/utilities/build.gradle

apply plugin 'com.android.library'

...

android ui/settings.gradle

include ':ui'
include ':utilities'
project(':utilities').projectDir = new File(settingsDir, '../android utilities/utilities')

android ui/build.gradle

...

android ui/ui/build.gradle

apply plugin 'com.android.library'

...

dependencies {
    compile project(':utilities')
}

I have both Android Utilities and Android Ui setup as separate projects, I am able to compile the Android Utilities project without issues. But, I am not able to compile the separate Android Ui project. Android Studio itself doesn't indicate any errors, but gradle produces multiple errors about packages from the utilities library not existing.

Upvotes: 2

Views: 1184

Answers (2)

Armaxis
Armaxis

Reputation: 161

I had similar problem in my project but managed to resolve it. Do you have more than 1 build flavor?

What I ended up doing is adding following in library (/ui/ui/build.gradle in your case), where buildFlavor1 and buildFlavor2 - are build variants used in your app. I think the problem was because Gradle is not aware of possible configurations when resolving dependencies, so he cannot properly pick the library for every build flavor. Hope this helps!

configurations {
    buildFlavor1DebugCompile
    buildFlavor2DebugCompile
    buildFlavor1ReleaseCompile
    buildFlavor2ReleaseCompile
}

dependencies {
    ...

    buildFlavor1DebugCompile project(path: ':utilities', configuration: 'buildFlavor1Debug')
    buildFlavor2DebugCompile project(path: ':utilities', configuration: 'buildFlavor2Debug')
    buildFlavor1ReleaseCompile project(path: ':utilities', configuration: 'buildFlavor1Release')
    buildFlavor2ReleaseCompile project(path: ':utilities', configuration: 'buildFlavor2Release')
}

Upvotes: 1

Jim
Jim

Reputation: 10288

You do not have the right syntax. This:

include 'ui'

should be this:

include ':ui'

(you missed the colon)

Upvotes: 0

Related Questions