Separate your Android project in a few other dependents

I have a project called "app-project" (abbreviated as AP here), which has an "application" module. To make things reusable, this project depends on another project library named "lib-android-network" ( LAN ) and its lib module. It depends on another project called "lib-android-base" (LAB) and its module "lib". All of these projects remain in the same directory in the hierarchy. I am using Gradle and Intellij on Windows 8.1.

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

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

To make an AP , see LAN for my settings. gradle AP :

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

and in the build.gradle of the "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 many attempts before reaching this configuration), but it works fine so far when I need a third LAB project.

As I said, my LAN is dependent on LAB . So I entered the LAN settings.gradle file:

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

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

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

This is the same idea that works for the APLAN dependency, but when I run Gradle on my first project, I get this error:

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

I think because dependent projects should remain inside the parent project as modules, but this is not interesting for me, because I do not want to duplicate the project one inside the other so that it works. I want to keep the same level of the directory hierarchy. I also do not want to add Intellij module dependencies (unless it works), so before I finalize the first configuration, I have a lot of problems.

EDIT

Based on this answer and this sample project I am changing something 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') 

Application / 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') 

Library / build.gradle:

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

But I still get the same error:

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

--- EDIT 2 ---

it works if I repeat the gradle configuration of the network project in the application 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 think I would not have to repeat this configuration in my base application whenever I had to add a dependency on the lib-android-network project. This will get confused ...

Is there a “Best Practice” for working with multiple library projects? What is the best way to handle this? Is it adding each lib as a helper module for the top project?

+4
source share
1 answer

Although I did not find the perfect solution. So far, the best solution I've found has been based on this blog post found by @hgoebl and posted in the comments:

To describe here what I did:

I am making changes to the lib-android-base project: to check for a different configuration, I changed the module name 'lib' to 'lib_base' and left like that, but there was no effect.

I am changing the IDE from Intellij 14.1.4 to Android Studio 1.3.1: I thought that Android Studio did better with import made in Gradle.

In the 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/') 

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

 compile project (':base') 

In the settings of my Project.gradle project, 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 the project build file of the Project Project build.gradle I added:

 compile project (':network') 

The only problem I could not find a solution to was the fact that I had to refer to the "android-lib-base" in the application project, since I did not use it directly. But for me this is normal.

+1
source

All Articles