Skip to content
bangarharshit edited this page Jan 21, 2018 · 3 revisions

Intro

Error Prone check to detect missing AutoDispose scope within defined scoped elements.

Installation

Here are sample configurations which pulls in both the ErrorProne and the AutoDispose check.

Gradle

  buildscript {
    repositories {
      maven {
        url "https://plugins.gradle.org/m2/"
      }
    }
  }
  
  plugins {
    // we assume you are already using the Java plugin
    id "net.ltgt.apt" version "0.13"
    id "net.ltgt.errorprone" version "0.0.13"
  }
  
  dependencies {
    apt "com.uber.autodispose:autodispose-error-prone-checker:x.y.z" // where x.y.z is the latest version.
  
    errorprone "com.google.errorprone:error_prone_core:2.1.3"
  }
  
  tasks.withType(JavaCompile) {
    // Only if you want to support custom configuration
    // Below is a sample configuration which include Conductor and Activity
    options.compilerArgs += ["-XepOpt:AutoDisposeLeakCheck"
                                         + "=com.bluelinelabs.conductor.Controller,android.app.Activity"]
  }

Maven

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5</version>
        <configuration>
          <compilerId>javac-with-errorprone</compilerId>
          <forceJavacCompilerUse>true</forceJavacCompilerUse>
          <source>1.8</source>
          <target>1.8</target>
          <showWarnings>true</showWarnings>
          <annotationProcessorPaths>
            <path>
               <groupId>com.uber.autodispose</groupId>
               <artifactId>autodispose-error-prone-checker</artifactId>
               <version>x.y.z</version>
            </path>
          </annotationProcessorPaths>
          <compilerArgs>
            <arg>--XepOpt:AutoDisposeLeakCheck=com.bluelinelabs.conductor.Controller,android.app.Activity</arg>
          </compilerArgs>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-compiler-javac-errorprone</artifactId>
            <version>2.8</version>
          </dependency>
          <!-- override plexus-compiler-javac-errorprone's dependency on
               Error Prone with the latest version -->
          <dependency>
            <groupId>com.google.errorprone</groupId>
            <artifactId>error_prone_core</artifactId>
            <version>2.1.3</version>
          </dependency>
        </dependencies>        
      </plugin>
  </build>

Code example

public static class ComponentWithLifecycle extends Activity {
  public void observeOnSomething() {
    Observable
        .interval(1, TimeUnit.SECONDS)
        .subscribe(new Consumer<Long>() {
          @Override public void accept(Long interval) throws Exception {
            System.out.println(interval);
          }
        });
  }
}
./gradlew build
error: [UseAutoDispose] Always apply an AutoDispose scope before subscribing within defined scoped elements.
        .subscribe(new Consumer<Long>() {
                  ^
    (see https://github.com/uber/AutoDispose/wiki/Error-Prone-Checker)

Command line flags

By default the checker is applied to standard android components with lifecycle and autodispose interfaces:

  1. Activity
  2. Fragment
  3. Support Fragment
  4. LifecycleScopeProvider
  5. ScopeProvider
  6. LifecycleOwner

It can be configured by Error Prone's Command-Line Flags.

The following flag is supported and takes input in a form of comma separated list:

  • -XepOpt:AutoDisposeLeakCheck=com.bluelinelabs.conductor.Controller,android.app.Activity

The check is now applied to Controller and Activity only.

Clone this wiki locally