Fix import order disagreement (kotlinx)

For checkstyle, 'kotlinx' is not specified, meaning it is expected
at the end, in alphabetical order with all other imports which lack
a defined ordering.

This disagrees with google-java-formatter and all IDE code formatter
configurations in use. This leads to confusion, manual correction or
in many cases ignored checkstyle warnings. This is in part due to
unhelpful checkstyle output:

Given:

    import kotlin.Pair;

    import kotlinx.coroutines.CoroutineDispatcher;

    import java.util.ArrayList;
    import java.util.Arrays;

Checkstyle reports:

    .../Foo.java:16:1: Wrong order for java.util.ArrayList import. Use
    Ctrl+Shift+O (Eclipse) or Ctrl+Alt+O (Intellij) to sort imports.
    https://source.android.com/setup/code-style#order-import-statements

Bug: 333533940
Test: manual
Change-Id: Ife87e1f92bdc620031cf2e90add6b688aef8b5ef
1 file changed
tree: 155230ea8546c495dc76833b84e04962c698270b
  1. gitlint/
  2. .gitignore
  3. android-style.xml
  4. checkstyle.jar
  5. checkstyle.py
  6. default-copyright-check.xml
  7. default-javadoc-checks.xml
  8. default-module-checks.xml
  9. default-treewalker-checks.xml
  10. LICENSE
  11. OWNERS
  12. PREUPLOAD.cfg
  13. README.md
  14. tests.py
README.md

Checkstyle

Checkstyle is used by developers to validate Java code style and formatting, and can be run as part of the pre-upload hooks.

Running it

It can be invoked in two ways.

  1. To check style of entire specific files: checkstyle.py -f FILE [FILE ...]
  2. To check style of the lines modified in the latest commit: checkstyle.py

Projects used

Checkstyle

A development tool to help programmers write Java code that adheres to a coding standard.

Git-Lint

Git-lint is a tool to run lint checks on only files changed in the latest commit.

  • URL: https://github.com/sk-/git-lint/
  • Version: 0.0.8
  • License: Apache 2.0
  • License File: gitlint/LICENSE
  • Local Modifications:
    • Downloaded gitlint/git.py and git/utils.py files individually.

Pre-upload linting

To run checkstyle as part of the pre-upload hooks, add the following line to your PREUPLOAD.cfg:

checkstyle_hook = ${REPO_ROOT}/prebuilts/checkstyle/checkstyle.py --sha ${PREUPLOAD_COMMIT}

Note that checkstyle does not always agree with clang-format, and so it's best to only have one enabled for Java.

Disabling Clang Format for Java

In .clang-format add the following to disable format checking and correcting for Java:

---
Language: Java
DisableFormat: true
SortIncludes: false
---

In some versions of clang-format, DisableFormat doesn't stop the sorting of includes. So to fully disable clang-format from doing anything for Java files, both options are needed.