Android Development --AS plug-batch solve the XML String / Color / Dimen hardcoded

1. Problem throw

1.1 development

For the daily development, each wrote a "# 333", we must hand in the current xml and colors.xml the switch back and forth to see if already defined , if the definition had to take over the multiplex, if not necessary to define a new called "# 333" resource name, and then use. The same situation exists in dimen well string, this is repeated redundant operation.

1.2 maintenance

Imagine that you took over an old project and found that color, string and dimen information exclusively in the xml file of the project in the form of hard-coded, and hundreds of projects in the xml file, then extract all the hard-coded in the project work will become very complicated.

2. Plug solve

In summary therefore, consider doing a plug-AS, can solve these two problems. Look at the effect plug-ins used :

2.1 plug-in installation

First, search the AS plug-shop YanXuan HardCode Opt to download and install.
Here Insert Picture Description

2.2 for a single file shortcut key operation

Shortcut keys Ctrl + S + Commend , the operation will be included on the color of a single file, string, and three kinds of hard-coded dimen extraction, need to pay attention to is the need after you have written the code, first Commend + S to save the contents, then perform plug-in , or there will be a conflict of pop file cache:
For a single file shortcut key hardcoded extraction operation

2.3 For bulk folder operations

Select the right plug-ins you want to perform a folder, you can choose one of the hard-coded extract, will all xml files in the selected folder full implementation of optimization and prompt execution result log in here in order to optimize the color, for example:
Folder batch operation

3. Plug solve three problems

3.1 plug-ins which can perform several levels
  • In res / layoutXXX / or res / drawableXXX / following xml file
  • / layoutXXX / or res / drawableXXX named in the res directory folder
3.2 how to find hard-coded
  • IntelliJ virtual file system VirtualFile
  • xml parsing to transfer files to Node tree, and then find the attribute values
  • string Search : copy the ever-changing, there is no law at all, it is not from the law of string to find, only to find the specified attribute that android: text and android: hint property
  • color and dimen look : Unlike string, color and dimen expression can be achieved through positive screening:
Pattern.compile(“^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$");
Pattern.compile("([1-9]\\d{0,3}|0)[.]{0,1}[5]{0,1}dp");
  • If dimen optimization performed, similar to the xdp value may be identified, where x is a positive integer greater than 0 or a decimal point 5, 0 <x <10000
  • If color optimization performed, similar to # xxx # xxxxxx, # value, can be identified XXXXXXXX
  • Filtered databinding styles
After finding how to replace 3.3
  • For dimen and color will give priority to fetch buffer , if hit directly out of existing resources file name, does not change the content value file. The string does not read cache will always generate a new value
  • Node values ​​directly modify the way, this embodiment modifies the original code (for example, the original code will format) To minimize changes to the existing code, rejection of the project
  • Throughout the entire file as a character string, to find a position to be replaced by String.indexOf manner, replacing Use the replace way, and then write the file to complete the replacement
  • values directories, and dimen / string / color file will automatically identify or generate
  • Named for new resources:
 - color以color_xxx来命名;
 - string以layout's name_text_index来命名;
 - dimen以size_xxdp来命名,带.5的小数以下划线代替小数点。

Note 4. When using plug-ins

  • For a single file, and supports the right shortcuts in two ways, Three Optimized default shortcut keys to perform together. After you have written the code necessary, first Commend + S to save the contents, then perform the plug , or there will be a conflict of pop file cache
  • For folders, for performance, only support the right to select an optimal way
  • After the plug-in is finished, it will in the Event Log will reuse item value in your project that already exists in the output of the results, color and dimen, but because of the strong string tied to business, and therefore will generate a new item value
  • Welcome to download to try and make comments -
Published 142 original articles · won praise 1456 · Views 1.68 million +

Guess you like

Origin blog.csdn.net/SEU_Calvin/article/details/104006415