Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass System.property to Android App from Gradle?

I want to be able to get system property from the application as:

String isTesting = System.getProperty("IS_TESTING");

I have tried to pass the argument using -D flag in gradle, like ./gradlew assemble -DIS_TESTING. Or doing so:

task setCustomProp << {
    System.setProperty("IS_TESTING", "true")
}

preBuild.dependsOn setCustomProp

Even tried to change the gradle.properties adding this line:

org.gradle.jvmargs=-DIS_TESTING=true

But, none of those attempts results on getting the value on the application code. I'm simply trying to get like:

@Override
  public void onCreate() {
    super.onCreate();
    String isTesting = System.getProperty("IS_TESTING");
}
like image 885
Amadeu Cavalcante Filho Avatar asked Jan 27 '26 21:01

Amadeu Cavalcante Filho


1 Answers

Why don't you just add:

buildConfigField "boolean", "IS_TESTING", "true" 

to build.gradle.

and then in your:

@Override
  public void onCreate() {
    super.onCreate();
    boolean isTesting = BuildConfig.IS_TESTING
}
like image 114
Alex Avatar answered Jan 30 '26 14:01

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!