1. Project guidelines
工程指南
1.1 Project structure
工程架构
New projects should follow the Android Gradle project structure that is defined on the Android Gradle plugin user guide. The ribot Boilerplate project is a good reference to start from.
新的工程应该跟从安卓的Gradle工程架构(Android Gradle plugin user guide, ribot Boilerplate project 这个工程是一个很好的参考来开始学习)
1.2 File naming
文件命名
1.2.1 Class files
类文件
Class names are written in UpperCamelCase.
类名 大写首字母,符合驼峰编码
For classes that extend an Android component, the name of the class should end with the name of the component; for example: SignInActivity
, SignInFragment
, ImageUploaderService
, ChangePasswordDialog
.
继承自安卓组件的类,类名需要以组件名结尾。 如SignInActivity
, SignInFragment
, ImageUploaderService
, ChangePasswordDialog
.
1.2.2 Resources files
资源文件
Resources file names are written in lowercase_underscore.
资源文件使用 小写字母+ 下划线
1.2.2.1 Drawable files
图片文件
Naming conventions for drawables:
图片的命名约定
Asset Type | Prefix | Example |
---|---|---|
Action bar | ab_ |
ab_stacked.9.png |
Button | btn_ |
btn_send_pressed.9.png |
Dialog | dialog_ |
dialog_top.9.png |
Divider | divider_ |
divider_horizontal.9.png |
Icon | ic_ |
ic_star.png |
Menu | menu_ |
menu_submenu_bg.9.png |
Notification | notification_ |
notification_bg.9.png |
Tabs | tab_ |
tab_pressed.9.png |
Asset Type | Prefix | Example |
---|---|---|
Icons | ic_ |
ic_star.png |
Launcher icons | ic_launcher |
ic_launcher_calendar.png |
Menu icons and Action Bar icons | ic_menu |
ic_menu_archive.png |
Status bar icons | ic_stat_notify |
ic_stat_notify_msg.png |
Tab icons | ic_tab |
ic_tab_recent.png |
Dialog icons | ic_dialog |
ic_dialog_info.png |
Naming conventions for icons (taken from Android iconography guidelines):
图标的命名约定(来自[Android iconography guidelines](http://developer.android.com/design/style/iconography.html)
Asset Type | Prefix | Example |
---|---|---|
Icons | ic_ |
ic_star.png |
Launcher icons | ic_launcher |
ic_launcher_calendar.png |
Menu icons and Action Bar icons | ic_menu |
ic_menu_archive.png |
Status bar icons | ic_stat_notify |
ic_stat_notify_msg.png |
Tab icons | ic_tab |
ic_tab_recent.png |
Dialog icons | ic_dialog |
ic_dialog_info.png |
State | Suffix | Example |
---|---|---|
Normal | _normal |
btn_order_normal.9.png |
Pressed | _pressed |
btn_order_pressed.9.png |
Focused | _focused |
btn_order_focused.9.png |
Disabled | _disabled |
btn_order_disabled.9.png |
Selected | _selected |
btn_order_selected.9.png |
Naming conventions for selector states:
选择器状态的命名约定
State | Suffix | Example |
---|---|---|
Normal | _normal |
btn_order_normal.9.png |
Pressed | _pressed |
btn_order_pressed.9.png |
Focused | _focused |
btn_order_focused.9.png |
Disabled | _disabled |
btn_order_disabled.9.png |
Selected | _selected |
btn_order_selected.9.png |
1.2.2.2 Layout files
Component | Class Name | Layout Name |
---|---|---|
Activity | UserProfileActivity |
activity_user_profile.xml |
Fragment | SignUpFragment |
fragment_sign_up.xml |
Dialog | ChangePasswordDialog |
dialog_change_password.xml |
AdapterView item | --- | item_person.xml |
Partial layout | --- | partial_stats_bar.xml |
布局文件
Layout files should match the name of the Android components that they are intended for but moving the top level component name to the beginning. For example, if we are creating a layout for the SignInActivity
, the name of the layout file should be activity_sign_in.xml
.
布局文件需要匹配安卓的组件的名字。但是把组件的名字放在前面。例如你要创建SignInActivity
的布局,那么文件名应该是:activity_sign_in.xml
.
Component | Class Name | Layout Name |
---|---|---|
Activity | UserProfileActivity |
activity_user_profile.xml |
Fragment | SignUpFragment |
fragment_sign_up.xml |
Dialog | ChangePasswordDialog |
dialog_change_password.xml |
AdapterView item | --- | item_person.xml |
Partial layout | --- | partial_stats_bar.xml |
A slightly different case is when we are creating a layout that is going to be inflated by an Adapter
, e.g to populate a ListView
. In this case, the name of the layout should start with item_
.
当我们创建给Adapter
创建一个布局时,跟上面有点的轻微差别。比如,为了填充一个ListView
,布局文件应该以item_
开头
Note that there are cases where these rules will not be possible to apply. For example, when creating layout files that are intended to be part of other layouts. In this case you should use the prefix partial_
.
请注意,在某些情况下,这些规则将不可能适用。例如,当准备给其他的布局创建部分布局的时候,你需要使用前缀partial_
。
1.2.2.3 Menu files
菜单文件
Similar to layout files, menu files should match the name of the component. For example, if we are defining a menu file that is going to be used in the UserActivity
, then the name of the file should be activity_user.xml
类似布局文件。
A good practice is to not include the word menu
as part of the name because these files are already located in the menu
directory.
一个好的做法是 不要包含 menu
这个单词,因为文件已经在 menu
这个文件夹下。
1.2.2.4 Values files
参数文件
Resource files in the values folder should be plural, e.g. strings.xml
, styles.xml
, colors.xml
, dimens.xml
, attrs.xml
2 Code guidelines
代码指南
2.1 Java language rules
Java 代码规则
2.1.1 Don't ignore exceptions
不要忽略异常
You must never do the following:
不要类似下面这样做
void setServerPort(String value) {
try {
serverPort = Integer.parseInt(value);
} catch (NumberFormatException e) { }
}
While you may think that your code will never encounter this error condition or that it is not important to handle it, ignoring exceptions like above creates mines in your code for someone else to trip over some day. You must handle every Exception in your code in some principled way. The specific handling varies depending on the case. - (Android code style guidelines)
当你让你认为你们的代码不会触发这个错误条件或者这个处理不重要,忽略异常就像上面代码的做法,那么终于一天 其他人可能会被绊倒。有必须有原则的处理每个代码内的异常。具体的处理方法根据具体的情况而不同(Android code style guidelines)
See alternatives here.
另外可以可以看这里.
2.1.2 Don't catch generic exception
不要捕获通用的异常
You should not do this:
try {
someComplicatedIOFunction(); // may throw IOException
someComplicatedParsingFunction(); // may throw ParsingException
someComplicatedSecurityFunction(); // may throw SecurityException
// phew, made it all the way
} catch (Exception e) { // I'll just catch all exceptions
handleError(); // with one generic handler!
}
See the reason why and some alternatives here
看下为什么这么做和一些替换方案,这里
2.1.3 Don't use finalizers
不要使用finalizers
We don't use finalizers. There are no guarantees as to when a finalizer will be called, or even that it will be called at all. In most cases, you can do what you need from a finalizer with good exception handling. If you absolutely need it, define a close()
method (or the like) and document exactly when that method needs to be called. See InputStream
for an example. In this case it is appropriate but not required to print a short log message from the finalizer, as long as it is not expected to flood the logs. - (Android code style guidelines)
_我们不要使用finalizers,没有办法保证,finalizers是否已经调用,或者 甚至是否会被调用。大部分情况,替代finalizer 你应该做你想需要的做的,同时写好的异常处理。 如果你确实需要写finalizer,需要定一个 close()
方法或类似,同时写清楚为什么要用的文档。以InputStream
为例,在这种情况下它是合适的但不需要打印一个简短的finalizers日志消息,只要它不会淹没日志。
2.1.4 Fully qualify imports
imports 路径使用全称
This is bad: import foo.*;
This is good: import foo.Bar;
See more info here
2.2 Java style rules
Java风格规则
2.2.1 Fields definition and naming
字段的定义和命名
Fields should be defined at the top of the file and they should follow the naming rules listed below.
字段应该定义在文件头部 同时要遵守下面的列出来的命名规则。
- Private, non-static field names start with m.
私有的,非静态 字段 使用 m开头 - Private, static field names start with s.
私有,静态字段 命名以s开头 - Other fields start with a lower case letter.
其他字段使用小写字母 - Static final fields (constants) are ALL_CAPS_WITH_UNDERSCORES.
Static final 字段使用 全部大写
Example:
public class MyClass {
public static final int SOME_CONSTANT = 42;
public int publicField;
private static MyClass sSingleton;
int mPackagePrivate;
private int mPrivate;
protected int mProtected;
}
2.2.3 Treat acronyms as words
Good | Bad |
---|---|
XmlHttpRequest |
XMLHTTPRequest |
getCustomerId |
getCustomerID |
String url |
String URL |
long id |
long ID |
首字母缩写
Good | Bad |
---|---|
XmlHttpRequest |
XMLHTTPRequest |
getCustomerId |
getCustomerID |
String url |
String URL |
long id |
long ID |
2.2.4 Use spaces for indentation
使用空格缩进
Use 4 space indents for blocks:
使用4 个空格来给块缩进
if (x == 1) {
x++;
}
Use 8 space indents for line wraps:
使用8 空格 来个换行 缩进
Instrument i =
someLongExpression(that, wouldNotFit, on, one, line);
2.2.5 Use standard brace style
使用标准的括号风格
Braces go on the same line as the code before them.
括号可代码同一行
class MyClass {
int func() {
if (something) {
// ...
} else if (somethingElse) {
// ...
} else {
// ...
}
}
}
Braces around the statements are required unless the condition and the body fit on one line.
括号应该包围语句, 除非条件和身体 适应在同一行
If the condition and the body fit on one line and that line is shorter than the max line length, then braces are not required, e.g.
如果条件和身体适应同一行,同时这行的长度小于最大行长,那么括号不不需要,比如:
if (condition) body();
This is bad:
这是 坏的:
if (condition)
body(); // bad!
2.2.6 Annotations
注释
2.2.6.1 Annotations practices
注释实践
According to the Android code style guide, the standard practices for some of the predefined annotations in Java are:
根据 安卓代码风格指南,Java里面有些预定义注释的标准实践。
-
@Override
: The @Override annotation must be used whenever a method overrides the declaration or implementation from a super-class. For example, if you use the @inheritdocs Javadoc tag, and derive from a class (not an interface), you must also annotate that the method @Overrides the parent class's method. -
@SuppressWarnings
: The @SuppressWarnings annotation should only be used under circumstances where it is impossible to eliminate a warning. If a warning passes this "impossible to eliminate" test, the @SuppressWarnings annotation must be used, so as to ensure that all warnings reflect actual problems in the code.
More information about annotation guidelines can be found here.
2.2.6.2 Annotations style
Classes, Methods and Constructors
When annotations are applied to a class, method, or constructor, they are listed after the documentation block and should appear as one annotation per line .
/* This is the documentation block about the class */
@AnnotationA
@AnnotationB
public class MyAnnotatedClass { }
Fields
Annotations applying to fields should be listed on the same line, unless the line reaches the maximum line length.
@Nullable @Mock DataManager mDataManager;
2.2.7 Limit variable scope
限制变量作用域
The scope of local variables should be kept to a minimum (Effective Java Item 29). By doing so, you increase the readability and maintainability of your code and reduce the likelihood of error. Each variable should be declared in the innermost block that encloses all uses of the variable.
Local variables should be declared at the point they are first used. Nearly every local variable declaration should contain an initializer. If you don't yet have enough information to initialize a variable sensibly, you should postpone the declaration until you do. - (Android code style guidelines)
2.2.8 Order import statements
import 语句顺序
If you are using an IDE such as Android Studio, you don't have to worry about this because your IDE is already obeying these rules. If not, have a look below.
如果你使用IDE 比如 Android Studio, 那么你不必要担心这部分,因为IDE已经遵循这个规则。
The ordering of import statements is:
- Android imports
- Imports from third parties (com, junit, net, org)
- java and javax
- Same project imports
To exactly match the IDE settings, the imports should be:
为了精确匹配IDE设置,imports 应该
- Alphabetically ordered within each grouping, with capital letters before lower case letters (e.g. Z before a).
- There should be a blank line between each major grouping (android, com, junit, net, org, java, javax).
More info here
2.2.9 Logging guidelines
日志的指南
Use the logging methods provided by the Log
class to print out error messages or other information that may be useful for developers to identify issues:
使用 Log
类提供的日志方法打印出错误信息或其他信息可能有助于开发人员识别问题:
-
Log.v(String tag, String msg)
(verbose)
详细 -
Log.d(String tag, String msg)
(debug)
调试 -
Log.i(String tag, String msg)
(information)
信息 -
Log.w(String tag, String msg)
(warning)
警告 -
Log.e(String tag, String msg)
(error)
错误
As a general rule, we use the class name as tag and we define it as a static final
field at the top of the file. For example:
public class MyClass {
private static final String TAG = "MyClass";
public myMethod() {
Log.e(TAG, "My error message");
}
}
VERBOSE and DEBUG logs must be disabled on release builds. It is also recommended to disable INFORMATION, WARNING and ERROR logs but you may want to keep them enabled if you think they may be useful to identify issues on release builds. If you decide to leave them enabled, you have to make sure that they are not leaking private information such as email addresses, user ids, etc.
To only show logs on debug builds:
if (BuildConfig.DEBUG) Log.d(TAG, "The value of x is " + x);
2.2.10 Class member ordering
类成员顺序
There is no single correct solution for this but using a logical and consistent order will improve code learnability and readability. It is recommendable to use the following order:
每一个简单正确的方法,但是 使用有逻辑 and 始终如一 的顺序可以提高代码的可读性和易学性。推荐使用下面的顺序
- Constants
常量 - Fields
字段 - Constructors
构造器 - Override methods and callbacks (public or private)
Override方法 和回调 - Public methods
- Private methods
- Inner classes or interfaces
Example:
public class MainActivity extends Activity {
private String mTitle;
private TextView mTextViewTitle;
//构造器
public void setTitle(String title) {
mTitle = title;
}
@Override
public void onCreate() {
...
}
private void setUpView() {
...
}
static class AnInnerClass {
}
}
If your class is extending an Android component such as an Activity or a Fragment, it is a good practice to order the override methods so that they match the component's lifecycle. For example, if you have an Activity that implements onCreate()
, onDestroy()
, onPause()
and onResume()
, then the correct order is:
如果你的类继承 安卓组件 比如Activity or a Fragment,重写方法 好的实践是匹配 组件的 生命周期。
public class MainActivity extends Activity {
//Order matches Activity lifecycle
@Override
public void onCreate() {}
@Override
public void onResume() {}
@Override
public void onPause() {}
@Override
public void onDestroy() {}
}
2.2.11 Parameter ordering in methods
参数排序方法
When programming for Android, it is quite common to define methods that take a Context
. If you are writing a method like this, then the Context must be the first parameter.
当编程安卓的时候,很经常要定义方法,使用Context
参数。Context
这个参数 要作为第一个参数
The opposite case are callback interfaces that should always be the last parameter.
相反的,callback 接口,应该做最后一个参数
Examples:
// Context always goes first
public User loadUser(Context context, int userId);
// Callbacks always go last
public void loadUserAsync(Context context, int userId, UserCallback callback);
2.2.13 String constants, naming, and values
字符串常量,命名和 赋值
Many elements of the Android SDK such as SharedPreferences
, Bundle
, or Intent
use a key-value pair approach so it's very likely that even for a small app you end up having to write a lot of String constants.
许多Android SDK的元素如 SharedPreferences
, Bundle
, or Intent
”使用一个键-值对 的方法,即使是一个小程序你最终不得不写大量的字符串常量。
When using one of these components, you must define the keys as a static final
fields and they should be prefixed as indicated below.
Element | Field Name Prefix |
---|---|
SharedPreferences | PREF_ |
Bundle | BUNDLE_ |
Fragment Arguments | ARGUMENT_ |
Intent Extra | EXTRA_ |
Intent Action | ACTION_ |
Note that the arguments of a Fragment - Fragment.getArguments()
- are also a Bundle. However, because this is a quite common use of Bundles, we define a different prefix for them.
Example:
// Note the value of the field is the same as the name to avoid duplication issues
static final String PREF_EMAIL = "PREF_EMAIL";
static final String BUNDLE_AGE = "BUNDLE_AGE";
static final String ARGUMENT_USER_ID = "ARGUMENT_USER_ID";
// Intent-related items use full package name as value
static final String EXTRA_SURNAME = "com.myapp.extras.EXTRA_SURNAME";
static final String ACTION_OPEN_USER = "com.myapp.action.ACTION_OPEN_USER";
2.2.14 Arguments in Fragments and Activities
Fragments and Activities的参数
When data is passed into an Activity
or Fragment
via an Intent
or a Bundle
, the keys for the different values must follow the rules described in the section above.
When an Activity
or Fragment
expects arguments, it should provide a public static
method that facilitates the creation of the relevant Intent
or Fragment
.
当 Activity
or Fragment
需要参数,我们应该提供一个 public static
方法来 帮助Intent
or Fragment
的创建。
In the case of Activities the method is usually called getStartIntent()
:
public static Intent getStartIntent(Context context, User user) {
Intent intent = new Intent(context, ThisActivity.class);
intent.putParcelableExtra(EXTRA_USER, user);
return intent;
}
For Fragments it is named newInstance()
and handles the creation of the Fragment with the right arguments:
public static UserFragment newInstance(User user) {
UserFragment fragment = new UserFragment;
Bundle args = new Bundle();
args.putParcelable(ARGUMENT_USER, user);
fragment.setArguments(args)
return fragment;
}
Note 1: These methods should go at the top of the class before onCreate()
.
注意1:应该在 onCreate()
之前.
Note 2: If we provide the methods described above, the keys for extras and arguments should be private
because there is not need for them to be exposed outside the class.
注意2:如果我们提供了上面的方法,那么这些参数的Key应该 保持private
2.2.15 Line length limit
一行长度限制
Code lines should not exceed 100 characters. If the line is longer than this limit there are usually two options to reduce its length:
- Extract a local variable or method (preferable).
- Apply line-wrapping to divide a single line into multiple ones.
There are two exceptions where it is possible to have lines longer than 100:
- Lines that are not possible to split, e.g. long URLs in comments.
-
package
andimport
statements.
2.2.15.1 Line-wrapping strategies
There isn't an exact formula that explains how to line-wrap and quite often different solutions are valid. However there are a few rules that can be applied to common cases.
Break at operators
When the line is broken at an operator, the break comes before the operator. For example:
int longName = anotherVeryLongVariable + anEvenLongerOne - thisRidiculousLongOne
+ theFinalOne;
Assignment Operator Exception
An exception to the break at operators
rule is the assignment operator =
, where the line break should happen after the operator.
int longName =
anotherVeryLongVariable + anEvenLongerOne - thisRidiculousLongOne + theFinalOne;
Method chain case
When multiple methods are chained in the same line - for example when using Builders - every call to a method should go in its own line, breaking the line before the .
Picasso.with(context).load("http://ribot.co.uk/images/sexyjoe.jpg").into(imageView);
Picasso.with(context)
.load("http://ribot.co.uk/images/sexyjoe.jpg")
.into(imageView);
Long parameters case
When a method has many parameters or its parameters are very long, we should break the line after every comma ,
loadPicture(context, "http://ribot.co.uk/images/sexyjoe.jpg", mImageViewProfilePicture, clickListener, "Title of the picture");
loadPicture(context,
"http://ribot.co.uk/images/sexyjoe.jpg",
mImageViewProfilePicture,
clickListener,
"Title of the picture");
2.2.16 RxJava chains styling
Rx chains of operators require line-wrapping. Every operator must go in a new line and the line should be broken before the .
public Observable<Location> syncLocations() {
return mDatabaseHelper.getAllLocations()
.concatMap(new Func1<Location, Observable<? extends Location>>() {
@Override
public Observable<? extends Location> call(Location location) {
return mRetrofitService.getLocation(location.id);
}
})
.retry(new Func2<Integer, Throwable, Boolean>() {
@Override
public Boolean call(Integer numRetries, Throwable throwable) {
return throwable instanceof RetrofitError;
}
});
}
2.3 XML style rules
2.3.1 Use self closing tags
When an XML element doesn't have any contents, you must use self closing tags.
This is good:
<TextView
android:id="@+id/text_view_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
This is bad :
<!-- Don\'t do this! -->
<TextView
android:id="@+id/text_view_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TextView>
2.3.2 Resources naming
Resource IDs and names are written in lowercase_underscore.
2.3.2.1 ID naming
IDs should be prefixed with the name of the element in lowercase underscore. For example:
Element | Prefix |
---|---|
TextView |
text_ |
ImageView |
image_ |
Button |
button_ |
Menu |
menu_ |
Image view example:
<ImageView
android:id="@+id/image_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Menu example:
<menu>
<item
android:id="@+id/menu_done"
android:title="Done" />
</menu>
2.3.2.2 Strings
String names start with a prefix that identifies the section they belong to. For example registration_email_hint
or registration_name_hint
. If a string doesn't belong to any section, then you should follow the rules below:
Prefix | Description |
---|---|
error_ |
An error message |
msg_ |
A regular information message |
title_ |
A title, i.e. a dialog title |
action_ |
An action such as "Save" or "Create" |
2.3.2.3 Styles and Themes
Unless the rest of resources, style names are written in UpperCamelCase.
2.3.3 Attributes ordering
As a general rule you should try to group similar attributes together. A good way of ordering the most common attributes is:
- View Id
- Style
- Layout width and layout height
- Other layout attributes, sorted alphabetically
- Remaining attributes, sorted alphabetically
2.4 Tests style rules
2.4.1 Unit tests
Test classes should match the name of the class the tests are targeting, followed by Test
. For example, if we create a test class that contains tests for the DatabaseHelper
, we should name it DatabaseHelperTest
.
Test methods are annotated with @Test
and should generally start with the name of the method that is being tested, followed by a precondition and/or expected behaviour.
- Template:
@Test void methodNamePreconditionExpectedBehaviour()
- Example:
@Test void signInWithEmptyEmailFails()
Precondition and/or expected behaviour may not always be required if the test is clear enough without them.
Sometimes a class may contain a large amount of methods, that at the same time require several tests for each method. In this case, it's recommendable to split up the test class into multiple ones. For example, if the DataManager
contains a lot of methods we may want to divide it into DataManagerSignInTest
, DataManagerLoadUsersTest
, etc. Generally you will be able to see what tests belong together because they have common test fixtures.
2.4.2 Espresso tests
Every Espresso test class usually targets an Activity, therefore the name should match the name of the targeted Activity followed by Test
, e.g. SignInActivityTest
When using the Espresso API it is a common practice to place chained methods in new lines.
onView(withId(R.id.view))
.perform(scrollTo())
.check(matches(isDisplayed()))
License
Copyright 2015 Ribot Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
网友评论