10

I have a xml file. In the xml file I want to get a BuildConfig value. BuildConfig value is set in build.gradle.

   <?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="com_my_api_key">**REPLACE_WITH_YOUR_API_KEY**</string>
</resources>

I want to get that api Key from buildConfig, similar to how I get those values in the java code.

  public class MyApp extends Application{

      public String APPTIMIZE_KEY = BuildConfig.APPTIMIZE_KEY;

 }

Here is the value being set in build.gradle, its coming from an external file.

   release {
        buildConfigField "boolean", "APP_DEBUG", "false"
        buildConfigField "String", "APPTIMIZE_KEY", "\"${props.getProperty("apptimizeKey_App1")}\""

How do i reference this ---> BuildConfig.APPTIMIZE_KEY in the xml file to get it?

sirvon
  • 2,547
  • 1
  • 31
  • 55

1 Answers1

10

There is a lightly documented resValue counterpart to buildConfigField. I haven't tried it yet, but something along the lines of this would be where I would start:

resValue "string", "com_my_api_key", "\"${props.getProperty("apptimizeKey_App1")}\""

This would go alongside your existing buildConfigField, if you need/want that value to also be accessible from BuildConfig. You would no longer need to manually declare the string resource, but instead it would be generated for you.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • @sirvon: Since I haven't tried it yet, if you don't mind my asking, did you need to keep the backslashed inner quotation marks? `buildConfigField` needs them because the value just gets blasted right into the generated Java, but I wasn't sure if `resValue` needed them as well. – CommonsWare Feb 03 '15 at 20:20
  • Indeed you do. I first tried like this...resValue "string", "RES_FOO", "RES FOO RELEASE". To no avail. Exactly how you posted it was the victor. – sirvon Feb 03 '15 at 20:22