android - Dagger2 依赖 - Gradle

标签 android android-gradle-plugin dagger-2

我正在尝试将 Dagger2 添加到我在 Android Studio 中的项目中,但我找不到合适的依赖项来粘贴到 build.gradle 中。你能帮我把正确的线路发给我吗?

最佳答案

Installing Dagger 2 on Android Studio 2

// Application build.gradle
dependencies {
    compile 'com.google.dagger:dagger:2.4'
    annotationProcessor "com.google.dagger:dagger-compiler:2.4"
}

Maven 存储库:

在 Maven 存储库中找到上述依赖项的最新版本:


注意事项:Android Studio < 2.2

旧版本的Android Studio需要android-apt进行注解处理。

// Project build.gradle
buildscript {
    dependencies {
        // Assists in working with annotation processors for Android Studio.
        // No longer needed with Android Studio 2.2+
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }
}
apply plugin: 'com.neenbedankt.android-apt'

// Application build.gradle
dependencies {
    compile 'com.google.dagger:dagger:2.4'
    apt "com.google.dagger:dagger-compiler:2.4"
}

注意事项: Dagger <2.1

对于 Dagger < 2.1-SNAPSHOT,javax.annotation 是 Dagger 生成代码中使用的 @Generated 注释所必需的(参见 github.com/google/dagger/issues/95)。注释不包含在 Android API jar 中,因此您需要使用以下库之一 (see differences):

// Application build.gradle
dependencies {
    compile 'javax.annotation:jsr250-api:1.0'
}

关于android - Dagger2 依赖 - Gradle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28661693/

相关文章:

java - 添加android项目依赖失败

java - 使用 Dagger 2 的自定义注解拦截器

android - 使用 ionic-v3 和 cordova 6.3.0 警告 Android API 级别 28

android - 发生内部错误。 [MISSING_CLIENT_IDENTIFIER]

android - 加载 url 后无法隐藏 webview

android - 更新 dexguard 版本时抛出错误 - Crashlytics 插件在 DexGuard 插件之前应用

android - 使用 annotationProcessor 而不是 android-apt 的带有 Android Studio 3.0 Preview (Canary 2) 的 Dagger 2

android - void inject(Activity activity) 和 SomeComponent getSomeComponent() 的区别

android - 为什么 RecognitionListener 在 JellyBean 中停止工作?

android - 如何在任何一个 Activity 中每秒更新标题布局文本并显示在所有屏幕中?