7

I am working with Dagger 2 to create my dependency injection. It was working a few hours ago but now the components aren't generated anymore.

Here is where I am creating the components:

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

This was working but now I get errors:

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  

There is my 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
}

And my gradle module

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'

}

I was using the same configuration all day, but now I don't know where the problem could be and why it doesn't generate the components class.

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);
}

Tips

When I comment the DataBaseComponent or ApiComponent everything work.

Robert
  • 10,403
  • 14
  • 67
  • 117
  • Your modules don't provide `WherePlaysAPI` and `DataBaseHelper` classes. – EpicPandaForce Mar 17 '16 at 10:30
  • You'd like to use them in `ServiceDownloaderPlayList`, but the component used to inject it doesn't have all the dependencies you'd like to provide. – EpicPandaForce Mar 17 '16 at 10:40
  • the both problems occurs because the project doesn't load DaggerApiComponent, if I removed then the error will be raise again but saying the same with other *DaggerComponentName*. If The DaggerComponentName in not loaded these kind of errors are raising. I update my question. – Robert Mar 17 '16 at 15:16
  • You cannot use field injection on a class unless your component is capable of providing all dependencies in the given scope using its modules. – EpicPandaForce Mar 17 '16 at 15:18
  • 1
    @EpicPandaForce that problem is solved when The components are loaded. I am trying to solve this problem first. I am going to try removing or not duplicating the ***ServiceDownloaderPlayList*** en different components. – Robert Mar 17 '16 at 15:50
  • You know that won't work, right? Components were initially designed so that there would be `one` component that would bind together multiple modules, and that one component would be able to provide ALL dependencies through field injection (if that's what you choose). – EpicPandaForce Mar 17 '16 at 16:03
  • I was going to comment on how the ServiceDownloaderPlayList tries to be injected via two different components that cannot satisfy its dependencies, but then I realized I already did that 7 months ago. Well then. – EpicPandaForce Oct 14 '16 at 07:03

4 Answers4

2

if you are using apt plugin as following

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' apply plugin: 'com.neenbedankt.android-apt'

then include

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

along with

compile "com.google.dagger:dagger:$DAGGER_VERSION"

in module level build.gradle

Other than config issue Dagger2 will also fail to generate the Component classes if there is any error (most probably syntax error) in the dependent module or classes.

just open the dependent classes and fix the issue if any and re build the project .

Some invalidating cache also works : Android Studio->file->invalidate cache and Restart

44kksharma
  • 2,740
  • 27
  • 32
  • First you should check if `apt "com.google.dagger:dagger-compiler` and `compile "com.google.dagger:dagger` share the same dagger version. – Logan Guo May 04 '17 at 02:36
0

It's in your stacktrace error

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);

Provide DataBaseHelper in a module.

Cătălin Florescu
  • 5,012
  • 1
  • 25
  • 36
0

It is more than sure that you solved the problem, however I would like to comment on the solution of the same problem in Kotlin.
In Kotlin you must replace the annotationProcessor with kapt by adding:

apply plugin: 'kotlin-kapt'

...in your build.gradle (module).
Once you have done this, you can correctly add dagger to your project by:

implementation 'com.google.dagger: dagger: 2.x'
kapt 'com.google.dagger: dagger-compiler: 2.x'
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Alejandro Fort
  • 545
  • 4
  • 9
0

If it's related to your IDE, this answer could help:

Pref -> Editor -> File Types -> Ignore Files And Folders -> Remove "Dagger*.java;"

Alireza Akbari
  • 2,153
  • 2
  • 28
  • 53