Home » Android » Can Android Libraries in Gradle have other Android Libraries as dependencies?

Can Android Libraries in Gradle have other Android Libraries as dependencies?

Posted by: admin May 14, 2020 Leave a comment

Questions:

Is this structure allowed?

MyProject/
 | settings.gradle
 + app/
    | build.gradle
 + libraries/
    + lib1/
       | settings.gradle
       | build.gradle
       + libraries/
          + sublib1/
             | build.gradle
          + sublib2/
             | build.gradle
    + lib2/
       | build.gradle

I had this working with Ant but with Gradle my project can’t find sublib1 or sublib2.

When I run gradle I get this error:

* What went wrong:
A problem occurred evaluating project ':libraries:lib1'.
> Project with path ':libraries:sublib1' could not be found in project ':libraries:lib1'.

This is settings.gradle from MyProject:

include ':libraries:lib1'
include ':libraries:lib2'

This is build.gradle from MyProject:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':libraries:lib1')
    compile project(':libraries:lib2')
}

android {
    compileSdkVersion 15
    buildToolsVersion "17.0.0"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        instrumentTest.setRoot('tests')
    }
}

This is settings.gradle from lib1:

include ':libraries:sublib1'
include ':libraries:sublib2'

This is build.gradle from lib1:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        compile fileTree(dir: 'libs', include: '*.jar')
        compile project(':libraries:sublib1')
        compile project(':libraries:sublib2')
    }
}

apply plugin: 'android-library'

android {
    compileSdkVersion 15
}
How to&Answers:

Gradle does not seem to support several settings.gradle files. This file should be present only in the project root and should include all the subprojects :

include ':libraries:lib1', ':libraries:lib1:libraries:sublib1', ':libraries:lib1:libraries:sublib2', ':libraries:lib2'

Source : http://www.gradle.org/docs/current/userguide/multi_project_builds.html

Answer:

You can have other libraries as dependencies in a library project.
In your settings.gradle just include ALL the applications/libraries modules.

And in the library project that depends on another library module you just add it as a dependency just like any other module in the project.

In one of my library modules the dependency in gradle looks like this:

dependencies {
    compile project(':library1')
    compile project(':library2')
    compile project(':library3')
}