Dagger2 未生成匕首* 类

Dagger2 Not Generating Dagger* classes

正如标题所说,Dagger2 不会生成前缀为 classes 的 Dagger*。我在这里查看了其他一些类似的帖子,但似乎没有任何效果。

我克隆了这个 repo https://github.com/ecgreb/mvpc,使 Android Studio 的缓存无效并重新启动它,我删除了 $Project/.gradle$Home/.gradle/caches,清理并重建项目,但仍然不工作。

这也发生在一些使用 Dagger2 的项目上

我是不是漏掉了什么?

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

android {
  compileSdkVersion 24
  buildToolsVersion "24.0.3"

  defaultConfig {
    applicationId "com.example.ecgreb.mvpc"
    minSdkVersion 15
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
  }
  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }
}

tasks.withType(Test) {
  testLogging {
    exceptionFormat "full"
    events "started", "skipped", "passed", "failed"
    showStandardStreams true
  }
}

repositories {
  mavenCentral()
}

dependencies {
  compile 'com.android.support:appcompat-v7:24.2.1'
  compile 'com.android.support:design:24.2.1'

  compile "com.google.dagger:dagger:2.7"
  annotationProcessor "com.google.dagger:dagger-compiler:2.7"
  provided 'javax.annotation:jsr250-api:1.0'

  testCompile 'junit:junit:4.12'
  testCompile 'org.mockito:mockito-core:1.10.19'
  testCompile 'org.assertj:assertj-core:1.7.1'
  testCompile 'org.robolectric:robolectric:3.1.2'
  testCompile 'org.khronos:opengl-api:gl1.1-android-2.1_r1'
}

应用程序class。

package com.example.ecgreb.mvpc;

import android.app.Application;

import com.example.ecgreb.mvpc.controller.LoginActivity;

import javax.inject.Singleton;

import dagger.Component;
public class MvpcApplication extends Application {

  @Singleton @Component(modules = { LoginModule.class }) public interface ApplicationComponent {
    void inject(LoginActivity loginActivity);
  }

  private ApplicationComponent component;

  @Override public void onCreate() {
    super.onCreate();
    //DaggerApplicationComponent IS NOT BEING GENERATED
    component = DaggerApplicationComponent.builder().build();
  }

  public ApplicationComponent component() {
    return component;
  }
}

如果你使用

apply plugin: 'com.neenbedankt.android-apt'

然后代替

 annotationProcessor "com.google.dagger:dagger-compiler:2.7"

 apt "com.google.dagger:dagger-compiler:2.7"

Android-apt 虽然不会在 AS 3.0 中工作,所以你需要 annotationProcessor 代替每个 apt.


如果您使用 Kotlin,则必须将 annotationProcessor 替换为 kapt,并将 apply plugin: 'kotlin-kapt' 添加到您的 build.gradle 文件

对于在项目中使用 Jack Compiler 时遇到此类问题的任何人,您可能已经看到 none 的解决方案有效,原因与 Dagger2 本身有关。最新版本不能很好地与 Jack 配合使用,我设法让它工作到 Dagger2 的 2.2 版本。