Programmatically add dependencies to external project

First, all of this code is available on GitHub at https://github.com/pcclark4/GradleDependencyTest, which may make it easier to follow what I’m about to describe.

I have a unique problem in which a main module loads classes at runtime using reflection if they exist. Project1 contains two modules: Lolol, the main application module that uses reflection to load an instance of a class that implements TestInterface, and LibLib, the module which contains the TestInterface interface.

Using the settings.gradle file in Project2, I manually go up a directory then back down into the Project1 directory in order to define those projects. Therefore, I can open up the settings.gradle file and see Project2 as well as the two modules from Project1. The module Rofl in Project2 is a library module which depends on LibLib from Project1 so I can define a class that extends TestInterface.

If I were to open Project1, I can still build it even without Project2, because it truly doesn’t care if that class is there or not. It will just load it if its available. However, if I open Project2, I need the Lolol module to see Rofl as a dependency when it is built. I do NOT want to manually insert “compile project(’:Rofl’)” into Lolol’s build.gradle file every time I decide to add an extension. Instead, I would prefer if I could programmatically tell Lolol, using Rofl’s build.gradle file, that it needs to build Rofl as a dependency.

I have tried adding this inside of Rofl’s build.gradle:

project(':Lolol').dependencies {
    compile project(':Rofl')
}

No success, however.

I hope I’ve worded everything correctly. I greatly appreciate any suggestions! The directory structure is below, but like I said, it might be easier to just view my GitHub project.

├── Project1
│   ├── build.gradle
│   ├── LibLib
│   │   ├── build.gradle
│   │   └── src
│   │  
   └── main
│   │  
       ├── AndroidManifest.xml
│   │  
       └── java
│   │  
           └── com
│   │  
               └── eowdaoc
│   │  
                   └── liblib
│   │  
                       └── TestInterface.java
│   ├── Lolol
│   │   ├── build.gradle
│   │   └── src
│   │  
   └── main
│   │  
       ├── AndroidManifest.xml
│   │  
       └── java
│   │  
           └── com
│   │  
               └── eowdaoc
│   │  
                   └── lolol
│   │  
                       └── TestActivity.java
│   └── settings.gradle
├── Project2
│   ├── build.gradle
│   ├── Rofl
│   │   ├── build.gradle
│   │   └── src
│   │  
   └── main
│   │  
       ├── AndroidManifest.xml
│   │  
       └── java
│   │  
           └── com
│   │  
               └── eowdaoc
│   │  
                   └── rofl
│   │  
                       └── TestClass.java
│   └── settings.gradle

Can you please explain what do you expect to happen and what is actually happening?

I checked out your project, run ‘gradle :Lolol:dependencies’ inside of ‘Project2’ and I can see ‘Rofl’ as a compile dependency of ‘Lolol’

compile - Classpath for compiling the main sources.
+--- Project2:LibLib:unspecified
\--- Project2:Rofl:unspecified
     \--- Project2:LibLib:unspecified

I see the same thing when running

gradle :Lolol:dependencies

inside of Project2. However, if I run

gradle :Lolol:build

it does not actually compile it as a dependency. This can be confirmed by checking the build directory of Lolol and looking at the intermediate files.

It can be even further confirmed by adding

import com.eowdaoc.rofl.TestClass;

to the top of TestActivity. Running a build from the Project2 directory causes a “package does not exist” error.

What I expect to happen is for Lolol to recognize Rofl as a dependency and not fail when running from the Project2 directory.

I expect this code block inside of Rofl’s build.gradle file to actually affect compilation of Lolol, but it doesn’t.

project(':Lolol').dependencies {
    compile project(':Rofl')
}

Is it possible that this is an Android plugin problem and not a Gradle problem? Or should I be able to do this regardless of what plugins I’m using?