英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论