Separate an Android project in several other dependents libraries

时间:2015-08-07 01:35:54

标签: android android-studio intellij-idea gradle android-gradle

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?

1 个答案:

答案 0 :(得分:1)

虽然我还没有找到完美的解决方案。到目前为止,我发现的最佳解决方案是基于@hgoebl发现的blog post并在评论中发布:

在这里描述我做了什么:

我对项目lib-android-base进行了更改:为了测试另一个不同的配置,我更改了模块名称' lib'到' lib_base'并离开了,但没有影响力。

我将IDE从Intellij 14.1.4更改为Android Studio 1.3.1:我认为android studio更好地应对Gradle中的导入。

在settings.gradle文件中的lib-android-network上,我添加了:

//":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/')

在lib-android-network / lib build.gradle文件中,我添加了:

compile project (':base')

在我的App Project settings.gradle文件中,我添加了:

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

在App Project模块build.gradle文件中我刚添加:

compile project (':network')

我找不到解决方案的唯一问题是我必须引用' android-lib-base'在App项目中,因为我不直接使用它。但对我来说现在还可以。