Reputation: 717
I got this error after I added these lines to my gradle file:
buildTypes.each {
it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY', MyOpenWeatherMapApiKey
}
then the log show:
Could not find property 'MyOpenWeatherMapApiKey' on com.android.build.gradle.AppExtension_Decorated@c3b784
The solutions on google that I searched cannot solve my problem. Please show me where I was wrong?
Upvotes: 16
Views: 21300
Reputation: 890
The accepted answer is absolutely correct. Another way, probably simpler, to format the value inside as the String is like this:
it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY', '"xxxxxxxxxxxxxxxxxx"'
Upvotes: 0
Reputation: 59
Add this line:
MyOpenWeatherMapApiKey="yourUniqueApiKey"
Upvotes: 4
Reputation: 39
I had to use a little information from multiple answers on here to fix this issue.
it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY', MyOpenWeatherMapApiKey
is fine. The top voted answer puts the key directly into this field rather than referencing a "global" key for your user.MyOpenWeatherMapApiKey
that is referenced by OPEN_WEATHER_MAP_API_KEY
from before. To do this, open gradle.properties in Android Studio and add MyOpenWeatherMapApiKey="<Your Key Here>"
Now you should be able to build the app with no issues.
Upvotes: 2
Reputation: 339
See " Open Weather Map API Key is required." at the bottom of https://github.com/udacity/Sunshine-Version-2
Upvotes: 5
Reputation: 15504
You should define MyOpenWeatherMapApiKey
in your local user settings, so, go to your home gradle settings: ~/.gradle/gradle.properties
(Win: %USERPROFILE%\.gradle\gradle.properties
). If gradle.properties
does not exist - just create it.
In the file add following line:
MyOpenWeatherMapApiKey="XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
(unfortunately, Android Udacity teachers were not very nice to explain how does it work from gradle perspective; as same as I've not easily found any documentation from gradle how it.buildConfigField works)
Upvotes: 13
Reputation: 862
The 'OPEN_WEATHER_MAP_API_KEY' references a gradle property named 'MyOpenWeatherMapApiKey' that needs to be configured.
One reason is for the build system to generate the code for this. Another might be so that you don't accidentally commit your API-KEY to GitHub or other public repo.
What you should do is add an entry to your 'gradle.properties' file like this:
MyOpenWeatherMapApiKey="[YOUR-API-KEY]"
Then sync your project with gradle (if using Android Studio)
Upvotes: 11
Reputation: 363737
Since you are using a String
you have to use this syntax:
buildConfigField "String" , "OPEN_WEATHER_MAP_API_KEY" , "\"XXXXX-XXXXX-XXX\""
The last parameter has to be a String
Otherwise you can use something like this:
resValue "string", "OPEN_WEATHER_MAP_API_KEY", "\"XXXXX-XXXXX-XXX\""
The first case generates a constants iin your BuildConfig
file.
The second case generates a string resource value that can be accessed using the @string/OPEN_WEATHER_MAP_API_KEY
annotation.
Upvotes: 26