JJD
JJD

Reputation: 51804

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.

Upvotes: 4

Views: 15136

Answers (2)

M.Ricciuti
M.Ricciuti

Reputation: 12086

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
    }
}

Upvotes: 2

Martin Zeitler
Martin Zeitler

Reputation: 76569

You need to define variable GradlePlugins with def (in Gradle) or public class GradlePlugins (in Java), before attempting to access it. Kotlin class GradlePlugins should also work.

dependencies {
    classpath GradlePlugins.ANDROID
    classpath GradlePlugins.MAVEN
    classpath GradlePlugins.VERSIONS
}

And I think the buildSrc directory belongs into the module directory, as the Gradle manual shows.

Upvotes: 0

Related Questions