how do I import java class in build.gradle file

Tags: , , ,



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.

Answer

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.



Source: stackoverflow