匕首不生成组件类

时间:2016-05-09 17:44:53

标签: java android dependency-injection dagger-2

我已经尝试了很多调试这个问题,但无法找到原因。 Dagger根本就没有创建DaggerComponent类。我已经检查过SO的重复项,但没有提供任何解决方案。

项目的build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
        classpath 'me.tatarka:gradle-retrolambda:3.2.3'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath 'me.tatarka:gradle-retrolambda:3.0.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        jcenter {
            url "http://jcenter.bintray.com"
        }
        mavenCentral()
        maven {
            url "https://oss.sonatype.org/content/repositories/snapshots"
        }

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

app的build.gradle

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'me.tatarka.retrolambda'


android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    defaultConfig {
        applicationId "com.hr.crux"
        minSdkVersion 18
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {

        }
        release {
            minifyEnabled false

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

retrolambda {
    jvmArgs '-noverify'
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'com.squareup.retrofit2:retrofit:2.0.2'
    provided 'javax.annotation:jsr250-api:1.0'
    apt 'com.google.dagger:dagger-compiler:2.2'
    compile 'com.google.dagger:dagger:2.2'
    provided 'javax.annotation:jsr250-api:1.0'
    testCompile 'junit:junit:4.12'

}

HttpModule.java

@Module
public class HttpModule {
    @Provides
    @Singleton
    Retrofit getRetrofit() {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://maps.googleapis.com/maps/api/place/")
                .build();
        return retrofit;

    }
}

HttpComponent.java

@Singleton
@Component(modules = {HttpModule.class})
public interface HttpComponent {

    void inject(MainActivity activity);

}

Application.java

public class Application extends android.app.Application {

private static Application application;

private HttpComponent appComponent;

@Override
public void onCreate() {
    super.onCreate();
    application = this;
    appComponent = //Cannot find DaggerHttpComponent
}


public static Application getInstance() {
    return application;
}
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Inject
Retrofit retrofit;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
  }
}

Dagger无法在我的Application类中生成组件类。我尝试过干净的建筑,我尝试过无效缓存,但没有任何作用。

1 个答案:

答案 0 :(得分:0)

您可以自己开始一个新项目,而不是遵循教程项目。如果您这样做,这是解决方案。

这两行负责在Dagger 2中生成编译时框架。

function getNestedChildren(arr, parent) { var out = [] for(var i in arr) { if(arr[i].parent == parent) { var children = getNestedChildren(arr, arr[i].id) if(children.length) { arr[i].children = children } out.push(arr[i]) } } return out } //在编译时生成框架compile 'com.google.dagger:dagger:2.14.1' //根据您提供的注释在编译时生成框架。

完全设置Dagger


 annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1'

注意:您需要按照以下屏幕截图中的说明配置注释流程。您可以执行此操作 //dagger 2 compile 'com.google.dagger:dagger:2.14.1' annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1' //to enable DaggerActivity, DaggerBroadcastReceiver, DaggerFragment etc classes compile 'com.google.dagger:dagger-android:2.14.1' annotationProcessor 'com.google.dagger:dagger-android-processor:2.14.1' //support libraries with dagger 2 compile 'com.google.dagger:dagger-android-support:2.14.1' 搜索File>Other Settings>Default Settings> enter image description here