Dagger 2 components not generated when using Jack

时光毁灭记忆、已成空白 提交于 2019-12-07 04:01:24

问题


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
}

回答1:


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!




回答2:


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'
}



回答3:


Jack is now deprecated, see this post.

You have to upgrade your Android Studio to 3.0 preview 1, to be able to use Java 8.

If you can't upgrade it (conflict with other lib), or you want to wait for a release version, you can try this workaround solution :

  • Add the retrolamba lib, follow instructions here.


来源:https://stackoverflow.com/questions/40897009/dagger-2-components-not-generated-when-using-jack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!