Gabe Sechan
Gabe Sechan

Reputation: 93559

Running Checker Framework on Android

I want to use Checker Framework to do some static analysis of my app- checking for Nullability failures, UI constraint failures, etc at runtime. I followed the instructions at https://checkerframework.org/manual/#android-gradle to try and make it run, but I'm getting an error that checkTypes is not a task. I think I followed the instructions correctly, and I fixed the capitalization mismatch they had. ANy ideas how to fix it? My build.gradle is below:

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
    google()
}

apply plugin: 'jacoco'
apply plugin: 'com.getkeepsafe.dexcount'

gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xmaxerrs" << "500"
    }
}

android {
    compileSdkVersion 25
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "com.handshake.hsdm"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 35
        versionName "0.0.35"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            testCoverageEnabled = false
        }
        debug {
            testCoverageEnabled = false
        }
        checkTypes {
            javaCompileOptions.annotationProcessorOptions.
                    classNames.add("org.checkerframework.checker.guieffect.GuiEffectChecker")
            // You can pass options like so:
            // javaCompileOptions.annotationProcessorOptions.arguments.put("warns", "")
        }

    }
    dexOptions {
        preDexLibraries = false
    }
    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
        }
    }
    packagingOptions {
        //These files constantly step on each other from multiple libraries, don't include them
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/*'
        exclude 'plugin.xml'
        exclude 'plugin.properties'
        exclude 'about_files/LICENSE-2.0.txt'
    }
    testOptions {
        unitTests.all {
            jacoco {
                includeNoLocationClasses = true
            }
        }
    }
    jacoco {
        version = '0.7.3.201502191951'
    }
    configurations.all {
        resolutionStrategy.force 'com.google.code.findbugs:jsr305:2.0.1'
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

}
configurations {
    checkerFrameworkAnnotatedJDK {
        description = 'a copy of JDK classes with Checker Framework type qualifers inserted'
    }
}

task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {

    reports {
        xml.enabled = true
        html.enabled = true
    }

    def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*',
                      'com/handshake/hsdm/demo/**','com/handshake/hsdm/localstorage/schema1/*Dao*',
                      'com/handshake/hsdm/localstorage/schema1/*Factory*', 'com/handshake/hsdm/dagger2',
                      '**/*Module*', '**/*_Factory*', '**/*_MembersInjector*'
    ]
    def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
    def mainSrc = "${project.projectDir}/src/main/java"

    sourceDirectories = files([mainSrc])
    classDirectories = files([debugTree])
    executionData = fileTree(dir: "$buildDir", includes: [
            "jacoco/testDebugUnitTest.exec",
            "outputs/code-coverage/connected/*coverage.ec"
    ])

    afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: it, exclude: ['com/handshake/hsdm/demo/**'])
        })
    }
}
dependencies {
//Clipping dependencies 

    ext.checkerFrameworkVersion = '2.2.1'
    implementation "org.checkerframework:checker-qual:${checkerFrameworkVersion}"
    annotationProcessor "org.checkerframework:checker:${checkerFrameworkVersion}"
    checkerFrameworkAnnotatedJDK "org.checkerframework:jdk8:${checkerFrameworkVersion}"

}
apply plugin: 'com.google.gms.google-services'

gradle.projectsEvaluated {
    tasks.withType(JavaCompile).all { compile ->
        if (compile.name.contains("checkTypes")) {
            compile.options.compilerArgs += [
                    "-Xbootclasspath/p:${configurations.checkerFrameworkAnnotatedJDK.asPath}"
            ]
        }
    }
}

Edit: For future readers- the line javaCompileOptions.annotationProcessorOptions.classNames +=["org.checkerframework.checker.nullness.NullnessChecker"] seems to be the problem. removing it runs dagger and other annotation processors, but obviously won't run the checker.

Upvotes: 2

Views: 1650

Answers (2)

Eldo Martadjaya
Eldo Martadjaya

Reputation: 61

the working Checker Framework library is now, to be put in build.gradle (app level):

implementation 'org.checkerframework:checker-qual:3.48.2'

Refer to the library documentation.

Upvotes: 0

Gabe Sechan
Gabe Sechan

Reputation: 93559

So apparently if you specify 1 annotation processor via classnames, it overwrites any others. So I had to specify dagger and autofactory as well. Then I can either run it from Android Studio as a build, or from command line as:

gradlew assembleCheckTypes

Upvotes: 2

Related Questions