Android 如何配置dagger + gradle

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22976251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:38:20  来源:igfitidea点击:

How to configure dagger + gradle

androidgradledagger

提问by Necronet

I have a project and migrating to gradle dependency, but I find myself with an issue trying to setup dagger with gradle, the first time I compile it work perfectly (or if I clean) but if I try it twice then it gives me error like:

我有一个项目并迁移到 gradle 依赖项,但是我发现自己在尝试使用 gradle 设置 dagger 时遇到了问题,我第一次编译它时可以完美运行(或者如果我清理了它),但是如果我尝试两次,它会给我类似的错误:

Error:(13, 14) error: duplicate class: com.myapp.android.application.InjectingApplication$InjectingApplicationModule$$ModuleAdapter

错误:(13, 14) 错误:重复类:com.myapp.android.application.InjectingApplication$InjectingApplicationModule$$ModuleAdapter

I try using android-apt plugin and configured as in the documentation but I still get the same error (https://bitbucket.org/hvisser/android-apt/overview)

我尝试使用 android-apt 插件并按照文档进行配置,但仍然出现相同的错误(https://bitbucket.org/hvisser/android-apt/overview

I also try using provided dependency instead like in this tutorial (https://github.com/frankdu/android-gradle-dagger-tutorial) of compile but no luck so far.

我也尝试使用提供的依赖项,而不是像在编译的本教程(https://github.com/frankdu/android-gradle-dagger-tutorial)中那样,但到目前为止还没有运气。

Do you have any ideas how to configure dagger and gradle?

您对如何配置 dagger 和 gradle 有任何想法吗?

EDIT

编辑

My build.gradle looks like this

我的 build.gradle 看起来像这样

apply plugin: 'android'
apply plugin: 'android-apt'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.2"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 19
        packageName "com.myapp.android"

    }

    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}


dependencies {
    compile project(':volley')
    apt 'com.squareup.dagger:dagger-compiler:1.2.0'
    compile 'com.squareup.dagger:dagger:1.2.0'
}

And my top level build.gradle look like this

我的顶级 build.gradle 看起来像这样

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2'
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

EDIT#2:

编辑#2:

I tried with provided again as @Marco suggested no luck, I don't know if there is a library or a version of gradle that could be causing this problem, I'm currently using 1.10. On the bright side I did find a way to make it work, but I would love to do it by just adding the provided statement. The way I did it is the old way:

我再次尝试提供,因为@Marco 建议没有运气,我不知道是否有可能导致此问题的库或 gradle 版本,我目前使用的是 1.10。从好的方面来说,我确实找到了一种使它工作的方法,但我很乐意通过添加提供的语句来做到这一点。我这样做的方式是旧的方式:

Define apt configuration

定义apt配置

configurations {
     apt
}

add Dagger compiler lib

添加 Dagger 编译器库

apt 'com.squareup.dagger:dagger-compiler:1.2.0'

And implement the this hook to applicationVariant which as far as I know android-apt does something similar. Does this make sense? why?

并将这个钩子实现到 applicationVariant,据我所知 android-apt 做了类似的事情。这有意义吗?为什么?

def getSourceSetName(variant) {
    return new File(variant.dirName).getName();
}

android.applicationVariants.each { variant ->
    def aptOutputDir = project.file("build/source/apt")
    def aptOutput = new File(aptOutputDir, variant.dirName)


    android.sourceSets[getSourceSetName(variant)].java.srcDirs+= aptOutput.getPath()

    variant.javaCompile.options.compilerArgs += [
            '-processorpath', configurations.apt.getAsPath(),
            '-s', aptOutput
    ]

    variant.javaCompile.source = variant.javaCompile.source.filter { p ->
        return !p.getPath().startsWith(aptOutputDir.getPath())
    }

    variant.javaCompile.doFirst {
        aptOutput.mkdirs()
    }
}

采纳答案by Marco RS

I am using dagger in this sample Volley Examples. I'm not experiencing any problems with dagger and I'm including the compiler using:

我在这个示例Volley Examples 中使用了匕首。我在使用 dagger 时没有遇到任何问题,我将编译器包括在内:

provided 'com.squareup.dagger:dagger-compiler:1.2.1'

回答by Sam

Update Gradle plugin to (root/gradle) classpath 'com.android.tools.build:gradle:2.2.0'

将 Gradle 插件更新到 (root/gradle) 类路径 'com.android.tools.build:gradle:2.2.0'

app/gradle

应用程序/gradle

compile 'com.google.dagger:dagger:2.4'
annotationProcessor 'com.google.dagger:dagger-compiler:2.4'

回答by xiezefan

It is work for me.

这对我来说是工作。

Step 1:
Add this code to you build.gradle

第 1 步:
将此代码添加给您build.gradle

provided 'com.squareup.dagger:dagger-compiler:1.2.2'

Step 2:
Add source code folder app/gento you project. So you can add this code to you app/build.gradle(src/main/javais you project core code folder)

第 2 步:
将源代码文件夹添加app/gen到您的项目中。所以你可以添加这个代码给你app/build.gradlesrc/main/java你是项目核心代码文件夹)

sourceSets.main {
    java.srcDirs = ['src/main/java', 'gen']
}