Android 资源 ID 突然不是最终的,switch() 损坏了
前言:这个问题已经过时了,它是在首选的 Android 开发环境是带有 Android 插件的 Eclipse 时编写的。
我有一段时间有一个 Java Android 项目。今天,我将 Android 开发工具更新到了 Google 的最新版本。项目失败了 - 我收到一堆“case 表达式必须是常量表达式”编译错误消息。
事实证明,R.java 文件现在以不同的方式生成。以前,它会有一堆
public static final int MyID=0x12340000;
语句; 现在,它会有一堆语句。现在,它看起来(在清理/重建之后)像这样:
public static int MyID=0x12340000;
final
消失了。因此,我拥有的(我有一些)资源 ID 上的所有开关都是错误的。请问发生什么事了?只有我吗?这里的理由是什么?它在任何地方都有记录吗?我可以以某种方式恢复final
吗?
PREAMBLE: this question is quite obsolete, it was written when the preferred Android dev environment was Eclipse with the Android plugin.
I had a Java Android project for a while. Today, I've updated the Android dev tools to the Google's latest. And the project broke - I get a bunch of "case expressions must be constant expressions" compilation error messages.
Turns out that the R.java file is being now generated differently. Formerly, it would have a bunch of
public static final int MyID=0x12340000;
statements; now, it looks (after a clean/rebuild) like this:
public static int MyID=0x12340000;
final
is gone. So all switches on resource IDs that I had (and I had a few) are wrong. What happened, please? Is it just me? What's the rationale here? Is it documented anywhere? Can I bring final
back somehow?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这发生在昨天,当 SDK/ADT 14 发布时:
ADT 14 提供了一个快速修复: http://tools.android.com/recent/switchstatementconversion
引用自这理由:
This happened about yesterday, when the SDK/ADT 14 got released:
There's a quickfix available from ADT 14: http://tools.android.com/recent/switchstatementconversion
To quote from the rationale:
只需添加括号即可:
Just add parentheses:
您可以切换到使用 If/Else 语句,警告将会消失。
样本:
You could switch over to using If/Else statements and the warning will go away.
Sample:
Google 建议您使用 if/else 条件
http://tools. android.com/tips/non-constant-fields
要自动更改它们,您可以将脱字符号放在
switch
关键字上,然后按 Alt +在 Windows 上按 Enter(在 Mac 上按 Option + Enter),然后选择将“switch”替换为“if”Google recommends you use if/else conditions
http://tools.android.com/tips/non-constant-fields
To change them automatically you can place the caret on the
switch
keyword and press Alt + Enter on Windows (Option + Enter on Mac) and select Replace 'switch' with 'if'这是因为在新的 AGP 版本中,Google 将所有资源 ID 设为非最终 ID,请在项目根目录下的
gradle.properties
文件中添加android.nonFinalResIds=false
来确保 AGP 生成最终的 res id 将使您避免修改代码。This is because in new AGP verion, Google make all rescours ids non final, add
android.nonFinalResIds=false
in yourgradle.properties
file in the root directory of your project to make sure the AGP generate final res ids will make you avoid modify your code.您应该使用视图绑定!
You should use view binding!
删除“final”关键字意味着 ID 不再是常量,因此不能用作需要常量值的 switch 语句中的标签。
Switch 语句的基础知识
在 Java 中,
switch
语句要求 case 标签是常量表达式。这意味着case
标签中使用的值必须在编译时已知并固定。您不能使用可以在运行时更改的变量。错误代码示例:
**
**
正确的代码示例:
Android 和
R.java
更改之前的行为:
更新的行为:< /强>
从 ADT 14 开始,Android 库项目现在使用:
final
关键字已从资源 ID 中删除,使其变得非常量。解决方案
将
switch
语句转换为if-else
语句来处理这些情况:The removal of the 'final' keyword means that IDs are no longer constants, and therefore cannot be used as labels in a switch statement, which requires constant values.
Basics of Switch Statements
In Java, a
switch
statement requires case labels to be constant expressions. This means that the values used in thecase
labels must be known and fixed at compile time. You cannot use variables that can change at runtime.Wrong Code Example:
**
**
Correct Code Example:
Android and
R.java
ChangesPrevious Behavior:
Updated Behavior:
As of ADT 14, Android library projects now use:
The
final
keyword has been removed from resource IDs, making them non-constant.Solution
Convert
switch
statements toif-else
statements to handle these cases:只需将这段代码添加到您的模块级
build.gradle
文件中即可:更多:
https://developer.android.com/studio/write/lint#gradle
Just add this snipped in your module-level
build.gradle
file:More:
https://developer.android.com/studio/write/lint#gradle