Skip to content

Instantly share code, notes, and snippets.

@Gazer
Last active January 6, 2016 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gazer/1386213de9f959cb2595 to your computer and use it in GitHub Desktop.
Save Gazer/1386213de9f959cb2595 to your computer and use it in GitHub Desktop.
Gradle tasks for code static code analysis on android apps.

Gradle Tasks for code static code analysis on android apps.

Credits to : https://github.com/artem-zinnatullin/qualitymatters

Usage :

  • Download all files on this GIST.
  • Put the XML files on your root directory under code_quality_tools/ folder.
  • Put the code_quality_tools.gradle file into your root project directory.
  • On each module you want to check add to the build.gradle the next line : apply from: '../code_quality_tools.gradle'
  • Run "gradlew check"
  • Fix your code :)
<?xml version="1.0"?><!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.2//EN"
"http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<module name="Checker">
<module name="FileTabCharacter" />
<module name="TreeWalker">
<module name="ConstantName" />
<module name="LocalFinalVariableName" />
<module name="LocalVariableName" />
<module name="MemberName" />
<module name="PackageName" />
<module name="ParameterName" />
<module name="AvoidStarImport" />
<module name="IllegalImport" />
<module name="RedundantImport" />
<module name="UnusedImports" />
<module name="LineLength">
<property name="max" value="180" />
</module>
<module name="MethodLength" />
<module name="GenericWhitespace" />
<module name="EmptyForIteratorPad" />
<module name="MethodParamPad" />
<module name="NoWhitespaceAfter" />
<module name="NoWhitespaceBefore" />
<module name="ParenPad" />
<module name="TypecastParenPad" />
<module name="WhitespaceAfter" />
<module name="RedundantModifier" />
<module name="LeftCurly" />
<module name="RightCurly" />
<module name="CovariantEquals" />
<module name="EmptyStatement" />
<module name="EqualsHashCode" />
<module name="IllegalInstantiation" />
<module name="RedundantThrows" />
<module name="HideUtilityClassConstructor" />
<module name="InterfaceIsType" />
<module name="ArrayTypeStyle" />
</module>
</module>
// Gradle Tasks for code static code analysis on android apps.
//
// Credits to : https://github.com/artem-zinnatullin/qualitymatters
//
// Usage :
// * Download all files on this GIST.
// * Put the XML files on your root directory under 'code_quality_tools/' folder.
// * Put the this file into your root project directory.
// * On each module you want to check add to the build.gradle the next line :
// apply from: '../code_quality_tools.gradle'
// * Run "gradlew check"
// * Fix your code :)
ext.preDexLibs = !project.hasProperty('disablePreDex')
project.plugins.whenPluginAdded { plugin ->
if ('com.android.build.gradle.AppPlugin'.equals(plugin.class.name) || 'com.android.build.gradle.LibraryPlugin'.equals(plugin.class.name)) {
// enable or disable pre-dexing
project.android.dexOptions.preDexLibraries = rootProject.ext.preDexLibs
}
}
plugins.apply('pmd')
pmd {
toolVersion = '5.4.0'
}
task pmd(type: Pmd) {
ignoreFailures = false // Fail early.
ruleSetFiles = project.files(rootProject.file("code_quality_tools/pmd.xml"))
ruleSets = []
source = fileTree('src/main/java')
}
plugins.apply('findbugs')
task findbugs(type: FindBugs) {
ignoreFailures = false // Fail early.
effort = 'max'
reportLevel = 'low' // Report even low priority problems.
classes = files("${project.projectDir}/build/intermediates/classes")
source = fileTree('src/main/java')
// If somebody has an idea how to make this work with support libraries -> open a PR please.
classpath = files()
excludeFilter = rootProject.file('code_quality_tools/findbugs-filter.xml')
}
plugins.apply('checkstyle')
task checkstyle(type: Checkstyle) {
configFile rootProject.file('code_quality_tools/checkstyle.xml')
ignoreFailures false // Fail early.
showViolations true
source 'src'
include '**/*.java'
classpath = files()
}
afterEvaluate {
tasks.findByName('pmd').dependsOn('assemble')
tasks.findByName('findbugs').dependsOn('assemble')
def checkTask = tasks.findByName('check')
checkTask.dependsOn('pmd')
checkTask.dependsOn('findbugs')
checkTask.dependsOn('checkstyle')
// Log instrumentation tests results.
// tasks.withType(AndroidTestTask) { task ->
// task.doFirst {
// logging.level = LogLevel.INFO
// }
// task.doLast {
// logging.level = LogLevel.LIFECYCLE
// }
// }
}
<FindBugsFilter>
<!-- Do not check auto-generated resources classes -->
<Match>
<Class name="~.*R\$.*"/>
</Match>
<!-- Do not check auto-generated manifest classes -->
<Match>
<Class name="~.*Manifest\$.*"/>
</Match>
<!-- Do not check auto-generated classes (Dagger puts $ into class names) -->
<Match>
<Class name="~.*Dagger*.*"/>
</Match>
<!-- Do not check for non-initialized fields in tests because usually we initialize them in @Before -->
<Match>
<Class name="~.*Test"/>
<Bug pattern="UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR" type="UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR"/>
</Match>
<!-- Ignore UPM in lambdas from Retrolambda, FindBugs does not correctly understand them -->
<Match>
<Bug code="UPM" />
<Class name="~.*\$\$Lambda\$.*"/>
</Match>
</FindBugsFilter>
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Android Application Rules"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>Custom ruleset for Android application</description>
<exclude-pattern>.*/R.java</exclude-pattern>
<exclude-pattern>.*/gen/.*</exclude-pattern>
<rule ref="rulesets/java/unnecessary.xml"/>
<rule ref="rulesets/java/imports.xml">
<exclude name="TooManyStaticImports"/>
</rule>
<rule ref="rulesets/java/unusedcode.xml"/>
<rule ref="rulesets/java/junit.xml"/>
<rule ref="rulesets/java/logging-java.xml"/>
<rule ref="rulesets/java/braces.xml"/>
<rule ref="rulesets/java/strings.xml"/>
<rule ref="rulesets/java/basic.xml"/>
<rule ref="rulesets/java/design.xml"/>
<rule ref="rulesets/java/typeresolution.xml"/>
</ruleset>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment