Official android Technical Documentation translation-constant field in the Case tag
In normal Android projects, constants in the resource R class are declared as follows: public static final int main = 0x7f030004;
However, until ADT 14
Class LibraryProject, they will be declared as follows: public static int main = 0x7f030004;
In other words, none of these constants in the Library Project
Final. The reason is simple: when multiple database projects are integrated, the actual field values (which must be unique) may collide. Before ADT 14, all such fields are final, so when all libraries are used, the main project must compile all their resources and related Java code. This performance is poor because it slows down the building process. It also hinders the distribution of library projects that do not contain source code and limits the scope of use of library projects.
The reason that the field is no longer final means that the library jar package can be compiled only once and can be reused directly in other projects. And the binary version that allows the repository project (which will appear in r15 ),
This makes building faster.
However, it also has an impact on the source code of the library. The following forms of code will no longer be compiled:
Int id = view. getId (); switch (id) {case R. id. button1:
action1();
break;
case R.id.button2:
action2();
break;
case R.id.button3:
action3();
break;
}
This is because
switch
The statement requires all case labels, such
R.id.button1
Is a constant during compilation (the value can be directly
CopyTo the. class file ).
The solution is simple: Convert the switch statement to the if-else statement. Fortunately, it is very easy to operate in Eclipse. Just put the inserted symbol on the switch keyword and press Ctrl + 1 (Cmd + 1 on Mac ):
In the preceding scenario
switch
The statement is converted to the following: int id = view. getId (); if (id = R. id. button1) {action1 ();} else if (id = R. id. button2) {action2 ();} else if (id = R. id. button3) {action3 ();}
This is usually in the UI code, and its impact on performance is negligible.
We have a detector that can detect these errors (reference the Uncommon case tag of the R field) and provide a brief description of the problem (and point to this page for details .) Detailed information about automatic detection.
P.S. If your switch statement is as follows:
switch (view.getId()) {
Then the result is converted to every
if
Check that view. getId () is repeatedly called.
if/else
Chain. You need to Extract this expression first (use the refactoring shortcut key of "Extract Local Variable (Extract Local Variable)"), and then convert this switch statement.