Home > front end >  gradle cannot resolve dependecy project(":app") from other sub-project
gradle cannot resolve dependecy project(":app") from other sub-project

Time:06-30

I'm new to gradle and still trying to understand it, so please assume I have no idea what I'm talking about if you give an answer. :) I'm using gradle 7.3.3.

I've got an Android app project that has the standard app module. In my app module is a class named com.inadaydevelopment.herdboss.DatabaseConfigUtil and I want to be able to run DatabaseConfigUtil.main() and it needs to have all of the classes from app in the classpath.

I've created a second module named libdbconfig which is just a Java Library module so that I can create a JavaExec task which will call DatabaseConfigUtil.main() and make sure that all of the classes from app are in the classpath.

My libdbconfig/build.gradle file looks like this:

plugins {
    id 'java'
}

dependencies {
    implementation project(":app")
}

task dbconfig(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath
    mainClass = "com.inadaydevelopment.herdboss.DatabaseConfigUtil"
}

I sync AndroidStudio with my build.gradle changes and then try to run the libdbconfig:dbconfig task and get the error:

* What went wrong:
Could not determine the dependencies of task ':libdbconfig:dbconfig'.

> Could not resolve all task dependencies for configuration ':libdbconfig:runtimeClasspath'.
   > Could not resolve project :app.

I thought I understand how to declare a dependency on another sub-project and whenever I look at examples (Example 11. Declaring project dependencies it looks like I'm doing it right.

If I change my dependencies to remove the word "implementation" then the gradle config doesn't throw an error, but I don't understand that at all since it doesn't attach the dependency to a configuration (like "implementation").

dependencies {
    project(":app")
}

When I do that, the gradle task will start, but will ultimately fail because the classes from the app module are not in the classpath and so it can't find the class to run:

> Task :libdbconfig:dbconfig FAILED
Error: Could not find or load main class com.inadaydevelopment.herdboss.DatabaseConfigUtil
Caused by: java.lang.ClassNotFoundException: com.inadaydevelopment.herdboss.DatabaseConfigUtil

Any help is appreciated. gradle has been voodoo to me for a long time and I'm trying to figure it out. I went through a udacity course on how to use it and I thought I had a much better understanding of it, but some of the basic things I thought I understood aren't working.

CodePudding user response:

Module :app should configure itself; better use buildSrc or includeBuild. It's an anti-pattern to access anything else from a library module than another library. If you really have to configure the database in a strange way, apply a custom plugin to the :app module. And I just don't understand why this question is tagged as android, it reads id 'java' and not id 'com.android.library'?

For example, add buildSrc/build.gradle:

buildscript {
    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.1'
    }
    repositories {
        mavenCentral()
        google()
    }
}

apply plugin: 'org.gradle.java-gradle-plugin'

allprojects {
    repositories {
        mavenCentral()
        google()
    }
}

gradlePlugin {
    plugins {
        DbConfigPlugin {
            id = 'com.inadaydevelopment.herdboss.dbconfig'
            implementationClass = 'com.inadaydevelopment.herdboss.DbConfigPlugin'
        }
    }
}

dependencies {
    implementation gradleApi()
    implementation 'com.android.tools.build:gradle-api:7.2.1'

    testImplementation gradleTestKit()
    testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2'
}

tasks.withType(Test).configureEach {
    useJUnitPlatform()
}

Then Gradle can execute Java code at build time:

class DbConfigPlugin implements Plugin<Project> {

    @Override
    public void apply(@NotNull Project project) {
        /* TODO: configure the database. */
    }
}

And it can be applied in module :app build.gradle:

plugins {
    id 'com.android.application'
    id 'com.inadaydevelopment.herdboss.dbconfig'
}

Else you'd have to stick to Groovy or Kotlin scripting.

CodePudding user response:

If you haven't already done so, you need to include your subprojects in your settings.gradle, as follows:

include "app"
include "libdbconfig"

The example you referenced is for Gradle 7.4.2. Maybe take a look at this example for 7.3.3.

  • Related