2

I want to read few properties from a static java class file as below

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

This would be in MyClass.java file under

src/main/java/myProject/

I need to read PROPERTY_A, and PROPERTY_B in my app build.gradle inside android{} block.

Note that these need to be read from java file for a specific reason, and are not general properties. Hence, I am not reading them from gradle.properties files.

I went through How do I call a static Java method from Gradle

but the article wasn't super clear.

I also tried gradle docs, but couldn't find a super clear answer.

I am bit new to groovy, so any suggestions here would be super helpful.

Thanks!

Akshay
  • 806
  • 11
  • 26
  • curious.. why do this? Why not just have a property file and then pull those values from there into build.gradle? I think information is supposed to flow from the build config into the application, not the other way around. – spentag Feb 08 '19 at 03:03
  • There is a restriction to use the properties from this java file – Akshay Feb 08 '19 at 03:19
  • That does not change the technological constraints. – spentag Feb 08 '19 at 03:31
  • You can try using `BuildConfigField` but this will be the opposite way for you . FYI I have the [same question](https://stackoverflow.com/questions/50874250/use-a-java-class-in-build-gradle) a while ago but did not get any answer . Looks like its not technological possible . – ADM Feb 08 '19 at 03:42
  • Thanjs @ADM I will try my luck for couple of hours before returning to use custom properties file – Akshay Feb 08 '19 at 04:01
  • in what part if the lifecycle do you need this: https://docs.gradle.org/4.10/userguide/build_lifecycle.html – Ray Tayek Feb 08 '19 at 07:44

2 Answers2

1

Find out when in the lifecycle that the code in the android {} gets executed. Worst case this will be early, so you may have to compile that class before anything else. Then write some groovy code in your gradle script to get the properties.

Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
0

So, it sounds like It's not possible in the initialization part of gradle build to read static java classes from the answers so far. I decided to read it via .properties file eventually.

Akshay
  • 806
  • 11
  • 26