1

I would like to launch my Android app in such a way that I can set some external variable that my app can read. It would be nice if this was possible either in Gradle or as part of the debug/run configuration.

In essence, I would like to test for a variable to see if it is set. In this example I would like to set USE_FAKE_DATA:

if (USE_FAKE_DATA) {
  ...
} else {
   ...
}

One way is to use build variants and I have done this before. But I'm wondering if another way has been made available.

Johann
  • 27,536
  • 39
  • 165
  • 279
  • You're looking to set variables on another apps? "I can set some external variable ". If you're only looking to save variables for your app and retrieve the values at any point you can use sharedPreferences – Joaquín Nov 21 '18 at 15:12
  • No. I want to start my app and have it recognize some parameters that get passed in. In a typical Java app, you can pass in command line arguments. Android does apparently support that but I prefer to have it part of Gradle or the debug/release configuration. – Johann Nov 21 '18 at 15:22

1 Answers1

2

Gradle File

android {
   buildTypes {
        debug {
            buildConfigField "boolean", "USE_FAKE_DATA", "true"
        }
        release {
            buildConfigField "boolean", "USE_FAKE_DATA", "false"
        }
    }
}

Java File

class Test extends Activity {
   @Override
   public void onCreate(Bundle data) {
     if (BuildConfig.USE_FAKE_DATA) {
       ...
     } else {
       ...
     }
   }
}

Please refer this answer for more.

hArsh
  • 111
  • 1
  • 10