refuses to save api key in build.gradle Module app

965 views Asked by At

I've decided this is the best way because it keeps the key out the way if I upload to github. However it never works, I add this to build.gradle Module app

        buildTypes.each {
        it.buildConfigField 'String', 'API_KEY', 'blah blah'
    }

I don't know if it needs single or double quotes, I've tried both.

When I try to build the project it has a wobbler about BuildConfig.java,

    public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "com.newsreader.guardian.guardiantech";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "1.0";
  // Fields from build type: debug
  public static final String API_KEY = sdjhgkjsdgsdkj-skdjhgsdkjsg-skjhskjsh;

It removes all the quotations, and says "Cannot resolve symbol". It's a String so I've tried editing the quotation marks back in but the file refuses to save user changes.

Presumably I need to configure things differently in build.gradle? Help!

1

There are 1 answers

1
Blackbelt On BEST ANSWER

you have to escape it. This line

it.buildConfigField 'String', 'API_KEY', 'blah blah'

should be

it.buildConfigField 'String', 'API_KEY', "\"blah blah\""

the values end up in a java class, so the syntax of the declaration of the members has to be respected. In your case, you are declaring a type String, and its initialization requires the string to be enclosed into a pair of "