0

I'm unable to find a solution to bring an Android Studio Library with Dagger 2 to run.

I will explain what happened:

  1. I have a running Android Studio Project that using Dagger 2
  2. I have created a module from the app (bevor I used refactor for renaming) from this Project
  3. In another Project I have imported this module as a new module
  4. In fact: Now I have a Project with 3 library modules and my app (application) module
  5. I try to build and get this error

Errors:

Error:(7, 46) error: cannot find symbol class DaggerApplicationComponent
Error:(8, 46) error: cannot find symbol class DaggerHomeActivityComponent
Error:(21, 46) error: cannot find symbol class DaggerProfileComponent
Error:(27, 46) error: cannot find symbol class DaggerSearchComponent
Error:(27, 46) error: cannot find symbol class DaggerShareFeedComponent
Error:(312, 19) error: element value must be a constant expression
Error:(314, 19) error: element value must be a constant expression
.
.
.

and so on... This is the java file snippet with red underlined:

import android.app.Application;
import android.content.Context;

import com.aroniez.materialsnap.di.components.ApplicationComponent;
import com.aroniez.materialsnap.di.components.DaggerApplicationComponent;
import com.aroniez.materialsnap.di.modules.ApplicationModule;

import net.danlew.android.joda.JodaTimeAndroid;

Here is my build.gradle from the module (Please note, there is a line apply plugin: 'com.android.library' not apply plugin: 'com.android.application'):

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

buildscript {
repositories {
    mavenCentral()


}
dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
   // applicationId "com.aroniez.materialsnap"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    renderscriptTargetApi 20
    renderscriptSupportModeEnabled true
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
packagingOptions {
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
//google libraries
//compile 'com.android.support:support-v4:23.1.1'

//square libraries
compile('com.squareup.retrofit2:retrofit:2.0.0-beta4') {
    exclude module: 'okhttp'
}

//dagger for DI
apt 'com.google.dagger:dagger-compiler:2.0.2'
provided 'javax.annotation:jsr250-api:1.0'
//provided 'org.glassfish:javax.annotation:10.0-b28'

//library for fonts
//compile 'uk.co.chrisjenx:calligraphy:2.1.0'

//for testing
debugCompile('org.mockito:mockito-core:1.10.19') {
    exclude group: 'org.hamcrest'
}
debugCompile('com.google.dexmaker:dexmaker:1.2') {
    exclude group: 'org.hamcrest'
}
debugCompile('com.google.dexmaker:dexmaker-mockito:1.2') {
    exclude group: 'org.hamcrest'
}

debugCompile 'commons-io:commons-io:2.4'

androidTestCompile('com.android.support.test.espresso:espresso-core:2.0') {
    exclude group: 'javax.inject'
}
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.0') {
    exclude group: 'javax.inject'
    exclude group: 'com.android.support'
}

compile 'com.android.support:palette-v7:23.2.0'
compile 'com.android.support:design:23.2.0'
compile 'com.android.support:recyclerview-v7:23.2.0'
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
compile 'com.colintmiller:simplenosql:0.5.1'
compile 'com.pkmmte.view:circularimageview:1.1'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.google.dagger:dagger:2.0.2'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'me.iwf.photopicker:PhotoPicker:0.2.8@aar'
compile 'com.github.bumptech.glide:glide:3.6.1'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.soundcloud.android:android-crop:1.0.1@aar'
compile 'jp.wasabeef:glide-transformations:1.3.1'
compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.3.0'
compile 'net.danlew:android.joda:2.9.2'
compile 'com.github.danylovolokh:hashtag-helper:1.1.0'
compile 'com.google.android.gms:play-services-ads:8.4.0'
}

This is the build.cradle from the project:

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

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




allprojects {
repositories {
    jcenter()
    mavenCentral()
    maven{
        url 'https://oss.sonatype.org/content/repositories/snapshots/'
    }
}
}

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

It seems Dagger 2 will not work when I set the plugin from "application" to "library" in the module build.gradle

I have tried a lot of things. But I am unable to solve it.

So, is there anybody outside with some experience to help a little bit? If yes, please try to explain to me what´s wrong (i am not a specialist with a dagger). It would be very nice if the problem could be solved!

Rahul
  • 3,293
  • 2
  • 31
  • 43
BakteriusD
  • 77
  • 1
  • 2
  • 8

1 Answers1

-1

Steps to include dagger 2 in a project

add in Project level build.gradle dependencies

    classpath 'com.android.tools.build:gradle:2.1.3'
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'

add plugin in module level build.gradle

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

add in module level build.gradle dependencies

compile 'com.google.dagger:dagger:2.0.1'
apt 'com.google.dagger:dagger-compiler:2.0.1'
provided 'org.glassfish:javax.annotation:10.0-b28'

thats all, now sync the project and you are ready to use Dagger2 in your project.

Veer3383
  • 1,785
  • 6
  • 29
  • 49
  • Thanks for the answer. But this is exactly what i have (except provided 'org.glassfish:javax.annotation:10.0-b28' . Instead of this i using provided 'javax.annotation:jsr250-api:1.0') So i try your solution, but (sure) same effect. Please note, the modul is a library, not an application. In the old Android Studio Project it works as application. Any other idea? – BakteriusD Aug 24 '16 at 13:28
  • Ok, as far as i understand, we need to implement our interface(your injector interface) in Application class, which unfortunately you wont have in your library module. hence you dont get the dagger generated classes which are shown in the error above. have you checked this answer? if not yet then do try, might help you. http://stackoverflow.com/a/37128723/3345864 – Veer3383 Aug 24 '16 at 14:45