Search code examples
javaandroidgradlebuildsrc

buildSrc: Could not get unknown property for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler


I am trying to reorganize this Android (Java based) library to use the buildSrc folder to define all versions and dependencies as described in this article.

I already set this successfully up for several times for Kotlin bases projects. This time the project is pure Java.

In the buildSrc folder I created the following buildSrc/src/main/java/org/ligi/snackengage/Dependencies.java file:

package org.ligi.snackengage;

public class Dependencies {

    public static class Android { /* ... */ }

    public static class GradlePlugins {
        public static final String ANDROID = "com.android.tools.build:gradle:3.6.3";
        // ...
    }

    public static class Libs { /* ... */ }

}

Then I refer to the definitions in the project root build.gradle among others:

import org.ligi.snackengage.Dependencies.GradlePlugins

apply plugin: "com.github.ben-manes.versions"

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath GradlePlugins.ANDROID
        classpath GradlePlugins.MAVEN
        classpath GradlePlugins.VERSIONS
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

Here is the work in progress branch. When I build the project then the following error occurs:

* Where:
Build file 'SnackEngage/build.gradle' line: 12

* What went wrong:
A problem occurred evaluating root project 'SnackEngage'.
> Could not get unknown property 'GradlePlugins' for object of type
  org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

Here is the build log.


Solution

  • You have defined GradlePlugins class as an inner static class of Dependencies, so you need to use Dependencies.GradlePlugins to access it from your build script.

    Change your dependencies block as follows:

    import org.ligi.snackengage.Dependencies // do not import org.ligi.snackengage.Dependencies.GradlePlugins
    buildscript {
        // ...
        dependencies {
            classpath Dependencies.GradlePlugins.ANDROID
            classpath Dependencies.GradlePlugins.MAVEN
            classpath Dependencies.GradlePlugins.VERSIONS
        }
    }
    
    

    EDIT you could also use a static import, as follows:

    import static org.ligi.snackengage.Dependencies.*
    buildscript {
        // ...
        dependencies {
            classpath GradlePlugins.ANDROID
            classpath GradlePlugins.MAVEN
            classpath GradlePlugins.VERSIONS
        }
    }