4

I'm having trouble to use errorprone and nullaway in android studio. Looks like nullaway depends on errorprone. These are static analysis tools to avoid NPE. The documentation on how to use nullaway in android on the nullaway github page is very poor and not clear.

I've tried going to plugins area in android studio and i installed the errorprone plugin, then switched the java compiler to javac with error prone like this image:

enter image description here

Then i put the following code in android studio thinking that the compiler would catch the potential null:

private void processLog() {     
     log(null);
}

static void log(Object x) {
    System.out.println(x.toString());
}
static void foo() {
    log(null);
}

instead nothing happened. Here is what i put in gradle files;

Bottom level build.gradle:

dependencies {
    annotationProcessor "com.uber.nullaway:nullaway:0.1.6"
}

Top level build.gradle file:

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
}
Emre Aktürk
  • 3,306
  • 2
  • 18
  • 30
j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • Hey, NullAway maintainer here. Hopefully our docs have gotten a bit better since you last looked. Are you still having an issue? If you can push up sample code to a GitHub project I'm happy to take a quick look. – msridhar Nov 10 '17 at 05:28
  • i've exhausted trying to follow the readme. do you guys have a simple doc how to set it up ? i wish the readme did not have so much complexity. just steps 1 to x. – j2emanue Dec 17 '17 at 05:58
  • I've put a self-contained Gradle build file for an Android app with NullAway [here](https://gist.github.com/msridhar/6cacd429567f1d1ad9a278e06809601c), based on our [Android sample app](https://github.com/uber/NullAway/tree/master/sample-app). You'll have to tweak the `"-XepOpt:NullAway:AnnotatedPackages=com.uber"` option to refer to your own packages, and the Android SDK / target versions as needed. If you are still stuck, please open an issue on the NullAway issue tracker and we can iterate there. Once we figure this out I'll try to make the README easier to follow. – msridhar Dec 18 '17 at 17:31
  • @msridhar i easily understood the standalone version you posted and nullaway is now running each time i compile my application. should you make an accepted answer or i can. i do wish there was a way to not abort the build if an error was found. should just warn but not stop build. but thanks for your help – j2emanue Dec 23 '17 at 11:34
  • Cool! I posted an answer, including how to have NullAway just warn and not block the build. – msridhar Dec 23 '17 at 18:16

2 Answers2

0

No need of installing error-prone plugin from android studio.

As described in the documentation, follow these two steps :

1) Add repository url and classpath in top level build file :

repositories {
    // other repositories
    maven { url "https://plugins.gradle.org/m2/" }
 }
dependencies {
    classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.0.13"
}

2) Add annotation processor for null-away and apply error-prone plugin in module level gradle file (Second part is what you are missing. You are not applying the plugin to your module) :

````

apply plugin: 'net.ltgt.errorprone'

dependencies {
    annotationProcessor "com.uber.nullaway:nullaway:0.1.6"
} 

```

Yash
  • 5,225
  • 4
  • 32
  • 65
  • i tried what you said but still nullaway will not show me any errors in the code i posted to try nullaway: private void processLog() { log(null); } static void log(Object x) { System.out.println(x.toString()); } static void foo() { log(null); } it seems nullaway does not work. did you try it ? – j2emanue Oct 24 '17 at 10:01
  • yes, I've tried. it works for me. Do you have your code hosted somewhere so that I can have a look? – Yash Oct 24 '17 at 13:03
  • Also, I'd suggest you to clone their repository and run their sample project. – Yash Oct 24 '17 at 13:04
  • Telle.how you ran it. I did ./gradlew assembleProdDebug ....on the command line. Is that the way ? Prod being one of my build flavors – j2emanue Oct 24 '17 at 16:34
  • yes, it should invoke nullaway when you run this task. Can you edit the question with updated gradle files? – Yash Oct 24 '17 at 17:03
  • Maybe `errorprone "com.google.errorprone:error_prone_core:2.1.1"` missing from dependencies is causing an issue? Not sure what Gradle EP plugin defaults to. – msridhar Nov 10 '17 at 18:57
0

I've put a self-contained Gradle build file for an Android app with NullAway here, based on our Android sample app. You'll have to tweak the "-XepOpt:NullAway:AnnotatedPackages=com.uber" option to refer to your own packages, and the Android SDK / target versions as needed. Also, if you just want NullAway to emit warnings but not block the build, omit the "-Xep:NullAway:ERROR" option.

msridhar
  • 2,846
  • 4
  • 22
  • 23