4

我正在尝试重新组织这个Android(基于 Java)库,以使用buildSrc文件夹来定义本文中描述的所有版本和依赖项。

我已经为 Kotlin 基础项目成功设置了几次。这次项目是纯Java

buildSrc文件夹中,我创建了以下buildSrc/src/main/java/org/ligi/snackengage/Dependencies.java文件:

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 { /* ... */ }

}

然后我参考项目根目录build.gradle中的定义等:

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

这是进行中的工作分支。当我构建项目时,会发生以下错误:

* 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.

4

2 回答 2

2

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
    }
}
于 2020-06-18T12:07:53.480 回答
0

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.

于 2020-06-02T20:16:17.753 回答