2

我想使用 Checker Framework 对我的应用程序进行一些静态分析——在运行时检查 Nullability 失败、UI 约束失败等。我按照https://checkerframework.org/manual/#android-gradle 上的说明尝试让它运行,但我收到一个错误,即 checkTypes 不是任务。我认为我正确地遵循了说明,并且修复了他们的大小写不匹配问题。任何想法如何解决它?我的 build.gradle 如下:

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}"
            ]
        }
    }
}

编辑:对于未来的读者- javaCompileOptions.annotationProcessorOptions.classNames +=["org.checkerframework.checker.nullness.NullnessChecker"] 行似乎是问题所在。删除它会运行 dagger 和其他注释处理器,但显然不会运行检查器。

4

1 回答 1

2

因此,显然如果您通过类名指定 1 个注释处理器,它会覆盖任何其他注释处理器。所以我也必须指定 dagger 和 autofactory。然后我可以从 Android Studio 作为构建运行它,或者从命令行运行它:

gradlew assembleCheckTypes
于 2017-11-01T14:58:07.763 回答