Official android Technical Documentation translation-constant field in the Case tag

Source: Internet
Author: User

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 switchThe statement requires all case labels, such R.id.button1Is 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 switchThe 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 ifCheck that view. getId () is repeatedly called. if/elseChain. 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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.