Require Gradle project from another directory
Asked Answered
L

5

91

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'
}
Lingua answered 10/10, 2013 at 14:58 Comment(4)
Like this? #17479576Enounce
Already tried that unfortunately. include ':logger' project(':logger').projectDir = new File(settingsDir, '../SysLog')Lingua
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: #6995890Cimbri
This question is too frequently visited by me, almost every time I make a new gradle project, so I put it in favorites.Rockies
F
207

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")
}
Faceharden answered 10/10, 2013 at 18:33 Comment(10)
Thanks, this is exactly what I needed. The project compiled successfully as soon as I added this.Lingua
Can you use a property value from the Gradle home directory's gradle.properties file?Frowsy
This is exactly what I just inferred I might be able to do, and so is exactly the answer I was looking for :-DNochur
I got same situation and this solution in settings.gradle worked for meTedman
I get an error that the project already exists, when I give it a new name it clones my repository into my workspace.Largo
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.Whisper
Thanks, worked well, I read over here docs.gradle.org/current/dsl/… for deeper understanding as wellMattiematting
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?Damara
file('') didn't work, but new File('') worked, I'm on WindowsEffy
What if that external dependencies has its own project depencies? Those paths are not resolved.Sungsungari
I
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.

Ipoh answered 15/12, 2016 at 9:50 Comment(1)
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.Nonchalance
F
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

Fumatorium answered 10/10, 2013 at 15:10 Comment(1)
Just tried that, with no success unfortunately. Still getting the same errors.Lingua
F
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

Flintshire answered 18/4, 2023 at 20:51 Comment(0)
G
-2

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
  │   └─── ...
  └─── ...
Gibbet answered 18/2, 2022 at 11:20 Comment(1)
Technically correct (like Microsoft), but not what was asked for ;-)Hyperactive

© 2022 - 2024 — McMap. All rights reserved.