1. 程式人生 > >Android Nullaway框架,從此告別NPE

Android Nullaway框架,從此告別NPE

https://github.com/uber/NullAway

project gradle下

3.0以前,repositories標籤下

新增maven { url 'https://maven.google.com' }

3.0以後新增google()

同時repositories標籤下還要新增

maven {url "https://plugins.gradle.org/m2/"}

plugins {
  id "net.ltgt.errorprone" version "0.0.13"

}

dependencies {
        classpath deps.build.gradlePlugins.android//這個可能不用
        classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.0.13"
}

module gradle

apply plugin: 'com.android.application'

apply plugin: 'net.ltgt.errorprone'

android下

compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }


    lintOptions {
        abortOnError false
    }

dependencies下

errorprone "com.google.errorprone:error_prone_core:2.1.3"

annotationProcessor "com.uber.nullaway:nullaway:0.4.6"

tasks.withType(JavaCompile) {
  // remove the if condition if you want to run NullAway on test code
  if (!name.toLowerCase().contains("test")) {
    options.compilerArgs += ["-Xep:NullAway:ERROR", "-XepOpt:NullAway:AnnotatedPackages=com.uber"]
  }

}

紅字的地方,是你要檢索NPE的包,這是你需要修改的地方。

在sync now應用gardle後,你需要在terminal中

./gradlew build

這樣你所有NPE的地方就會顯示出來了

對於你可能有空指標的地方,希望你能用@Nullable、@NotNull、if語句進行修復

////////////////////

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    
    repositories {
        google()
        jcenter()

        maven { url 'https://maven.google.com' }
        maven {url "https://plugins.gradle.org/m2/"}
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
// NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
//        classpath deps.build.gradlePlugins.android
classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.0.13"}
}

plugins {
    id "net.ltgt.errorprone" version "0.0.13"
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
apply plugin: 'com.android.application'
apply plugin: 'net.ltgt.errorprone'android {
    compileSdkVersion 26
defaultConfig {
        applicationId "com.example.lib"
minSdkVersion 21
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
javaCompileOptions {
            annotationProcessorOptions {
                arguments = [ eventBusIndex : 'org.greenrobot.eventbusperf.MyEventBusIndex' ]
            }
        }

    }
    buildTypes {
        release {
            minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    lintOptions {
        abortOnError false
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
api 'com.android.support:recyclerview-v7:26.1.0'
api 'com.squareup.okhttp3:okhttp:3.8.0'
api 'com.squareup.okio:okio:1.13.0'
//    implementation 'com.github.bumptech.glide:glide:4.3.1'
api 'org.greenrobot:eventbus:3.1.1'
annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.0.1'
errorprone "com.google.errorprone:error_prone_core:2.1.3"
    annotationProcessor "com.uber.nullaway:nullaway:0.4.6"

    tasks.withType(JavaCompile) {
        // remove the if condition if you want to run NullAway on test code
        if (!name.toLowerCase().contains("test")) {
            options.compilerArgs += ["-Xep:NullAway:ERROR", "-XepOpt:NullAway:AnnotatedPackages=com.example"]
        }

    }
}

命令列測試結果如下