我无法在 android studio中使用errorprone和nullaway 。看起来 nullaway 取决于容易出错。这些是避免 NPE 的静态分析工具。nullaway github页面上关于如何在android中使用nullaway的文档很差而且不清楚。
我试过去 android studio 中的插件区域并安装了容易出错的插件,然后将 java 编译器切换到 javac 容易出错,如下图所示:
然后我将以下代码放在 android studio 中,认为编译器会捕获潜在的 null:
private void processLog() {
log(null);
}
static void log(Object x) {
System.out.println(x.toString());
}
static void foo() {
log(null);
}
相反,什么也没发生。这是我放入 gradle 文件的内容;
底层build.gradle:
dependencies {
annotationProcessor "com.uber.nullaway:nullaway:0.1.6"
}
顶级build.gradle 文件:
buildscript {
repositories {
jcenter()
maven { url 'https://maven.fabric.io/public' }
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.google.gms:google-services:3.1.1'
classpath 'io.fabric.tools:gradle:1.24.+'
classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.0.11"
classpath "net.ltgt.gradle:gradle-apt-plugin:0.11"
}
}
plugins {
id "net.ltgt.errorprone" version "0.0.13"
}
allprojects {
repositories {
jcenter()
mavenCentral()
maven { url "https://maven.google.com" }
maven { url "http://dl.bintray.com/pt-midtrans/maven" }
maven { url "https://jitpack.io" }
maven { url "https://plugins.gradle.org/m2/" }
}
}
subprojects {
project.configurations.all {
resolutionStrategy.eachDependency { details ->
if (details.requested.group == 'com.android.support'
&& !details.requested.name.contains('multidex')) {
details.useVersion "$supportlib_version"
}
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}