如何在我的 Android 项目上修改 BuildConfig.java?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36770218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to modify BuildConfig.java on my Android project?
提问by Emiliano Rodriguez
I'm working on an online course and i had 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));
So, the API key apparently had to be set up on the BuildConfig.java file.
因此,显然必须在 BuildConfig.java 文件上设置 API 密钥。
I tried to do that, by adding this line of code to the BuildConfig.java file:
我试图通过将这行代码添加到 BuildConfig.java 文件中来做到这一点:
public static final String OPEN_WEATHER_MAP_API_KEY = 111111111111111111;
The first issue is that the string was saved without quotation marks and wouldn't let me compile.
第一个问题是字符串保存时没有引号,不会让我编译。
The second, and main issue is that i can't modify that file anymore. Every time i delete the line or add those missing quotation marks, and then try to compile, the line reverts to it's flawed version and compilation stops.
第二个也是主要的问题是我不能再修改那个文件了。每次我删除该行或添加那些缺少的引号,然后尝试编译时,该行都会恢复为有缺陷的版本并且编译停止。
I also get this message when i try to modify BuildConfig.java : "Generated source files should not be edited. The changes will be lost when sources are generated."
当我尝试修改 BuildConfig.java 时,我也收到此消息:“不应编辑生成的源文件。生成源时更改将丢失。”
Any help will be welcome.
欢迎任何帮助。
回答by CommonsWare
Values in BuildConfigcome from the build system. Custom BuildConfigvalues like OPEN_WEATHER_MAP_API_KEYcome from buildConfigFieldstatements in your build.gradlefile. The BuildConfig.javafile is off of your build/directory; files in there are generated by the build process and cannot be modified by hand. Instead, examine your build.gradlefiles and find where your API key is defined.
值BuildConfig来自构建系统。自定义BuildConfig值,例如OPEN_WEATHER_MAP_API_KEY来自文件中的buildConfigField语句build.gradle。该BuildConfig.java文件不在您的build/目录中;那里的文件是由构建过程生成的,不能手动修改。相反,检查您的build.gradle文件并找到您的 API 密钥的定义位置。
回答by Ege Kuzubasioglu
buildTypes.each { it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY', "11111111111111111111111" }
put your API key inside ' '
将您的 API 密钥放在“ ”中
buildTypes.each { it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY',' "11111111111111111111111"' }
and sync the gradle again, it'll solve the issue.
并再次同步gradle,它会解决问题。
回答by Vlusion
I have just finished a similar course. I used the OpenWeatherMap API to. You need to get an APK Key from the OpenWeatherMap site. And put that in '1111111111111111111111111111111'
我刚刚完成了一个类似的课程。我使用了 OpenWeatherMap API。您需要从 OpenWeatherMap 站点获取 APK 密钥。并将其放入'11111111111111111111111111111111'

