It is very important for developers to write clean and standard code, which shows their expertise and skill level, and is appreciated by others. For the team, the team members follow the uniform code specification, can better maintain the stability of the code base harmony. Android Advanced Advanced Reading Notes

Android is developed based on the Java language, so when it comes to coding specifications, you first need to follow the Java coding specifications.

– 1 Java coding specifications

1.1 Definition of source code files

The source code file is named by the top-level Java class in the file content, and is case-sensitive. The file extension is.java, and the encoding format of the file is UTF-8.

1.2 Structure of source code file

A complete source code file consists of four parts

  • Copyright information or licenses such as Android RxJava copyright notice:
/** - Copyright (C) 2016-present, RxJava POLYMORPHISM. -- 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.
 */Copy the code
  • Declaration of the package where the Java files are located
  • Import import statements for dependencies that Java classes need to use. Wildcard characters are generally not recommended, for exampleimport android.annotation.*Instead, introduce concrete classes such asimport android.annotation.SdkConstant;. Import statements are also grouped by type, and the groups are separated by empty lines. The formatting function of the IDE is used automatically.
  • There is one and only one definition of a Java top-level class, but there can be an inner class definition. The members of each class need to be sorted according to some kind of logic, and maintainers need to add new members according to the same logic, rather than just putting new members at the end. At the same time, multiple overloaded methods should be placed together in order, with no other methods inserted in between.

1.3 Format that follows

Format is mainly related to the layout of the code, which needs to be focused on the following:

  • Use curly braces: statements such as if, else, for, do, and while should be used with curly braces, even if there is only one statement or if it is empty. The following is incorrect and may introduce bugs in later code maintenance.
if(isLogin)
    login();
else
    toLogin();Copy the code

The correct way to write it is with curly braces:

if(isLogin){
    login();
}else{
    toLogin();
}Copy the code
  • Limit the number of column characters: Depending on the display used within the team and IDE specific problems, the ultimate goal is to ensure good code readable, generally common is to agree on a line of 80 or 100 characters, more than part of the IDE automatically wrapped.
  • Use of white space: do not write all code together, need to group according to logic, vertical white space principle mainly includes: a) inside the method, use blank lines between logical groups of statements; B) Add blank lines between contiguous members of a class (fields, constructors, normal methods, nested inner classes, statically initialized code blocks, etc.). Of course, there is no need to add multiple blank lines, just one.
  • A switch statement: In addition to indentation and blank use of switch statements, if it is clear that a break statement is not needed between two consecutive cases, it is recommended to add // fall through comments to facilitate the code maintainer’s understanding. At the same time, a default statement must be added. A switch statement that conforms to the specification is as follows.
        switch (input) {
            case1:case2:doOneTwo();
                // fall through
            case3:doOneTwoThree();
                break;
            default:
                dobigNumber();
        }Copy the code
  • Order of modifiers: Class and member modifiers must be sorted as defined in the Java language specification:
public protected private abstract static final transient volatile synchronized native strictfpCopy the code

1.4 Naming Conventions

Classes are named following the big-camel nomenclature UpperCamelCase, while method and variable names follow the small-camel nomenclature lowerCamelCase. Constant names are uppercase letters separated by an underscore, for example, static final Int CONNECTION_TIMEOUT = 10000.

1.5 the Javadoc

Common tags and meanings in standard Javadoc are as follows:

/** * Javadoc common markup ** @param method parameter description * @returnDescription of the method return value * @throws contempt for the exception thrown by the method * @version module version number * @author module author * @see reference direction * @Deprecated flag is deprecated */Copy the code

2 Android naming conventions

2.1 Layout file naming

Layout files are named in lower case prefix _ logical name

  • The Activity layout file: activity_xxx;
  • Fragment layout file: fragment_xxx;
  • Custom control layout file: view_xxx;
  • Dialog box layout file: dialog_xxx;
  • Layout file for list items: item_xxx;

But for the development of large projects. The list of nearly 100 layouts at the beginning of an activity is still blind. So that would be preceded by the module name.

2.2 Naming resource files

Resource files are named using the prefix module name logical name. All words are lowercase

  • The button name is prefixed with BTN, for example, btn_login. PNG. If the button has multiple configurations, you need to add the configurations, for example, btn_login_normal. PNG or btn_login_pressed
  • Icon names are prefixed with IC, for example, ic_share.png
  • Background images are named with a bg prefix, such as bg_main.png
  • The name of the dividing line is prefixed with the divider, for example, divider_gry.png

2.3 Naming of classes

Classes are named in accordance with the Java class naming convention, that is, the use of large camel name, and the introduction of Android-related naming rules according to the specific purpose of the class.

  • The Activity class needs an Activity suffix, such as MainActivity
  • The Fragment class requires a Fragment suffix, such as HomeFragment
  • The Service class must have Service as the suffix, for example, DownloadService
  • The BroadcastReceiver class needs the suffix “Receiver”, such as “PushReceiver”
  • The ContentProvider class needs a Provider suffix, such as ContactProvider
  • Utility classes need the Util suffix, such as NetworkUtil
  • Custom common Base classes start with Base, such as BaseActivity
  • Unit Test classes have the suffix Test, such as HashTest

2.4 Variable Naming

It starts with m. Example When mAdapter is used, press an M and it all comes out

2.5 Method Naming

Better to write a good comment than a good name.

3. Use of CheckStyle

CheckStyle is a code specification checker for the Java language. By default, it follows Google’s Java code specification and Sun’s code specification, but it is highly configurable and can be tailored or added by different teams to suit their needs.

Write the picture description here

Restart Android Studio after the installation is complete

Then configure checkStyle.

Write the picture description here

You can also use your own Sun and Google here.

Corrections and comments are welcome

CSDN address: blog.csdn.net/sinat_36668…