Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates the process of checking Java code to spare humans of this boring (but important) task. This makes it ideal for projects that want to enforce a coding standard.
app/build.gradle
1 2 3 4 5 6 7 8 9 10 11 12 13 14
apply plugin:'checkstyle'
task checkstyle(type: Checkstyle) { description 'Check code standard' group 'verification'
configFile file('$project.rootDir/tools/checkstyle.xml') source 'src' include '**/*.kt' exclude '**/gen/**'
<!-- Checks for Naming Conventions --> <!-- See http://checkstyle.sourceforge.net/config_naming.html --> <modulename="MethodName"/> <modulename="ConstantName"/>
<!-- Checks for Imports --> <!-- See http://checkstyle.sourceforge.net/config_imports.html--> <modulename="AvoidStarImport"/> <modulename="UnusedImports"/>
<!-- Checks for Size --> <!-- See http://checkstyle.sourceforge.net/config_sizes --> <modulename="ParameterNumber"> <propertyname="max"value="6"/> </module>
<!-- other rules ignored for brevity --> </module> </module>
Android Studio provides a code scanning tool called lint that can help you to identify and correct problems with the structural quality of your code without your having to execute the app or write test cases
<!-- Disable the given check in this project --> <issueid="IconMissingDensityFolder"severity="ignore" /> <!-- Change the severity of hardcoded strings to "error" --> <issueid="HardcodedText"severity="error" /> </lint>
To make your app as small as possible, you should enable shrinking in your release build to remove unused code and resources. When enabling shrinking, you also benefit from obfuscation, which shortens the names of your app’s classes and members, and optimization, which applies more aggressive strategies to further reduce the size of your app
When you use Android Studio 3.4 or Android Gradle plugin 3.4.0 and higher, R8 is the default compiler that converts your project’s Java bytecode into the DEX format that runs on the Android platform
# Remove logs -assumenosideeffects class android.util.Log { public static boolean isLoggable(java.lang.String, int); public static int v(...); public static int i(...); public static int w(...); public static int d(...); public static int e(...); }
You don’t need to use ktlint or detekt to ensure that your code is formatted consistently. Simply enable “File is not formatted according to project settings” in the inspection settings.
dependencies { ktlint "com.pinterest:ktlint:0.32.0" // additional 3rd party ruleset(s) can be specified here // just add them to the classpath (e.g. ktlint 'groupId:artifactId:version') and // ktlint will pick them up }
The intention of a whitelist is that only new code smells are printed on further analysis. The blacklist can be used to write down false positive detections (instead of suppressing them and polute your code base).