如何在 build.gradle 文件中导入 Java 类。

huangapple go评论88阅读模式
英文:

how do I import java class in build.gradle file

问题

我想在 build.gradle 文件中从以下类中导入静态数据:

 public class MyClass{
     public static final String PROPERTY_A = "myStringA";
     public static final String PROPERTY_B = "myStringB";
  }

我看到一个项目中他们使用了类似的方式:

import com.example.MyClass

但我无法弄清楚应该提供哪些配置以便在 build.gradle 中能够看到这个 Java 类。

非常感谢您的帮助。

英文:

I want to import static data in build.gradle file from below class:

 public class MyClass{
     public static final String PROPERTY_A = "myStringA";
     public static final String PROPERTY_B = "myStringB";
  }

I saw a project where they used, something like:

import com.example.MyClass

but I couldn't figure out what configurations are to be provided in order to have the visibility of java class from build.gradle.

Any help would be highly appreciated.

答案1

得分: 0

你需要使用 buildSrc:https://docs.gradle.org/current/userguide/organizing_gradle_projects.html#sec:build_sources

所以根据你的示例,你的目录结构会类似这样:

.
├── build.gradle
├── buildSrc
│   ├── build.gradle
│   └── src
│       ├── main
│       │   └── java
│       │       └── com
│       │           └── example
│       │               └── MyClass.java
│       └── test
│           └── java
│               └── com
│                   └── example
│                       └── MyClassTest.java
└── settings.gradle

然后你只需像之前一样导入即可。

英文:

You'll want to use buildSrc: https://docs.gradle.org/current/userguide/organizing_gradle_projects.html#sec:build_sources

So for your example, your directory structured would look something like:

.
├── build.gradle
├── buildSrc
│   ├── build.gradle
│   └── src
│       ├── main
│       │   └── java
│       │       └── com
│       │           └── example
│       │               └── MyClass.java
│       └── test
│           └── java
│               └── com
│                   └── example
│                       └── MyClassTest.java
└── settings.gradle

Then you would just import as you have above.

huangapple
  • 本文由 发表于 2020年8月20日 19:16:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63503861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定