89

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'
}
Mahozad
  • 18,032
  • 13
  • 118
  • 133
Euan T
  • 2,041
  • 3
  • 20
  • 28
  • 3
    Like this? http://stackoverflow.com/questions/17479076/android-studio-add-external-project-to-build-gradle – Lesleh Oct 10 '13 at 15:02
  • Already tried that unfortunately. include ':logger' project(':logger').projectDir = new File(settingsDir, '../SysLog') – Euan T Oct 10 '13 at 15:22
  • write this line ```includeFlat 'prjname'``` into MyProject\settings.gradle . if project are sideByside / Flat / same level , gradle auto accepts that. also it project automatically pops up in intellij idea details: https://stackoverflow.com/questions/6995390/gradle-how-to-configure-multiproject-setup-with-side-by- – bh_earth0 Sep 08 '18 at 08:57
  • This question is too frequently visited by me, almost every time I make a new gradle project, so I put it in favorites. – prem30488 Oct 17 '18 at 11:52

5 Answers5

202

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")
}
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
Rene Groeschke
  • 27,999
  • 10
  • 69
  • 78
  • Thanks, this is exactly what I needed. The project compiled successfully as soon as I added this. – Euan T Oct 11 '13 at 09:43
  • Can you use a property value from the Gradle home directory's gradle.properties file? – Rick Nov 15 '16 at 17:32
  • This is exactly what I just inferred I might be able to do, and so is exactly the answer I was looking for :-D –  Jul 27 '17 at 18:05
  • I got same situation and this solution in settings.gradle worked for me – rinilnath May 04 '18 at 06:39
  • I get an error that the project already exists, when I give it a new name it clones my repository into my workspace. – JesseBoyd Jul 17 '18 at 23:16
  • none of this hyper-over-extended build stuff makes any sense anymore. gradle is getting almost to be like autotools. Bless you for providing a simple answer and solution, and for making things work without the rest of us who just want to write software having to understand it. – stu Oct 12 '19 at 01:20
  • 1
    Thanks, worked well, I read over here https://docs.gradle.org/current/dsl/org.gradle.api.initialization.Settings.html#org.gradle.api.initialization.Settings:include(java.lang.String[]) for deeper understanding as well – Ankit Jain Oct 28 '19 at 10:12
  • I know this is ages old, but is there any way to do this iteratively for a long list of subprojects rather than statically defining them all like this? – topher217 Aug 17 '20 at 09:30
  • `file('')` didn't work, but `new File('')` worked, I'm on Windows – user924 Feb 02 '21 at 13:05
  • 3
    What if that external dependencies has its own project depencies? Those paths are not resolved. – SMUsamaShah May 21 '21 at 15:46
15

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.

Dave Tyler
  • 595
  • 5
  • 7
  • I tried this in Android Studio but now my root project is no longer considered a library and the build variants only show what you called ":new_lib" as the available module. The original 'app' is no longer in the module list. – ZeroStatic Jan 19 '18 at 14:05
10

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

miw
  • 776
  • 4
  • 11
2

For those who meet the error Could not find method compile()... based on the answer from @Rene Groeschke, @Michael Mrozek and @user924, in the build.gradle of MyProject use

implementation project(":logger")

instead of

compile project(":logger")

It works with Gradle-7.4

Yi Wei
  • 41
  • 4
-1

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
  │   └─── ...
  └─── ...
Mahozad
  • 18,032
  • 13
  • 118
  • 133