this is a non-work-related homework n00b dagger2 problem (please accord it only that much respect :)

69 views
Skip to first unread message

Eric Flatt

unread,
Apr 26, 2016, 12:54:33 PM4/26/16
to Dagger Discuss
I decided to kill a bunch birds w/ one stone and use java (which I never do) and dagger2 (which I never have) in some homework problem for a machine learning class I'm taking.  The problem I have here is lifted from the web as a toy example to simplify things down.  I basically have a Bar class that has a dependency on a BarDatabase (I forget where I stole the example from).

Problem is, I think I've done all the right steps (see below), but when push comes to shove in my MainActivity (doing this in android studio) and Dagger_BarComponent should be there to autocomplete or bind, it isn't.  The code isn't getting generated.  Please waste little time on this, but if something obvious sprung up, I wanted to ask.


1. Here's my bar module BarModule.java:

/**
* Created by eflatt on 4/26/16.
*/
@Module
public class BarModule {

@Provides @Singleton
BarDatabase provideBarDatabase () {
return new BarDatabase();
}

@Provides @Singleton
Bar provideBar () {
return new Bar(new BarDatabase());
}
}

2.  Here's me @Injecting the ctor in Bar.java:
public class Bar {
// Bar depends on BarDatabase
private BarDatabase db;

@Inject
public Bar(BarDatabase db) {
this.db = db;
}

public BarDatabase getBar() { return db; }

public Cocktail getCocktail(String name) {
String row = db.getCocktail(name);
return new Cocktail(row);
}
}
3.  Here's my BarComponent.java (where I define the component, naturally):

/**
* Created by eflatt on 4/26/16.
*/
@Singleton
@Component(modules = {BarModule.class})
public interface BarComponent {
Bar provideBar();
}
4.  But when I try to use it in my MainActivity.java:
BarComponent component = Dagger   // broken, it can't find the component, so no autocomplete, no dagger.

My build.gradle files look like this:
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'

// Assists in working with annotation processors for Android Studio.
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}

allprojects {
repositories {
jcenter()
}
}

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

and my app level build.gradle:
apply plugin: 'com.android.application'

// Assists in working with annotation processors for Android Studio.
apply plugin: 'com.neenbedankt.android-apt'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

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

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'

// Dagger 2 and Compiler
compile 'com.google.dagger:dagger:2.0.1'
apt "com.google.dagger:dagger-compiler:2.0.1"

// Needed for @Generated annotation (missing in Android API jar)
// No longer needed in dagger >= 2.1-SNAPSHOT (github.com/google/dagger/issues/95)
compile 'javax.annotation:jsr250-api:1.0'
}

David P. Baker

unread,
Apr 26, 2016, 1:11:26 PM4/26/16
to Dagger Discuss
I see some problems with your component, but they shouldn't lead to a build error.
  1. Bar has an @Inject constructor, but you also have a @Provides method that returns a Bar. In any component with that @Provides method, the @Inject constructor will be ignored. (A @Provides method takes precedence.)
  2. The @Provides method that returns a Bar calls new BarDatabase() explicitly, rather than letting Dagger call the constructor. If you want to use a @Provides method for Bar, it should take dependencies as parameters:

    @Provides static Bar provideBar(BarDatabase barDatabase) {
      return new Bar(barDatabase);
    }

    (Also, making @Provides methods static like this lets Dagger skip instantiating the module at all.)
I don't know Gradle, but I see you have some annotation processors configured there. Do you have to apply Dagger as well?

--
You received this message because you are subscribed to the Google Groups "Dagger Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dagger-discus...@googlegroups.com.
To post to this group, send email to dagger-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages