Cannot resolve symbol c882c94be45fff9d16a1cf845fc1

2019-02-03 20:02发布

问题:

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.

回答1:

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



回答2:

    "\"c882c94be45fff9d16a1cf845fc16ec5\""

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