Cannot resolve symbol c882c94be45fff9d16a1cf845fc16ec5
Asked Answered
R

2

8

I am a new developer exploring the world of Android. I am currently working through the Udacity tutorials for creating the Sunshine app. In the fragment activity class in order to get data from openweathermap I must add the API key I got from my account to the end of the generated URL. There is a call to BuildConfig.java in the Fragment activity (click to see the call to BuildConfig.java which is on the 6th line as part of String apiKey).

The build.gradle file is as follows:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"
    defaultConfig {
        applicationId "com.example.android.sunshine.app"
        minSdkVersion 10
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

    }
    buildTypes.each {
        it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY', 'c882c94be45fff9d16a1cf845fc16ec5'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.0'
}

in buildTypes.each it.buildConfigField is called with 'String', 'OPEN_WEATHER_MAP_API_KEY', 'c882c94be45fff9d16a1cf845fc16ec5' resulting in

public static final String OPEN_WEATHER_MAP_API_KEY = c882c94be45fff9d16a1cf845fc16ec5;

being generated in BuildConfig.java, however I keep getting this error: Cannot Resolve Symbol (click to see error message and BuildConfig.java file) I do not understand why the String OPEN_WEATHER_MAP_API_KEY is automatically being created as just a group of letters and numbers without quotes around them, but if I edit the code to read:

public static final String OPEN_WEATHER_MAP_API_KEY = "c882c94be45fff9d16a1cf845fc16ec5";

or

public static final String OPEN_WEATHER_MAP_API_KEY = 'c882c94be45fff9d16a1cf845fc16ec5';

the BuildConfig.java automatically changes itself. I am not sure what I am doing wrong and I checked many of the Udacity videos which did not have any information about this issue. Please let me know if you know how to fix this.

Regards.

Roussillon answered 27/10, 2015 at 10:37 Comment(3)
You might want to generate a new API key at Open Weather Maps if you haven't done so already as anybody could use your key in their app.Tillery
@sornars, very good observation. To all, when putting private and confidential information, like owner API Key in this case, always put dummy value.Buxom
don't worry I changed my API keyRoussillon
L
21

Change

 buildTypes.each {
     it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY', 'c882c94be45fff9d16a1cf845fc16ec5'
 }

with

 buildTypes.each {
     it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY', "\"c882c94be45fff9d16a1cf845fc16ec5\""
 }

this way OPEN_WEATHER_MAP_API_KEY should be escaped correctly

Lalita answered 27/10, 2015 at 10:39 Comment(6)
Can you explain why it needs to be formatted like this?Nucleus
@thecog the reason is the both gradle's buildConfigField and Java's strings have to be enclosed into a pair of ""Lalita
I noticed that '"appid"' works as well (single double double single).Nucleus
AppId doesn't end up in BuildConfig.javaLalita
Should it? The OPEN_WEATHER_MAP_API_KEY variable appears in BuildConfig.java (with the value defined in build.gradle) for me.Nucleus
No it can't. Only what is declared with buildConfigField explicitly by you, and "default " values apper thereLalita
G
3
    "\"c882c94be45fff9d16a1cf845fc16ec5\""

This is the correct syntax for making changes to the BuildTypes container.

Giantess answered 21/1, 2016 at 23:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.