简体   繁体   English

Dagger 2 在 Android Studio 库模块中使用

[英]Dagger 2 using in Android Studio Library Module

I'm unable to find a solution to bring an Android Studio Library with Dagger 2 to run.我找不到一个解决方案来运行带有Dagger 2Android Studio库。

I will explain what happened:我将解释发生了什么:

  1. I have a running Android Studio Project that using Dagger 2我有一个正在运行的 Android Studio 项目,它使用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事实上:现在我有一个包含 3 个库模块和我的应用程序(应用程序)模块的项目
  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:等等......这是带有红色下划线的java文件片段:

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'):这是我的 build.gradle 模块(请注意,有一行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:这是项目中的 build.cradle:

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当我在模块 build.gradle 中将插件从“应用程序”设置为“库”时, Dagger 2似乎不起作用

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!如果问题能解决就太好了!

Steps to include dagger 2 in a project在项目中包含 Dagger 2 的步骤

add in Project level build.gradle dependencies添加项目级别的build.gradle依赖项

    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模块build.gradle添加插件

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

add in module level build.gradle dependencies添加模块build.gradle依赖项

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.就是这样,现在同步项目,您就可以在项目中使用 Dagger2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM