1

Goal

Building APKs for different environments using build types

Approach

I'm using this post to build my apk without android studio. Everything works except when I use environment variables in gradle:

Before build, I set the environment variable

export MY_API_TOKEN="ABCDEFGH"

Then in my build.gradle:

buildTypes {

  debug {
    buildConfigField("String", "MY_API_TOKEN", System.getenv('MY_API_TOKEN'))
  }

}

And when I execute : gradle assemble I get this error:

/home/apps/app/src/main/java/com/my/package/controller/api/MyAwesomeCode.java:64: error: cannot find symbol
                BuildConfig.MY_API_TOKEN;
                                  ^
  symbol:   variable BuildConfig
  location: class RestAPI
/home/apps/app/build/generated/source/buildConfig/debug/com/my/package/BuildConfig.java:14: error: cannot find symbol
  public static final String MY_API_TOKEN = ABCDEFGH;
                                               ^
  symbol:   variable ABCDEFGH
  location: class BuildConfig

I tried with several combinations, and I get the same error:

I also verified the same behavior with variables

def MY_API_TOKEN_VAR = "ABCDEFGH"

debug {
  buildConfigField "String", "MY_API_TOKEN", MY_API_TOKEN_VAR
}

Error :

BuildConfig.java:14: error: cannot find symbol
public static final String MY_API_TOKEN = ABCDEFGH;
                                          ^
symbol:   variable ABCDEFGH
location: class BuildConfig

Question

buildTypes works only for hardcoded values in build.gradle?

JRichardsz
  • 14,356
  • 6
  • 59
  • 94

2 Answers2

0

BuildConfig field values with string type should be declared with the field value in escaped quotes:

android {
    ...
    defaultConfig {
        ...
        def MY_API_TOKEN_VAR = "ABCDEFGH"
        buildConfigField "String", "MY_API_TOKEN", "\"ABCDEFGH\""
    }
    ...
}

With system environment variable:

android {
    ...
    defaultConfig {
        ...
        def MY_API_TOKEN_VAR = System.getenv('MY_API_TOKEN')
        buildConfigField "String", "MY_API_TOKEN", "\"ABCDEFGH\""
    }
    ...
}

When you don't declare the field value in escaped quotes, you can see the syntax error in generated BuildConfig:

public final class BuildConfig {
  // ...
  // Fields from build type: debug
  public static final String MY_API_TOKEN = ABCD; // Should be "ABCD"
}
Natig Babayev
  • 3,128
  • 15
  • 23
  • Thank you very much Natig. I tried but the error is the same. Please if you can, check my attempts: https://gist.github.com/jrichardsz/803ebd6eec990f6880ff16b5e6b5b194 **attempt_2** is the most promising – JRichardsz Sep 24 '19 at 20:00
  • Second attempt is not promising, because as I mentioned in my explanation, you cannot set value for string without escaped quotes when creating buildConfigField. Check my updated code to fix the issue. – Natig Babayev Sep 24 '19 at 21:36
  • I mean promising because, at least value is replaced. With attempt_1 , the error is before replacement. – JRichardsz Sep 25 '19 at 03:08
  • How can I do that? I'm not using android studio, just full linux shell with a minimal text editor. I appreciate your time and attention. – JRichardsz Sep 25 '19 at 14:14
0

Assuming given export MY_API_TOKEN="ABC" (please make sure variable is visible for your build system of choice) and your config is configured like this:

buildConfigField("String", "MY_API_TOKEN", "\"" + System.getenv("MY_API_TOKEN") + "\"")

...your BuildConfig class will have this extra field after sync/rebuild:

public static final String MY_API_TOKEN = "ABC";
ror
  • 3,295
  • 1
  • 19
  • 27