Android Nullaway框架,从此告别NPE

本文介绍了如何在Android项目中集成Uber的NullAway框架,通过配置Gradle文件,设置错误处理选项,以及指定要检查的包,帮助开发者有效防止NullPointerException。通过运行特定的Gradle命令,NullAway会高亮显示潜在的NPE问题,从而指导开发者使用注解或条件语句修复这些问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

    }
}


命令行测试结果如下


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值