Android 资源 ID 突然不是最终的,switch() 坏了 [英] Android resource IDs suddenly not final, switch()'es broken

查看:22
本文介绍了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 带回来吗?

解决方案

这发生在昨天,SDK/ADT 14 发布时:

<块引用>

从 ADT 14 开始,库项目中的资源常量不再是最后.这在 http://tools.android.com/tips/non-constant-fields 中有更详细的解释一个>

ADT 14 提供了一个快速修复:http://tools.android.com/recent/switchstatementconversion

引用理由:

<块引用>

当多个库项目组合时,实际值字段(必须是唯一的)可能会发生冲突.在 ADT 14 之前,所有字段是最终的,因此,所有图书馆都必须拥有他们所有的资源和相关的 Java 代码与 main 一起重新编译每当使用它们时进行项目.这对性能不利,因为它使构建非常缓慢.它还阻止分发库不包含源代码的项目,限制了使用范围图书馆项目.

字段不再是 final 的原因是这意味着库 jar 可以编译一次并直接在其他项目中重用.除了允许分发库项目的二进制版本(在 r15 中推出)之外,这还可以加快构建速度.

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?

解决方案

This happened about yesterday, when the SDK/ADT 14 got released:

As of ADT 14, resource constants in library projects are no longer final. This is explained in greater detail in http://tools.android.com/tips/non-constant-fields

There's a quickfix available from ADT 14: http://tools.android.com/recent/switchstatementconversion

To quote from the rationale:

When multiple library projects are combined, the actual values of the fields (which must be unique) could collide. Before ADT 14, all fields were final, so as a result, all libraries had to have all their resources and associated Java code recompiled along with the main project whenever they were used. This was bad for performance, since it made builds very slow. It also prevented distributing library projects that didn't include the source code, limiting the usage scope of library projects.

The reason the fields are no longer final is that it means that the library jars can be compiled once and reused directly in other projects. As well as allowing distributing binary version of library projects (coming in r15), this makes for much faster builds.

这篇关于Android 资源 ID 突然不是最终的,switch() 坏了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆