Require Gradle project from another directory

AndroidGradlebuild.gradleAndroid LibraryMulti Project

Android Problem Overview


I have a directory/project setup like this:

C:\
	_dev\
		Projects\
			Logger
			MyProject

Logger is an Android library project using Gradle. MyProject is a standard Android project project that needs to make use of the Logger library.

I am using Android Studio and have tried adding Logger to the external libraries. Whilst this works during development, I get messages about the class not being found when building.

I'm completely new to Gradle, but have tried the following in my build.gradle within MyProject:

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

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.1.0"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 18
    }

    dependencies {
        compile files("../Logger")
    }
}

dependencies {
	compile 'com.android.support:gridlayout-v7:18.0.0'
	compile 'com.android.support:appcompat-v7:18.0.0'
}

Android Solutions


Solution 1 - Android

The simplest way is to make MyProject a multi project with the Logger project as a subproject.

settings.gradle in MyProject directory:

include ":logger"
project(":logger").projectDir = file("../logger")

In the build.gradle of MyProject you can now reference this lib as a project:

dependencies {
     compile 'com.android.support:gridlayout-v7:18.0.0'
     compile 'com.android.support:appcompat-v7:18.0.0'
     compile project(":logger")
}

Solution 2 - Android

Android Studio 2.2.3:

Add to settings.gradle.

include ':app', ':new_lib'
project(':new_lib').projectDir = new File('../new_lib/app')
  • The path must be relative from the root of the project you're working on.
  • The module you're referencing must have a reference to it's "app" directory.

Then edit your Project Structure | Modules to setup dependencies.

Solution 3 - Android

Try adding the dependency to the global "dependencies" section, not the "android > dependencies". During development, the "android" configuration is used, but not to package the runtime.

dependencies {
    compile 'com.android.support:gridlayout-v7:18.0.0'
    compile 'com.android.support:appcompat-v7:18.0.0'
    compile files("../Logger")
}

It may also be worthwhile to look into setting up a multi-project gradle configuration, with a build.gradle and settings.gradle in the shared parent directory like here: http://www.gradle.org/docs/current/userguide/multi_project_builds.html

Solution 4 - Android

Here is a solution for settings.gradle.kts and build.gradle.kts (Kotlin DSL).

settings.gradle.kts:

include(":my-sub-project")

Top-level build.gradle.kts:

dependencies {
    implementation(project(":my-sub-project"))
    // ...
}

Directory structure:

🗁 project
  ├─── settings.gradle.kts
  ├─── build.gradle.kts
  ├─── 🗁 my-sub-project
  │   ├─── build.gradle.kts
  │   └─── ...
  └─── ...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEuan TView Question on Stackoverflow
Solution 1 - AndroidRene GroeschkeView Answer on Stackoverflow
Solution 2 - AndroidDave TylerView Answer on Stackoverflow
Solution 3 - AndroidmiwView Answer on Stackoverflow
Solution 4 - AndroidMahozadView Answer on Stackoverflow