How do I modify the BuildConfig .java in my Android project?
I’m participating in an online class and I have these lines of code:
String baseUrl = "http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7";
String apiKey = "&APPID=" + BuildConfig.OPEN_WEATHER_MAP_API_KEY;
URL url = new URL(baseUrl.concat(apiKey));
Therefore, the API key obviously must be set in the BuildConfig.java file.
I’m trying to do this by adding this line of code to the BuildConfig.java file :
public static final String OPEN_WEATHER_MAP_API_KEY = 111111111111111111;
The first problem is that the string is saved without quotes and does not let me compile.
The second major problem is that I can no longer modify the file. Every time I remove the line or add those missing quotes, and then try to compile, the line reverts to its defective version and compilation stops.
When I try to modify the BuildConfig.java, I also get this message: “The generated source file should not be edited. Changes are lost when the source is generated. ”
Any help is welcome.
Solution
The values in BuildConfig
come from the build system. Custom BuildConfig
values such as OPEN_WEATHER_MAP_API_KEY
come from the buildConfigField statement in the build.gradle file.
The BuildConfig.java
file is not in your build/
directory; The files there are generated by the build process and cannot be modified manually. Instead, check your build.gradle
file and find the location where the API key is defined.