Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger 2 components not generated when using Jack

When I enable the Jack compiler in Android Studio 2.2 the Dagger 2 component is not generated. Can Dagger 2 be used with Jack? If so, how would I go about configuring my application?

From my application's build.gradle:

jackOptions {
     enabled true
   }

 compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
like image 315
Mario Dennis Avatar asked Nov 30 '16 19:11

Mario Dennis


2 Answers

I sunk like 2 days into figuring this out. So I'm circling back to posting the findings here in case it saves someone time:

This is caused by a bug in Jack that prevents classpaths from working properly. It has to do with Jack running "in-process" (in the same JVM as the gradle daemon). Setting android.defaultConfig.jackOptions.jackInProcess to false does get beyond the "Preconditions" error but it causes other problems (2 JVMs that hog system resources) & bugs that break the build in other (worse) ways.

For now, the best solution seems to be:

  • Wait for the 2.3 release of the Android gradle plugin, which already has the fix for this.
  • Downgrade Dagger to v2.2, in the meantime.
    It's the highest version that seems to avoid the Guava conflict with Jack.

EDIT: update 1/14/2017:
I ran into several OTHER problems with Jack and got so tired of it that I switched to retrolambda and kicked myself for not doing this earlier! Right now, Jack simply seems to cause more problems than it solves. Just add the lines with a plus and delete the lines with a minus and you can return to Dagger 2.8 while waiting for Jack to get it's act together!

+plugins {
+    id "me.tatarka.retrolambda" version "3.4.0"
+}

 apply plugin: 'com.android.application'
+apply plugin: 'me.tatarka.retrolambda'

-        jackOptions {
-            enabled true
-        }

For even faster retrolambda builds, add org.gradle.jvmargs=-Xmx4608M to your gradle.properties file so that dexing can happen in-process. Now, I'm on Dagger 2.8 and my clean builds are only 12 seconds, GOOD RIDDANCE, JACK!

like image 106
gMale Avatar answered Sep 29 '22 13:09

gMale


The documentation page on Jack and Jill has instructions specific to annotation processors "to be applied at compile time but not to be included in your APK", advising the use of the annotationProcessor dependency scope. The example coincidentally mentions Dagger 2:

dependencies {
    compile 'com.google.dagger:dagger:2.0'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.0'
}
like image 24
Jeff Bowman Avatar answered Sep 29 '22 12:09

Jeff Bowman