4

I have a project named 'app-project' (abbreviate here as AP) that have a module "app". To make things reusable, this project depends on another project library named 'lib-android-network' (LAN) and his module lib. It depends on another project named 'lib-android-base' (LAB) and his module "lib". All these projects stay in the same directory in the hierarchy. I use Gradle and Intellij on Windows 8.1.

ex: directories: root/app-project root/lib-android-network root/lib-android-base

I start with project AP depends on project LAN, without project LAB.

to make AP see LAN my settings.gradle of AP was:

include ':app', "../lib-android-network", "../lib-android-network:lib"

and in build.gradle of 'app' module I have:

compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':../lib-android-network:lib')

I don't know how (I have to make a lot of attempts until reach this configuration) but it works perfectly until now, when need the third, LAB, project.

As I say now I have LAN depends on LAB. So I put in LAN settings.gradle file:

include ':lib', '../lib-android-base', '../lib-android-base:lib'

And in build.gradle of LAN 'lib' module:

compile project(':../lib-android-base:lib')

It's the same idea that works for AP - > LAN dependency, but when I run Gradle against my first project I get this error:

Error:(37, 0) Project with path ':../lib-android-base:lib' could not be found in project ':../lib-android-network:lib'.

I think it's because the dependent projects should stay inside the parent project as modules, but Its not interesting for me, because I don't want to have duplicate the project one inside another to make it work. I want to leave same directory hierarchy level. I don't want to add Intellij modules dependencies too (unless sure work), cause before I reach the first worked configuration, I have a lot of problems doing this.

EDIT

Based on this answer and this example project I change somethings in my own project to make it more correct:

AP project:

settings.gradle:

include ':lib-android-network', ':lib-android-network:lib'
project(':lib-android-network').projectDir = new File(settingsDir, '../lib-android-network')
project(':lib-android-network:lib').projectDir = new File(settingsDir, '../lib-android-network/lib')

app/build.gradle:

compile project(':lib-android-network:lib')

LAN project:

settings.gradle:

include ':lib-android-base', ':lib-android-base:lib'
project(':lib-android-base').projectDir = new File (settingsDir, '../lib-android-base')
project(':lib-android-base:lib').projectDir = new File (settingsDir, '../lib-android-base/lib')

lib/build.gradle:

compile project(":lib-android-base:lib")

But I'm still getting the same error:

Error:(39, 0) Project with path ':lib-android-base:lib' could not be found in project ':lib-android-network:lib'.

--- EDIT 2 ---

it works if I repeat the settings.gradle configuration of network project into app project, but

include ':lib-android-base', ':lib-android-base:lib', ':lib-android-network', ':lib-android-network:lib'
project(':lib-android-base').projectDir = new File(settingsDir, '../lib- android-base')
project(':lib-android-base:lib').projectDir = new File(settingsDir, '../lib-android-base/lib')

But I guess I shouldn't have to repeat this configuration in my base application whenever I had to add a dependency to lib-android-network project. It will get confused...

Is there a "Best Practice" to deal with a several library projects? Which is the best way to deal with it? Is it adding each lib as sub module for the top project?

Community
  • 1
  • 1
alexpfx
  • 6,412
  • 12
  • 52
  • 88
  • I have almost the same requirements in my Android apps migrating from ant/IntelliJ "click-build" to Gradle. Unfortunately I'm currently a beginner with Gradle. But I'm wondering why you have ":lib-android-base" and ":lib-android-base:lib". Do you think you need this? One of them not sufficient? Another thought (haven't tried it yet): Would it be helpful to work with sym-links (Linux, MacOS) to "include" library projects into the app project even when they physically live as siblings in the directory? – hgoebl Aug 09 '15 at 19:09
  • I don't know exactly why I need the two statements, but it only works this way. I did several 'trial and error'. I think sym-links can solve problem, I was thought about it, but as I use windows I couldn't test. As I cannot do what I want yet, for now I put the lib project inside the main project as modules. Meanwhile I'm trying to better understand how Gradle build system works, since everything I did so far was blindly. – alexpfx Aug 09 '15 at 23:28
  • 1
    OK. I'll do the same as you. Maybe one of us comes up with a clear and shiny solution. It's a pity that there is no nice blueprint for this. I've found an [interesting post](http://www.philosophicalhacker.com/2014/10/02/an-alternative-multiproject-setup-for-android-studio/) but it doesn't handle libraries depending on other libraries. – hgoebl Aug 10 '15 at 08:21
  • @hgoebl Today I got time to test the solution you found. It almost solves the problem and for me it's good enough. I make an answer explained what I did. – alexpfx Aug 13 '15 at 00:51

1 Answers1

1

Although I haven't found a perfect solution. So far, the best solution I found was based on this blog post found by @hgoebl and posted in comments:

To describe here what I did:

I make a change on project lib-android-base: To test another different configuration I changed the module name 'lib' to 'lib_base' and left so, but there was no influence.

I change the IDE from Intellij 14.1.4 to Android Studio 1.3.1: I thought the android studio coped better with the imports made in Gradle.

On lib-android-network in the settings.gradle file I added:

//":base" here can be any word you use to name lib-android-base project in lib-android-network context.

include ':base'
project (':base').projectDir = new File ('../lib-android-base/lib_base/')

On lib-android-network/lib build.gradle file I added:

compile project (':base')

On my App Project settings.gradle file I added:

include ':base'
include ':network'
project(':base').projectDir = new File ('../lib-android-base/lib_base/')
project(':network').projectDir = new File('../lib-android-network/lib/')

and in App Project module build.gradle file I just added:

compile project (':network')

The only problem I not found a solution was the fact I had to reference the 'android-lib-base' in the App project, since I don't use it directly. But for me it's ok for now.

alexpfx
  • 6,412
  • 12
  • 52
  • 88
  • Sounds like a good solution. BTW I switched from IntelliJ 14.1.4 to Android Studio 1.3.1 as well. IntelliJ (by far my most loved piece of software) seems to be behind Android Studio in some points. This is very sad, but hopefully just a matter of time. Just a feeling, but changing `buildscript { dependencies { classpath ... } }` to a newer version could have helped, in my case it was 'com.android.tools.build:gradle:1.2.3'. – hgoebl Aug 13 '15 at 06:03
  • The link is dead. –  Feb 07 '17 at 17:55