Could not find property 'xxxx' on com.android.build.gradle.AppExtension_Decorated

隐身守侯 提交于 2019-11-28 20:37:48

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.

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)

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)

See " Open Weather Map API Key is required." at the bottom of https://github.com/udacity/Sunshine-Version-2

  1. Sign up account at http://openweathermap.org/appid#use or other weather api providers to get your unique API Key
  2. Go to your home gradle settings: ~/.gradle/gradle.properties

Add this line:

MyOpenWeatherMapApiKey="yourUniqueApiKey"
  1. Rebuild it

I had to use a little information from multiple answers on here to fix this issue.

  1. 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.
  2. You need to acquire a Open Weather Map API Key by making an account here
  3. Gradle expects you to put the key that you get from creating an Open Weather Map account into a field named 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.

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"'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!