Eclipse环境下格式化Android的代码风格
田海立
2012-10-05
Google对Android的编码风格在Code Style Guidelinesfor Contributors中做了描述,并在Android源码中release了import和Java的配置文件android.importorder与android-formatting.xml。本文分析这些配置文件在Eclipse环境下格式化Android编码风格都做了什么,在Java和XML文件中如何具体体现。
Android源码目录<android_src_root>/development/ide/eclipse/下有文件android.importorder和android-formatting.xml,可以在Eclipse中导入import次序及Java编码风格:
1) 打开Window > Preferences > Java> Code Style;
2) 在Organizer Imports中点击Imports,选择android.importorder导入;
3) 在Formatter中点击Imports,选择android-formatting.xml导入。
下面讲解这些配置都做了什么,在代码中如何具体体现的。
一、Import的次序
Google推荐的AndroidJava文件开头import的次序(按照先后)是:
排列原则:
这个次序也是根据看import语句的重要性来排定的:首先希望看到用了android里的哪些类;然后是其他第三方的;最后才关注标准Java库里的。 不同的import分组之间是有一个空白行,在5.2.1 4)中描述。 同一import分组内部按照字母次序排列。二、缩进(Indentation)
2.1 总则
缩进只用空格,不用制表符(TAB)。缩进用4个空格,按下TAB键用4个空格代替。
/** * An example for comment formatting. This example is meant to illustrate the various possibilities offered by <i>Haili TIAN</i> in order to format comments. */ package mypackage; /** * This is the comment for the example interface. */interface Example { // This is a long comment with white space that should be split in multiple // line comments in case the linecomment formatting is enabled int foo3(); // void commented() {// System.out.println("indented");// } // void indentedCommented() { // System.out.println("indented"); // } /* block comment on first column */ int bar(); /* * These possibilities include:<ul><li>Formatting of header * comments.</li><li>Formatting of Javadoc tags</li></ul> */ int bar2(); // This is along comment that should be split in multiple line // comments in case the linecomment formatting is enabled /** * The following is some sample code whichillustrates source formatting * within javadoc comments: * * <pre> * public class Example { * final int a = 1; * * final boolean b = true; * } * </pre> * * Descriptions of parameters and returnvalues are best appended at end of * the javadoc comment. * * @param a The first parameter. For an optimum result, this should be an * odd number between 0 and 100. * @param b The second parameter. * @return The result of the foo operation, usually within 0 and 1000. */ int foo(int a, int b);} 注释每行长度是80个字符; 开启对Javadoc注释的格式化; 用html的TAG; 开启对‘pre’TAG里的Java代码片段进行格式化;Javadoc TAG前面插入空白行;#37 ~ #43
Javadoc Tag缩进§ ‘@param’TAG后面的描述缩进#47是插入的行
不对‘@param’TAG插入新行;#49的odd相对#48的a缩进了4个空格。
‘/**’和‘*/’在不同的行; 移除空白行;#48和#50的描述与参数在同一行。
开启对块注释的格式化; ‘/*’和‘*/’在不同的行; 移除空白行;Javadoc注释形式见#33~ #52
开启对行注释的格式化; 注释在行的第一列块注释形式见#26~ #29
关闭对头注释的格式化;#15, #16 和#17是行注释,且‘//’与原程序之间的空格仍旧保持。
关闭对块注释缩进到第一列;#1, #2和#3的头注释保持不变。
关闭对行注释缩进到第一列;#23是块注释,与程序有相同的缩进,不是缩进到第一列。
注:笔者对行注释验证,发现Eclipse中无论如何设置,基本不会改变其行为。
前面讲了那么多都是针对Java程序的,Android中有大量的XML文件。对XML的格式也要进行排版格式化。
打开Window> Preferences,可以通过两个地方对XML文件进行格式化:
1) XML > XML Files > Editor
对其中的各项设置进行配置
2) Android > Editors
对其中的各项设置进行配置
本文分析了Android编码风格在Eclipse环境中具体实施,通过对Eclipse环境的配置可以方便的格式化Android程序:Java文件和XML文件,特别是Java文件,其配置是基于JDT实现的,所以对Java的方方面面的配置都覆盖了。
但是,只有这些还不够,对一般的Java设计编码原则和Android中推荐的风格还不能完全自动执行,还需要人的参与,依赖于团队风格规范的制定,成员的设计能力和领悟执行能力。下面是这些工具所不能解决的风格和原则:
对Exception的处理; 写短小的方法; 域的命名规则; 程序块内的逻辑分组; 等等。