Dagger 2没有生成我的组件类

时间:2016-03-17 02:59:23

标签: android android-gradle build.gradle dagger-2

我正在与Dagger 2合作创建我的依赖注入。它在几个小时前工作,但现在组件不再生成了。

以下是我创建组件的地方:

public class App extends Application {

    CacheComponent mCacheComponent;
    ApiComponent mApiComponent;
    DataBaseComponent mDataBaseComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        mCacheComponent = DaggerCacheComponent.builder()
                .cacheModule(new CacheModule(getApplicationContext()))
                .build();

        mApiComponent = DaggerApiComponent.builder()
                .apiModule(new ApiModule())
                .build();

        mDataBaseComponent = DaggerDataBaseComponent.builder()
                .dataBaseModule(new DataBaseModule(getApplicationContext()))
                .build();
    }

    public CacheComponent getCacheComponent(){ return mCacheComponent;}
    public ApiComponent getApiComponent() { return mApiComponent;  }
    public DataBaseComponent getDataBaseComponent() { return mDataBaseComponent;}
}

这是有效的,但现在我收到了错误:

E:\AndroidProject\JoinTV\app\src\main\java\com\myproject\jointv\utils\App.java:7: error: cannot find symbol
import com.myproject.jointv.component.DaggerApiComponent;
                                       ^       
  symbol:   class DaggerApiComponent           
  location: package com.myproject.jointv.component
E:\AndroidProject\JoinTV\app\src\main\java\com\myproject\jointv\utils\App.java:8: error: cannot find symbol
import com.myproject.jointv.component.DaggerCacheComponent;
                                       ^       
  symbol:   class DaggerCacheComponent         
  location: package com.myproject.jointv.component
E:\AndroidProject\JoinTV\app\src\main\java\com\myproject\jointv\utils\App.java:9: error: cannot find symbol
import com.myproject.jointv.component.DaggerDataBaseComponent;
                                       ^       
  symbol:   class DaggerDataBaseComponent      
  location: package com.myproject.jointv.component
Note: Resolve log file to E:\AndroidProject\JoinTV\app\build\generated\source\apt\androidannotations.log
Note: Initialize AndroidAnnotations 3.3.2 with options {androidManifestFile=E:\AndroidProject\JoinTV\app\build\intermediates\manifests\full\debug\AndroidManifest.xml, resourcePackage
Name=com.myproject.jointv}
Note: Start processing for 5 annotations on 32 elements
Note: AndroidManifest.xml file found with specified path: E:\AndroidProject\JoinTV\app\build\intermediates\manifests\full\debug\AndroidManifest.xml
Note: AndroidManifest.xml found: AndroidManifest [applicationPackage=com.myproject.jointv, componentQualifiedNames=[com.myproject.jointv.activities.MainActivity_, com.myproject
.jointv.activities.YoutubePlayerViewActivity, com.myproject.jointv.activities.VideoPlayerActivity_, com.myproject.jointv.services.ServiceDownloaderPlayList], permissionQualifiedN
ames=[android.permission.INTERNET, android.permission.WRITE_EXTERNAL_STORAGE], applicationClassName=com.myproject.jointv.utils.App, libraryProject=false, debugabble=false, minSdkVe
rsion=21, maxSdkVersion=-1, targetSdkVersion=23]
Note: Found project R class: com.myproject.jointv.R
Note: Found Android class: android.R           
Note: Validating elements                      
Note: Validating with EActivityHandler: [com.myproject.jointv.activities.MainActivity, com.myproject.jointv.activities.VideoPlayerActivity]
Note: Validating with EFragmentHandler: [com.myproject.jointv.fragments.VideoPlayerFragment]
Note: Validating with ViewByIdHandler: [buttonPlay, videoView, videoView]
Note: Validating with FragmentArgHandler: [url, cachePath]
Note: Validating with AfterViewsHandler: [onViewInjected(), afterViewInjected()]
Note: Processing root elements                 
Note: Processing root elements EActivityHandler: [com.myproject.jointv.activities.VideoPlayerActivity, com.myproject.jointv.activities.MainActivity]
Note: Processing root elements EFragmentHandler: [com.myproject.jointv.fragments.VideoPlayerFragment]
Note: Processing enclosed elements             
Note: Number of files generated by AndroidAnnotations: 3
Note: Writting following API classes in project: []
Note: Generating class: com.myproject.jointv.fragments.VideoPlayerFragment_
Note: Generating class: com.myproject.jointv.activities.MainActivity_
Note: Generating class: com.myproject.jointv.activities.VideoPlayerActivity_
Note: Time measurements: [Whole Processing = 387 ms], [Extract Manifest = 131 ms], [Generate Sources = 97 ms], [Process Annotations = 59 ms], [Find R Classes = 43 ms], [Extract Annot
ations = 25 ms], [Validate Annotations = 18 ms],
Note: Finish processing                        
E:\AndroidProject\JoinTV\app\src\main\java\com\myproject\jointv\component\ApiComponent.java:22: error: com.myproject.jointv.database.DataBaseHelper cannot be provided without an
@Inject constructor or from an @Provides- or @Produces-annotated method.
    void inject(ServiceDownloaderPlayList service);
         ^                                     
      com.myproject.jointv.services.ServiceDownloaderPlayList.dataBaseHelper
          [injected field of type: com.myproject.jointv.database.DataBaseHelper dataBaseHelper]
E:\AndroidProject\JoinTV\app\src\main\java\com\myproject\jointv\component\DataBaseComponent.java:18: error: com.myproject.jointv.api.WherePlaysAPI cannot be provided without an @
Provides- or @Produces-annotated method.
    void inject(ServiceDownloaderPlayList service);
         ^                                     
      com.myproject.jointv.services.ServiceDownloaderPlayList.wherePlaysAPI
          [injected field of type: com.myproject.jointv.api.WherePlaysAPI wherePlaysAPI]
Note: Start processing for 0 annotations on 0 elements
Note: Time measurements: [Whole Processing = 1 ms],
Note: Finish processing                        
5 errors                                       
:app:compileDebugJavaWithJavac FAILED          

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:        
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED  

有我的gradle app。

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

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

我的gradle模块

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

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.myproject.jointv"
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        resourcePackageName android.defaultConfig.applicationId
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.android.support:recyclerview-v7:23.2.0'
    compile 'com.android.support:leanback-v17:23.2.0'
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.github.bumptech.glide:glide:3.4.+'
    compile 'com.squareup.okhttp:okhttp:2.5.0'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.5.0'
    compile 'com.android.support:support-v4:23.2.0'
    compile 'com.android.support:design:23.2.0'
    compile 'com.danikula:videocache:2.3.3'
    compile 'org.androidannotations:androidannotations-api:3.3.2'
    apt 'org.androidannotations:androidannotations:3.3.2'
    compile 'com.google.dagger:dagger:2.0.2'
    apt 'com.google.dagger:dagger-compiler:2.0.2'
    provided 'org.glassfish:javax.annotation:10.0-b28'

}

我整天使用相同的配置,但现在我不知道问题出在哪里以及为什么它不会生成组件类。

ApiComponent。

@Singleton
@Component(modules = {
        ApiModule.class,
        DataBaseModule.class
})

public interface ApiComponent {
    void inject(ServiceDownloaderPlayList service);
}

CacheComponent

@Singleton
@Component(modules = {
        CacheModule.class
})
public interface CacheComponent {
    void inject(VideoPlayerFragment fragment);
}

DataBaseComponent

@Singleton
@Component(modules = {
        DataBaseModule.class
})
public interface DataBaseComponent {
    void inject(ServiceDownloaderPlayList service);
}

提示

当我评论 DataBaseComponent ApiComponent 时,一切正常。

4 个答案:

答案 0 :(得分:1)

如果你使用apt插件如下

classpath' com.neenbedankt.gradle.plugins:android-apt:1.8' 申请插件:' com.neenbedankt.android-apt'

然后包括

apt" com.google.dagger:dagger-compiler:$ DAGGER_VERSION"

一起

编译" com.google.dagger:dagger:$ DAGGER_VERSION"

模块级build.gradle中的

除了配置问题之外,如果依赖模块或类中存在任何错误(最可能是语法错误),Dagger2也将无法生成Component类。

只需打开依赖类并修复问题(如果有的话)并重新构建项目。

某些无效缓存也有效:Android Studio-> file->无效缓存并重新启动

答案 1 :(得分:0)

对我来说,我必须构建/制作项目一次,才能创建组件

答案 2 :(得分:0)

它在您的堆栈跟踪错误中

E:\AndroidProject\JoinTV\app\src\main\java\com\myproject\jointv\component\ApiComponent.java:22: error: com.myproject.jointv.database.DataBaseHelper cannot be provided without an@Inject constructor or from an @Provides- or @Produces-annotated method.
void inject(ServiceDownloaderPlayList service);

在模块中提供DataBaseHelper

答案 3 :(得分:0)

绝对可以确定您已解决了该问题,但是我想评论一下Kotlin中相同问题的解决方案。
在Kotlin中,您必须通过添加以下内容来将annotationProcessor替换为kapt

apply plugin: 'kotlin-kapt'

...在您的 build.gradle (模块)中。
完成此操作后,可以通过以下方式将dagger正确添加到项目中:

implementation 'com.google.dagger: dagger: 2.x'
kapt 'com.google.dagger: dagger-compiler: 2.x'