Dagger 2 and android Studio: working but can't see generated classes
Solution 1
Use the Android-Apt plugin by Hugo Visser:
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
buildscript {
repositories {
mavenCentral()
}
dependencies {
// the latest version of the android-apt plugin
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}
android {
compileSdkVersion 22
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.mateuyabar.android.dagger2test"
minSdkVersion 22
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false // ignoring some references from dagger-compiler
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'javax.inject:javax.inject:1'
compile 'javax.annotation:javax.annotation-api:1.2'
compile 'com.google.dagger:dagger:2.0'
apt 'com.google.dagger:dagger-compiler:2.0'
provided 'org.glassfish:javax.annotation:10.0-b28'
}
Note the apt 'com.google.dagger:dagger-compiler:2.0'
line as well. This should make the generated sources visible for AS.
Solution 2
I stumbled upon same problem, in my case I was testing new classes and tried to inject them separately into my main class, in that case Dagger won't generate any DaggerComponents from factory.
Simply, remember to put all injections that goes into one class in one Module & one Component. You cannot inject more than one DaggerComponent into one class. Besides that's the point of those components, to aggregate injected classes
Related videos on Youtube
Mateu
Updated on June 04, 2022Comments
-
Mateu almost 2 years
I'm trying to use Dagger 2 in an Android Studio Project. I've used the CoffeeMaker example. I've managed to make the app build and working however: - I don't success in seeing the generated code. - If I debug, I can't see it neither. - Moreover DaggerCoffeeApp_Coffee as marked as reed (Cannot resolve symbol)
My gradle files are:
// 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:1.1.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() } }
and
apply plugin: 'com.android.application' android { compileSdkVersion 22 buildToolsVersion "21.1.2" defaultConfig { applicationId "com.mateuyabar.android.dagger2test" minSdkVersion 22 targetSdkVersion 22 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } lintOptions { abortOnError false // ignoring some references from dagger-compiler } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.0.0' compile 'javax.inject:javax.inject:1' compile 'javax.annotation:javax.annotation-api:1.2' compile 'com.google.dagger:dagger:2.0' provided 'com.google.dagger:dagger-compiler:2.0' provided 'org.glassfish:javax.annotation:10.0-b28' }
Thanks
-
Jared Burrows about 9 yearsIf it builds and works why concern yourself with finding the generated files from
Dagger
? -
Mateu about 9 yearsFirst of all, learn and understand. Second one debugging.
-
Jared Burrows about 9 yearsWhere did you get the this example?
-
Mateu about 9 years@JaredBurrows Example is the one in Dagger2 site. Its not for Android but I've just copied the classes to an Android project.
-
Jared Burrows about 9 yearsIf their example did not work, I'd suggest you make a pull request to fix their example.
-
Mateu about 9 years@JaredBurrows already accepted a solution.
-
Jared Burrows about 9 yearsYes. That is why I said if their example does not work you should make a pull request to fix their example on their Github project page.
-
Mateu about 9 years@JaredBurrows Example uses pow.xml, not gradle
-
-
clocksmith almost 9 yearsI have of all of this, and still cannot import class:
DaggerMyComponent
. What else must you do to generate? -
nhaarman almost 9 yearsIf you've set it up correctly, it should work. Generated files can be found in
/build/generated/source/apt/
, so you can verify if generation has run. -
yuval over 8 yearsIf you have multiple gradle files, try specifying the classpath of the dependency in your most top-level file. For me, I have
project/build.gradle
andproject/app/build.gradle
. I applied the plugin inapp
but declared the dependency inproject
and it worked for me. -
Robert Estivill about 7 yearsNo need for apt. Android Gradle plugin now ships with support for annotationProcessor dependencies like dagger compiler